I have been having trouble populating a select element with PHP. Currently, I create an array with information pulled from a database. This is used to edit or insert an entry into a logbook for my record keeping.
The variables used in the code deal with customer numbers and customer names, if that helps give any extra relevance.
The method outlined below works perfectly in the case that it is not a new entry (editing an existing entry), but the options do not populate in the case of a new entry (where $ID == 0).
$ID = $_GET['ID'];
connectDB();
$mons = "SELECT * FROM mons ORDER BY mName ASC";
$mons = queryHashArray($mons);
disconnectDB();
<tr><td><select name="mID"> <?php
foreach($mons as $m) {
if($ID == 0) { ?>
<option value="<?= $m['cusNum'] ?>"><?= $m['mName'] ?></option> <?php }
else { ?>
<option value="<?= $m['cusNum'] ?>" <?php if($record['mID'] == $m['cusNum']) echo "selected=\"selected\""; ?> ><?= $m['mName'] ?></option> <?php
}
}?>
</select></td>
Any help would be appreciated. I could not find an example online that was specific enough to my issue. I hope it is something small and simple that I have overlooked.
UPDATE:
Below is the page source from editing an existing entry and from a new entry.
Existing Entry: Due to privacy concerns, option text has been changed to "Name Omitted".
<tr><td><select name="mID"> <option value="3" selected="selected" >Name Omitted</option> <option value="0" >Name Omitted</option> <option value="0" >Name Omitted</option>
New Entry:
<tr><td><select name="mID"> <br />
<b>Warning</b>: Invalid argument supplied for foreach() in <b>/home/folder/public_html/subfolder/file.php</b> on line <b>265</b><br />
</select></td>
$record is defined elsewhere in the code and is the record which is being edited. If it is a new record, all values are set to ''.
Does this help?
It looks like your queryHashArray
function isn't returning an array when the ID is 0.
Make sure that the ID or the $_GET['ID'] variable isn't overwriting one used in your function.
Try print_r($mons)
just after you run the function. That will show you what the function is returning.