Jump to content

creating 2 drop down menues from the same table


spires

Recommended Posts

Hi,

I'm trying to create 2 dropdown menus.
both will take data from the same table but from different columns.
one will displays all of the values in 'type' column
and the second will displays all of the values in the 'cat' column 

I can get either one on its own to work, but not together.

The problem is: In both dropdown menues i have a while loop (going through all the values in the table). But the both contain a mysql_fetch_array().

Is there any way around this.

select from database.
[code]
$query = "SELECT * FROM type order by id DESC";
$result = mysql_query($query) or die ("query 1 failed");
$count = mysql_num_rows($result);
[/code]

type column
[code]
<?php
echo
'<form name="type1" method="post" action="">
<select name="type">
<option value="NULL" selected>- - - - - - - </option>';

while ($row = mysql_fetch_array($result)) {
echo '<option value="'.$row['type'].'">'.$row['type'].'</option>';   }

echo '</select>
</form>';
?>
[/code]


cat column
[code]
<?php
echo
'<form name="cat1" method="post" action="">
<select name="cat">
<option value="NULL" selected>- - - - - - - </option>';

while ($row1 = mysql_fetch_array($result)) {
echo '<option value="'.$row1['cat'].'">'.$row1['cat'].'</option>';  
}

  echo '</select>
</form>';
?>
[/code]


maybe a foreach loop?
But, i'm not to sure how they work.

Thanks
[code]<?php
  $cathtml='<select name="cat"><option value="null" selected="selected">- - - - - - - -</option>';
  $typehtml='<select name="type"><option value="null" selected="selected">- - - - - - - -</option>';
  $query = mysql_query("SELECT * FROM type order by id DESC");
  while ($row=mysql_fetch_array($query)) {
    $cathtml.='<option value="'.$row['cat'].'">'.$row['cat'].'</option>';
    $typehtml.='<option value="'.$row['type'].'">'.$row['type'].'</option>';
  }
  $cathtml.='</select>';
  $typehtml.='</select>';
  print '<form action="" method="post">';
  print 'Cat: '.$cathtml;
  print 'Type: '.$typehtml;
  print '</form>';
?>[/code]
Just combine both of your while loops into one.  However, instead of echoing... create a string and just keep concatenating it as follows:

[code]
<?php
echo
'<form name="type1" method="post" action="">
<select name="type">
<option value="NULL" selected>- - - - - - - </option>';
$col1 = '<form name="type1" method="post" action="">
<select name="type">
<option value="NULL" selected>- - - - - - - </option>';
        $col2 = '<form name="cat1" method="post" action="">
<select name="cat">
<option value="NULL" selected>- - - - - - - </option>';
while ($row = mysql_fetch_array($result)) {
$col1 .= '<option value="'.$row['type'].'">'.$row['type'].'</option>';
                $col2 .= '<option value="'.$row['cat'].'">'.$row['cat'].'</option>';
  }

  $col1 .= '</select></form>';
                $col2 .= '</select></form>';
          // Now just echo them where ever you need them :)
?>
[/code]

Crap... looks like someone beat me to it :)
Forgot to add that once you have added the submit button you would detect which was chosen by something like this:
[code]if ($_POST['submit']) {
  $chosencat=$_POST['cat'];
  $chosentype=$_POST['type'];
}[/code]This way you only need to create one form.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.