Page 1 of 1

displaying table rows with a dropdown?

Posted: Sun Mar 08, 2009 7:46 pm
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`

Re: displaying table rows with a dropdown?

Posted: Sun Mar 08, 2009 7:55 pm
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>
 

Re: displaying table rows with a dropdown?

Posted: Sun Mar 08, 2009 8:27 pm
by system5
So $resource is the table itself? How would I best represent that when declaring the variable?

Re: displaying table rows with a dropdown?

Posted: Sun Mar 08, 2009 8:31 pm
by Benjamin
$resource is the result of mysql_query();

Re: displaying table rows with a dropdown?

Posted: Sun Mar 08, 2009 8:41 pm
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?

Re: displaying table rows with a dropdown?

Posted: Sun Mar 08, 2009 8:50 pm
by Benjamin
I don't know what you mean.

Re: displaying table rows with a dropdown?

Posted: Sun Mar 08, 2009 8:54 pm
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.

Re: displaying table rows with a dropdown?

Posted: Sun Mar 08, 2009 8:58 pm
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.

Re: displaying table rows with a dropdown?

Posted: Thu Mar 12, 2009 8:56 am
by system5
Thats great thanks!