We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,183 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Add returned record values to select box in php

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

2
Contributors
5
Replies
10 Hours
Discussion Span
6 Months Ago
Last Updated
6
Views
Question
Answered
AndreRet
Industrious Poster
4,706 posts since Jan 2008
Reputation Points: 391
Solved Threads: 481
Skill Endorsements: 20

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>
GliderPilot
Posting Whiz in Training
239 posts since Sep 2006
Reputation Points: 34
Solved Threads: 42
Skill Endorsements: 12

Hahahaha, how could I miss something that stupid.

Thanx Glider, all sorted now.

AndreRet
Industrious Poster
4,706 posts since Jan 2008
Reputation Points: 391
Solved Threads: 481
Skill Endorsements: 20
Question Answered as of 6 Months Ago by GliderPilot

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?

AndreRet
Industrious Poster
4,706 posts since Jan 2008
Reputation Points: 391
Solved Threads: 481
Skill Endorsements: 20
<option value="<?php echo $record['type'];?>" <?php if($record['type'] == $vehicle_type) {echo "SELECTED";} ?>><?php echo $record['type'];?></option>
GliderPilot
Posting Whiz in Training
239 posts since Sep 2006
Reputation Points: 34
Solved Threads: 42
Skill Endorsements: 12

Thank you, got it now!! lol.

AndreRet
Industrious Poster
4,706 posts since Jan 2008
Reputation Points: 391
Solved Threads: 481
Skill Endorsements: 20

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0727 seconds using 2.68MB