Jump to content


Photo

Working with Select menu in PHP


  • This topic is locked This topic is locked
2 replies to this topic

#1 saikiran

saikiran
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 10 July 2006 - 08:12 AM

hi,

First post in this forum

i am using a drop down select menu to choose a particular category of products from my database table.

I want to give one default option in the select menu, Select ALL, whenever user select this and press the submit button it should retrieve all the records.

kindly check out the below given code and help me out.


Dynamic select menu code


<?php include('config.php');?>
<? $query = "SELECT distinct product FROM prod_catlog2";
  $result = mysql_query($query);
 
  $pulldown = '<option>Select one option</option>';
  while($row=mysql_fetch_array($result,MYSQL_ASSOC))
  {
  $pulldown .= "<option value  = \"{$row['product']}\">{$row['product']}</option>\n";
  }?>
 
  <html>
<title>Order Form</title>
<form action="show_ord_det_pg_org.php" method="POST">
Select your category<select name="cat">
<?php echo $pulldown;?> 
</select>
<br>
<input type="submit" name="submit" value="Show Details">
</form>
</html>



show_ord_det_pg_org.php

...

<?php
  $cat = $_POST['cat'];
  $sql = "select * from prod_catlog2 where product = '$cat'";
  $result = mysql_query($sql)or die(mysql_errno().mysql_error());?> 
....


here $cat is the variable which stores the option that user will pick from the select menu, whose name is cat.

kindly help me out

cheers
sai
saisen76@hotmail.com





#2 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 10 July 2006 - 09:53 AM

<?php include('config.php');?>
<? $query = "SELECT distinct product FROM prod_catlog2";
  $result = mysql_query($query);
 
  $pulldown = '<option>Select one option</option>';
  $pulldown .= '<option>ALL</option>';

  while($row=mysql_fetch_array($result,MYSQL_ASSOC))
  {
      $pulldown .= "<option value  = \"{$row['product']}\">{$row['product']}</option>\n";
  }?>
 
  <html>
<title>Order Form</title>
<form action="show_ord_det_pg_org.php" method="POST">
Select your category<select name="cat">
<?php echo $pulldown;?> 
</select>


<input type="submit" name="submit" value="Show Details">
</form>
</html>

<?php
        if($_POST['cat']=='ALL')
        {$cat='%';}
else
        {$cat = $_POST['cat'];}
        $sql = "select * from prod_catlog2 where product like '$cat'";
        $result = mysql_query($sql)or die(mysql_errno().mysql_error());?>   


#3 saikiran

saikiran
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 10 July 2006 - 11:01 AM

hi,

Thank u AV1611. You  have solved my problem. Thank you once again.


cheers
saikiran




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users