Stuck again... :)

I am returning close to 60 records from a search in mysql. I want to add these records to a select box.

When I am looping through the records, it adds a select box for each record. Not sure where I am going wrong. It does load the correct values, but one in each select box... -

<?php foreach($recordset as $record){ ?>
       <select name="model" id="model" class="searchselectbox" onClick="toggleVisibility('pricelabel'); toggleVisibility('price');" onchange="this.form.submit();">
            <?php echo '<option value="'.$record['type'].'">'.$record['type'].'</option>';?>
       </select>

       <?php
            }
        ?>

I had the code like this below as well, still adding a select box for each record...

<?php foreach($recordset as $record){ ?>
       <select name="model" id="model" class="searchselectbox" onClick="toggleVisibility('pricelabel'); toggleVisibility('price');" onchange="this.form.submit();">
            <option value="<?php echo $record['type'];?>"><?php echo $record['type'];?></option>
       </select>

       <?php
            }
        ?>

How can I get the option values loaded into only ONE select box? I have found plenty sample codes reflecting the above, mine just don't seem to want to work. :)

Recommended Answers

All 5 Replies

You have to put your <select></select tags outside the foreach loop or it will start a new select box for each:

<select name="model" id="model" class="searchselectbox" onClick="toggleVisibility('pricelabel'); toggleVisibility('price');" onchange="this.form.submit();">
  <?php foreach($recordset as $record){ ?>
    <option value="<?php echo $record['type'];?>"><?php echo $record['type'];?></option>
  <?php            }        ?>
</select>
commented: thanx, simple as that! :) +12

Hahahaha, how could I miss something that stupid.

Thanx Glider, all sorted now.

a Little of topic if I may, still related though -

There is 3 select boxes, each one returns a different field from database i.e., in database - 2010 BMW 320i, record 2 2008 Aston Martin DB9 and so on.

What I do is to first get user to select a year, then submit form so I can return all vehicles with that year tied to them, then manufacturer (BMW, Aston Martin etc), then the type vehicle (320i, DB9 etc).

Problem is that the select goes back to default values once submitted/posted. How can I show the return values again. I still have the values available in php, i.e -

$vehicle_year
$vehicle_make

I'm just not sure how to "re-show" them in the select options?

<option value="<?php echo $record['type'];?>" <?php if($record['type'] == $vehicle_type) {echo "SELECTED";} ?>><?php echo $record['type'];?></option>
commented: Thanx for off topic reply. :) +0

Thank you, got it now!! lol.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.