displaying table rows with a dropdown?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
system5
Forum Newbie
Posts: 14
Joined: Wed Feb 25, 2009 12:38 pm

displaying table rows with a dropdown?

Post by system5 »

Hi guys, you've been pretty helpful with my newbie questions.

I was wondering if it was possible to display MYSQL table data by a drop down box? I've looked for a bunch of tutorials but can't seem to find anything. Currently I'm using this code to display the data itself.

Code: Select all

<?php
$connection = mysql_connect("localhost", "username", "password") or die("Error connecting to database");
mysql_select_db("database", $connection);
$result = mysql_query("SELECT * FROM room ORDER BY roomId", $connection) or die("error querying database");
$i = 0;
while($result_ar = mysql_fetch_assoc($result)){
?>
<tr <?php if($i%2 == 1){ echo "class='body2'"; }else{echo "class='body1'";}?>>
<td>
<?php echo $result_ar['roomName']; ?></td>
<td>
<img src=<?php echo $result_ar['roomPicture']; ?>>
</td>
<td>
<?php echo $result_ar['roomItem1']; ?></td>
</td>
<td>
<?php echo $result_ar['roomItem2']; ?></td>
</td>
<td>
<?php echo $result_ar['roomItem3']; ?></td>
</td>
<?php
$i+=1;
}
 ?>
I'm trying to create a drop down that displays the data depending what campus is selected. So my SQL would be:

Code: Select all

SELECT `roomName` , `roomPicture` , `roomItem1` , `roomItem2` , `roomItem3` , `maxpeople`
FROM `room`
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: displaying table rows with a dropdown?

Post by Benjamin »

I think you are looking for something like this.

Code: Select all

 
<select name="foo">
  <?php while ($row = mysql_fetch_assoc($resource)); ?>
    <option value="<?php echo $row['id']; ?>"><?php echo $row['option']; ?></option>
  <?php } ?>
</select>
 
system5
Forum Newbie
Posts: 14
Joined: Wed Feb 25, 2009 12:38 pm

Re: displaying table rows with a dropdown?

Post by system5 »

So $resource is the table itself? How would I best represent that when declaring the variable?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: displaying table rows with a dropdown?

Post by Benjamin »

$resource is the result of mysql_query();
system5
Forum Newbie
Posts: 14
Joined: Wed Feb 25, 2009 12:38 pm

Re: displaying table rows with a dropdown?

Post by system5 »

Sorry for all these dumb questions.

So basically that code updates the drop down box with whats in the table?

How would it would it update the table display to only show what was selected in the drop down box?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: displaying table rows with a dropdown?

Post by Benjamin »

I don't know what you mean.
system5
Forum Newbie
Posts: 14
Joined: Wed Feb 25, 2009 12:38 pm

Re: displaying table rows with a dropdown?

Post by system5 »

Using a table to display the data of the table, while using a drop down box to select what data is shown in the table.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: displaying table rows with a dropdown?

Post by Benjamin »

You can use the javascript onchange() event in the select menu to submit the form. The form method should be set to get. You can then use the value of the get variable from the select menu to determine what data to be displayed.
system5
Forum Newbie
Posts: 14
Joined: Wed Feb 25, 2009 12:38 pm

Re: displaying table rows with a dropdown?

Post by system5 »

Thats great thanks!
Post Reply