0

Basically, I have this code and everything works in it EXCEPT I want the dropdown box to fill with options from another database table.

When I put new info into the other database table, I want it to be available for me to select it as an option in the dropdown box. I marked right above the line and I want to autofill with options from the other database table.

Thanks! It's near the bottom I just wanted you to have the WHOLE function.

       <?php 
       function editarea($id) {

    if (isset($_POST["submit"])) {

        extract($_POST);
        $errors = 0;
        $errorlist = "";
        if ($name == "") { $errors++; $errorlist .= "Name is required.<br />"; }
        if ($latitude == "") { $errors++; $errorlist .= "Latitude is required.<br />"; }
        if (!is_numeric($latitude)) { $errors++; $errorlist .= "Latitude must be a number.<br />"; }
        if ($longitude == "") { $errors++; $errorlist .= "Longitude is required.<br />"; }
        if (!is_numeric($longitude)) { $errors++; $errorlist .= "Longitude must be a number.<br />"; }
        if ($elevation == "") { $errors++; $errorlist .= "Elevation is required.<br />"; }
        if (!is_numeric($elevation)) { $errors++; $errorlist .= "Elevation must be a number.<br />"; }
        if ($creatureid == "") { $errors++; $errorlist .= "creatureid is required.<br />"; }
        if (!is_numeric($creatureid)) { $errors++; $errorlist .= "creatureid must be a number.<br />"; }
        if ($exploretext == "") { $errors++; $errorlist .= "Explore Text is required.<br />"; }

        if ($errors == 0) { 
            $query = doquery("UPDATE {{table}} SET name='$name',latitude='$latitude',longitude='$longitude',elevation='$elevation',creatureid='$creatureid',n='$n',s='$s',e='$e',w='$w',nw='$nw',ne='$ne',se='$se',sw='$sw',u='$u',d='$d' ,exploretext='$exploretext' WHERE id='$id' LIMIT 1", "area");
            admindisplay("Area updated.","Edit Area");
        } else {
            admindisplay("<b>Errors:</b><br /><div style=\"color:red;\">$errorlist</div><br />Please go back and try again.", "Edit Area");
        }        

    }   


    $query = doquery("SELECT * FROM {{table}} WHERE id='$id' LIMIT 1", "area");
    $row = mysql_fetch_array($query);

$page = <<<END
<b><u>Edit Area</u></b><br /><br />
<form action="admin.php?do=editarea:$id" method="post">
<table width="90%">
<tr><td width="20%">ID:</td><td>{{id}}</td></tr>
<tr><td width="20%">Name:</td><td><input type="text" name="name" size="30" maxlength="30" value="{{name}}" /></td></tr>
<tr><td width="20%">Latitude:</td><td><input type="text" name="latitude" size="5" maxlength="10" value="{{latitude}}" /><br /><span class="small">Positive or negative integer.</span></td></tr>
<tr><td width="20%">Longitude:</td><td><input type="text" name="longitude" size="5" maxlength="10" value="{{longitude}}" /><br /><span class="small">Positive or negative integer.</span></td></tr>
<tr><td width="20%">Elevation:</td><td><input type="text" name="elevation" size="5" maxlength="10" value="{{elevation}}" /><br /><span class="small">Positive or negative integer.</span></td></tr>
<tr><td width="20%">Creature ID:</td><td><input type="text" name="creatureid" size="5" maxlength="10" value="{{creatureid}}" /> ID<br /><span class="small">Positive integer.</span></td></tr>
<tr><td width="20%">Exploring Text:</td><td><input type="text" name="exploretext" size="50" maxlength="255" value="{{exploretext}}" /><br /><span class="small">Exploring Text to be seen.</span></td></tr>
<tr><td width="20%">Value connects NORTH to:</td><td><input type="text" name="n" size="5" maxlength="10" value="{{n}}" /><br /><span class="small">Area ID.</span></td></tr>
<tr><td width="20%">Value connects EAST to:</td><td><input type="text" name="e" size="5" maxlength="10" value="{{e}}" /><br /><span class="small">Area ID.</span></td></tr>
<tr><td width="20%">Value connects SOUTH to:</td><td><input type="text" name="s" size="5" maxlength="10" value="{{s}}" /><br /><span class="small">Area ID.</span></td></tr>
<tr><td width="20%">Value connects WEST to:</td><td><input type="text" name="w" size="5" maxlength="10" value="{{w}}" /><br /><span class="small">Area ID.</span></td></tr>
<tr><td width="20%">Value connects NORTHWEST to:</td><td><input type="text" name="nw" size="5" maxlength="10" value="{{nw}}" /><br /><span class="small">Area ID.</span></td></tr>
<tr><td width="20%">Value connects NORTHEAST to:</td><td><input type="text" name="ne" size="5" maxlength="10" value="{{ne}}" /><br /><span class="small">Area ID.</span></td></tr>
<tr><td width="20%">Value connects SOUTHEAST to:</td><td><input type="text" name="se" size="5" maxlength="10" value="{{se}}" /><br /><span class="small">Area ID.</span></td></tr>
<tr><td width="20%">Value connects SOUTHWEST to:</td><td><input type="text" name="sw" size="5" maxlength="10" value="{{sw}}" /><br /><span class="small">Area ID.</span></td></tr>
<tr><td width="20%">Value connects UP to:</td><td><input type="text" name="u" size="5" maxlength="10" value="{{u}}" /><br /><span class="small">Area ID.</span></td></tr>
<tr><td width="20%">Value connects DOWN to:</td><td><input type="text" name="d" size="5" maxlength="10" value="{{d}}" /><br /><span class="small">Area ID.</span></td></tr>



<------------------THIS one below is the one i want to autopull for dropbox-------------------->
<tr><td width="20%">North Connection:</td><td><select name="area"><option value="0" {{n0select}}>None</option><option value="{{id}}">$row[name] Lat:$row[latitude] Long:$row[longitude] Elev:$row[elevation]</option></select><br /><span class="small">test</span></td></tr>
</table>
<input type="submit" name="submit" value="Submit" /> <input type="reset" name="reset" value="Reset" />
</form>

END;
    if ($row["n"] == 0) { $row["n1select"] = "selected=\"selected\" "; } else { $row["n1select"] = ""; }
    if ($row["n"] != 1) { $row["n2select"] = "selected=\"$row[n]\" "; } else { $row["n2select"] = ""; }


    $page = parsetemplate($page, $row);
    admindisplay($page, "Edit Area");

}
joan16v
  • 5,055
  • 4
  • 49
  • 49
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Nov 11 '15 at 13:27
  • i have tons and tons of code allready programmed in mysql... i cannot go thru thousands of lines of code and change all of it into mysqli as it would take to long and i don't believe i would be successful at it at this point. – Zack Michael Thompson Nov 11 '15 at 20:56
  • i am using mysql.... not PDO or mysqli.. i posted my current code so i could know what to do for MYSQL only. i can't reprogram thousands of lines right now.. so i just need the code for mysql. – Zack Michael Thompson Nov 13 '15 at 02:17

1 Answers1

0

You can add options within your <select> element using PHP. Fetch the items you want to print as options in an array, and you can loop through the items and print them accordingly.

You can try something like this:

<select name="area">
    <option value="#">-- Make a choice --</option>
    <?php
    $query = 'SELECT * FROM `example` WHERE `projectId` = 1'; // Example query to fetch some objects.

    $result = $pdo->query($query);

    if ($result->rowCount() > 0) { // Check if result has rows.
        foreach ($result as $row) {
            echo '<option value="'.$row['id'].'">'.$row['title'].'</option>'; // Print row as a new option in the select dropdown.
        }
    }
    ?>
</select>
Audite Marlow
  • 1,034
  • 1
  • 7
  • 25
  • Why should the OP "try something like this"? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO. – Jay Blanchard Nov 11 '15 at 13:28
  • I modified my answer with a short explanation. I don't feel like I can go into details very much, since it's pretty short and straightforward code. Maybe you can show me where or how I can learn to provide good and helpful answers? – Audite Marlow Nov 11 '15 at 13:32
  • i am using mysql.... not PDO or mysqli.. i posted my current code so i could know what to do for MYSQL only. – Zack Michael Thompson Nov 13 '15 at 02:15
  • `$result = mysql_query($query);`, then just use `mysql_fetch_assoc($result)` to fetch the result. Also, I highly recommend dropping PHP's MySQL functions for MySQLi or PDO, as the MySQL functions are deprecated and removed in PHP 7. – Audite Marlow Nov 13 '15 at 09:07