0
votes

I have a PHP select that dynamically populates based on a MySQL recordset and uses a in_array value to identify and select the value(s) found in the database like this:

<select name="StaffSetSelection[]" size="5" multiple="MULTIPLE" id="StaffSetSelection">
    <option <?php if ($totalRows_StaffSetID == 0) { echo "selected"; } ?> value="">Choose a Staff Set</option>
    <?php
    do {
        ?>
        <option <?php if (in_array($row_StaffSetChoices['EmpNumber'], $StaffSetIDs)) {
            echo "selected";
        } ?> value="<?php echo $row_StaffSetChoices['EmpNumber'] ?>"><?php echo $row_StaffSetChoices['EmpFirstName'] ?></option><?php
    } while ($row_StaffSetChoices = mysql_fetch_assoc($StaffSetChoices));
    $rows = mysql_num_rows($StaffSetChoices);
    if ($rows > 0) {
        mysql_data_seek($StaffSetChoices, 0);
        $row_StaffSetChoices = mysql_fetch_assoc($StaffSetChoices);
    }
    ?>
</select>

Instead of displaying the selected values in a form option select I'd like to just display them as text.

I've tried to use the in_array again in a simple php echo but it does not display the data and there are no errors in my apache log.

This is what I've tried:

<?php if(in_array($row_StaffSetChoices['EmpNumber'], $StaffSetIDs)) echo $row_StaffSetChoices['EmpFirstName']?>
1
Your code makes no sense. "If query returns non-zero rows, rewind fetch to start of result set". This would be an infinite loop for any queries which return any rows... - Marc B
It works great and is used if there is not prior selection. It's not part of the question though so can be removed as a factor if you want @MarcB - Rocco The Taco

1 Answers

0
votes

I think you forgot the brackets, Try this way:

<?php 
     if(in_array($row_StaffSetChoices['EmpNumber'], $StaffSetIDs)) {
         echo $row_StaffSetChoices['EmpFirstName'];
     } 
?>