Jump to content


Photo

creating 2 drop down menues from the same table


  • Please log in to reply
7 replies to this topic

#1 spires

spires
  • Members
  • PipPipPip
  • Advanced Member
  • 492 posts

Posted 22 August 2006 - 05:58 PM

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.
$query = "SELECT * FROM type order by id DESC";
$result = mysql_query($query) or die ("query 1 failed");
$count = mysql_num_rows($result);

type column
<?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>';
	?>				


cat column
	<?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>';
	?>


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

Thanks
How to make over $600 a day from AdSense? Discover EXACTLY how he does it in these videos:
http://www.adsense-online.com

#2 Yesideez

Yesideez
  • Members
  • PipPipPip
  • Advanced Member
  • 2,334 posts
  • LocationDevon, UK

Posted 22 August 2006 - 06:08 PM

<?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>';
?>

Not a pro just an enthusiast :)

if (empty($coffee)) {$coffee=new coffee();}

Please surround any code using the CODE tags - I rarely look at anything without them

#3 Corona4456

Corona4456
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts
  • LocationEl Paso, TX

Posted 22 August 2006 - 06:09 PM

Just combine both of your while loops into one.  However, instead of echoing... create a string and just keep concatenating it as follows:

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

Crap... looks like someone beat me to it :)
What is the CoronaMatrix?

#4 Yesideez

Yesideez
  • Members
  • PipPipPip
  • Advanced Member
  • 2,334 posts
  • LocationDevon, UK

Posted 22 August 2006 - 06:13 PM

Forgot to add that once you have added the submit button you would detect which was chosen by something like this:
if ($_POST['submit']) {
  $chosencat=$_POST['cat'];
  $chosentype=$_POST['type'];
}
This way you only need to create one form.
Not a pro just an enthusiast :)

if (empty($coffee)) {$coffee=new coffee();}

Please surround any code using the CODE tags - I rarely look at anything without them

#5 spires

spires
  • Members
  • PipPipPip
  • Advanced Member
  • 492 posts

Posted 22 August 2006 - 06:15 PM

Thanks guy, i'm gonna go try them out.


How to make over $600 a day from AdSense? Discover EXACTLY how he does it in these videos:
http://www.adsense-online.com

#6 Yesideez

Yesideez
  • Members
  • PipPipPip
  • Advanced Member
  • 2,334 posts
  • LocationDevon, UK

Posted 22 August 2006 - 06:17 PM

Sorry for all the editing - I can't figure out why my posts come in boring grey whereas both of yours comes out boxed and in color :S
Not a pro just an enthusiast :)

if (empty($coffee)) {$coffee=new coffee();}

Please surround any code using the CODE tags - I rarely look at anything without them

#7 Corona4456

Corona4456
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts
  • LocationEl Paso, TX

Posted 22 August 2006 - 06:25 PM

I believe it's the '<?php' tag.

<?php echo 'Hello World!'; ?>

What is the CoronaMatrix?

#8 Yesideez

Yesideez
  • Members
  • PipPipPip
  • Advanced Member
  • 2,334 posts
  • LocationDevon, UK

Posted 22 August 2006 - 06:27 PM

Yeah I just figured it out looking back at one of my other posts in anothr topic - thanks ;)
Not a pro just an enthusiast :)

if (empty($coffee)) {$coffee=new coffee();}

Please surround any code using the CODE tags - I rarely look at anything without them




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users