How to pass OR assign a variable to a drop down menu in php.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scaleautostyle
    New Member
    • Aug 2009
    • 14

    How to pass OR assign a variable to a drop down menu in php.

    Hi!! I know the question look weird but this is what I want to do.

    I have to dropdown witch I pass an sql query to populate it. my problem is I really don't know how to pass the value to my search.php

    you can made a test by going here



    and chose 1/24 from the first drop down, nissan from the second and put 24161 in the text field. clic submit and VOILA... the missing info are the scale and the vehicule manufacture.. because variables are not assign to the dropdown. so HOW I can do that??? and how my code will change in the search.php??
    this is my form...


    Code:
    <?php
    include_once("db_connection.php");
    ?>
      
      <html>  
       <head>  
         <meta  http-equiv="Content-Type" content="text/html;  charset=iso-8859-1">   
        <title>Search  engine</title>  
       </head>  
       <p><body>
        
       <table width="920" border="0">
      <tr>
        <td width="524"><p class="style5"><br />
         
         <table width="920" border="0"><tr><td width="39" align="right"><table width="920" border="0">
           <tr>
             <td><?php // start of dropdown
    		 
    $sql = mysql_query("SELECT DISTINCT scale FROM kit ");
    $options = '';
    
    echo '<select scale="scale"><option>Scale</option>';
    
    while($row = mysql_fetch_array($sql)) {
    $thing = $row['scale'];
    echo '<option>'.$thing.'</option>';
    }
    echo '</select>';
    // end of dropdown ///?></td>
             <td><div align="left">
               <?php        //  start of drop down  ///
    		   
    $sql = mysql_query("SELECT DISTINCT manufacturer_reel FROM kit ORDER BY manufacturer_reel");
    $options = '';
    
    echo '<select manufacturer_reel="manufacturer_reel"><option>Reel manufacturer</option>';
    
    while($row = mysql_fetch_array($sql)) {
    $thing = $row['manufacturer_reel'];
    echo '<option>'.$thing.'</option>';
    }
    echo '</select>';
    // end of dropdown ///?>
             </div></td>
            
             </tr>
           
           <tr>
             <td colspan="2"><h1 align="center">Model kit database</h1>
                 <p align="center">You  may search either by vehicule's  year of fabrication , kit manufacturer or kit number*</p>
               <form  method="post" action="search.php?go"  id="searchform">
                   <div align="center">
                     <input  type="text" name="name" />
                     <input  type="submit" name="submit" value="Search" />
                   </div>
               </form></td>
           </tr>
         </table></td>
          </table>
         <br>
         <table width="920" border="0">
           <tr>
             <td><h4 align="center"><strong>*  don't put any hyphen &quot; - &quot; in the kit number.
             Ex: usually revell number are 85-2654. here put 852654</strong></h4>
             </td>
           </tr>
         </table>
    THIS IS THE SEARCH.PHP

    Code:
    <?php
    
    include_once("db_connection.php");
       if(isset($_POST['submit'])){   
       if(isset($_GET['go'])){   
       if(preg_match("/^[  a-zA-Z0-9]+/", $_POST['name'])){   
       $name=$_POST['name'];   
          
         //-query  the database table for the post field  
       $sql="SELECT  kit_id, kit_number, kit_name, description, manufacturer_kit, engine_detail, year_prod_kit, year_prod_reel, image_id, image_id1, image_id2  FROM kit WHERE kit_number LIKE '%" . $name ."%' OR kit_name LIKE '%" . $name .  "%' OR description LIKE '%" . $name .  "%' OR manufacturer_kit LIKE '%" . $name .  "%' OR year_prod_reel LIKE '%" . $name ."%'";  
     
     
      //-run  the query against the mysql query function   
       $result=mysql_query($sql);  
       
      //-make the header of the table   
       
       echo "  <table align=center width=\"1255\" border=\"\">\n";
       echo "    <tr>\n";
       echo"  <td nowrap align=center width='95'>  kit manufacturer  \n";		
       echo"  <td nowrap align=center width='55'>	scale \n";		
       echo"  <td nowrap align=center width='90'>	kit number \n";		
       echo"  <td nowrap align=center width='290'>	kit name \n";		
       echo"  <td nowrap align=center width='400'>	description \n";
       echo"  <td nowrap align=center width='95'>	vehicule manufacturer \n";
       echo"  <td nowrap align=center width='75'>	year of reel production of car \n";		
       echo"  <td nowrap align=center width='75'>	complete engine detail\n";		
       echo"  <td nowrap align=center width='75'>	Select Picture to get bigger\n";
       echo"  <td nowrap align=center width='75'>	instruction sheet\n";
       echo"  <td nowrap align=center width='50'>	box contain\n";
       echo "    <tr>\n";
    
       //-create  while loop and loop through result set   
       while($row=mysql_fetch_array($result)){   
             $description  =$row['description'];   
             $manufacturer_kit=$row['manufacturer_kit'];   
             $kit_id=$row['kit_id']; 
    		 $kit_number=$row['kit_number'];
    		 $kit_name=$row['kit_name'];
    		 $engine_detail=$row['engine_detail'];
    		 $year_prod_reel=$row['year_prod_reel'];
    		 $image_id=$row['image_id'];
    		 $image_id1=$row['image_id1'];
    		 $image_id2=$row['image_id2'];
    
    		 
    		   
       //-create  table of item during he while loop
       
    echo "  <table align=center width=\"1255\" border=\"\">\n";		   
    echo"  <td nowrap align=center width='95'>  $manufacturer_kit  \n";		
    echo"  <td nowrap align=center width='90'>	$kit_number \n";		
    echo"  <td nowrap align=center width='290'>	$kit_name \n";		
    echo"  <td nowrap width='400'>	$description \n";		
    echo"  <td nowrap align=center width='75'>	$year_prod_reel \n";		
    echo"  <td nowrap align=center width='75'>	$engine_detail \n";	
    echo"  <td nowrap width='75'> <a href=".$image_id.">   <img src='".$image_id."' width='75' height='50' border='0'/>";
    echo"  <td nowrap width='75'> <a href= ".$image_id1.">   <img src= 'http://www.scale24-25.com/images/PDF_logo.gif' width='50' height='50' border='0'/>";
    echo"  <td nowrap width='50'> <a href=".$image_id2.">   <img src='http://www.scale24-25.com/images/PDF_logo.gif' width='50' height='50' border='0'/>";
    echo "    </tr>\n";
    echo "    </table>\n"; 
    echo "    </tr>\n";
    echo "    </table>\n";
     
    
       }   
       }  
       else{   
       echo  "<p>Please enter a search query</p>";   
       }   
       }   
       }
     ?>
    THASNK YOU VERY MUCH FOR YOUR HELP

    sebastien
  • chazzy69
    New Member
    • Sep 2007
    • 196

    #2
    Not sure if this will help but i found when working with .php, mysql and html at the same time its usually easy to retrieve all information from mysql database before trying to display.

    for example set it up like this(note this is not the full code you need) -

    Code:
    <?php
    //connect to your database here
    //then retrieve all varibles you will need
    
    $Var[$i] = mysql_query($query,$connection)
    ?>
    <html>
    <head>
    </head>
    <body>
    <select size="1" name="optionbox">
    <option value="1"><?php $var[0]?></option><option value="2"><?php $var[1]?></option>
    <option value="3"><?php $var[2]?></option>
    <option value="4"><?php $var[3]?></option>
    <option value="5"><?php $var[4]?></option>
    <option value="6"><?php $var[5]?></option>
    <option value="7"><?php $var[7]?></option></select>
    </body>
    </html>
    I threw in an example of a drop down box with an example varible standing in for a static value.

    Personally i think its better to run all .php before a page fully loads if possible to reduce loading times otherwise everytime someone chooses a different option on your drop down box your going to make another connection to you database.

    Hope this helps

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      I haven't looked through that thoroughly, but it looks like you're not including the two dropdowns (scale and manufacturer_re el) within your form which only starts on line 52. Also, you have:
      Code:
      <select manufacturer_reel="manufacturer_reel">
      <select scale="scale">
      That is not good HTML and it should be:
      Code:
      <select name="manufacturer_reel">
      <select name="scale">
      I am not sure if that works (I have never tried it), but I doubt it will and it is important to understand why.

      Comment

      Working...