基本上,我有这个代码,一切都在其中工作除了我希望下拉框填充来自另一个数据库表的选项。
当我将新信息放入另一个数据库表时,我希望它可以在下拉框中选择它作为选项。我在行上方标记,我想自动填充其他数据库表中的选项。
谢谢!它接近底部我只是希望你拥有整个功能。
<?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");
}
答案 0 :(得分:0)
您可以使用PHP在<select>
元素中添加选项。获取要作为数组中的选项打印的项目,然后您可以遍历项目并相应地打印它们。
您可以尝试这样的事情:
<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>