0

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. :)

Edited by AndreRet

2
Contributors
5
Replies
8
Views
5 Years
Discussion Span
Last Post by AndreRet
1

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>
Votes + Comments
thanx, simple as that! :)
0

Hahahaha, how could I miss something that stupid.

Thanx Glider, all sorted now.

0

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?

1
<option value="<?php echo $record['type'];?>" <?php if($record['type'] == $vehicle_type) {echo "SELECTED";} ?>><?php echo $record['type'];?></option>
Votes + Comments
Thanx for off topic reply. :)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.