Page 1 of 1

PHP to retrieve specific data in MySQL

Posted: Sun Mar 20, 2011 5:09 pm
by abdorito
Hello,

I have an issue here that is driving me crazy. I have a table in MySQL that has more than 2000 items, each having a code that can go from 2 to 6 digits (so my table has 2 columns, "Code" and "Designation"). I am writing a PHP code for a series of dynamic dependent dropdown lists to filter the table. In the first drop down menu, I want the list of items that have 2-digits code; for the second dropdown code, I want a list of items who have 3-digit codes, of which the first 2-digits are the ones selected previously + a digit between 0-9, and so on.

If I can figure out how to get the first 2 menu working, I will do the same for the remaining 3. Here is my partial code for these 2 first dropdown menus (except the MySQL connection part of the code, which is tested successfully). The code doesn't work and I can't figure out why. I would appreciate any help!


Code: Select all

<form name="theForm" method="get">

    <!-- TWO DIGITS MENU -->
        <select name="two" onChange="autoSubmit();">
        <option value="null"></option>
                   
        <!-- POPULATE DROP DOWN MENU OF TWO DIGITS -->
<?php        
        $sql = "SELECT Code,Designation FROM list WHERE Code ='..'";
        $twos = mysql_query($sql,$conn);  
        while($row = mysql_fetch_array($twos))
        {        
            echo ("<option value=\"$row[Code]\"" . ($two == $row["Code"]?"selected" : "") . ">$row[Designation]</option>");        
        }
   ?>    
    </select>
    <br><br>
    <!-- THREE DIGITS MENU BASED ON TWO DIGITS VALUE -->    
    <?php
        if($two != null && is_numeric($two))
    {
        ?>
        <select name="three" onChange="autoSubmit();">        
        <option value="null"></option>  
        <?php
       
        //POPULATE DROP DOWN MENU WITH THREE DIGITS STARTING WITH GIVEN TWO DIGITS
       
        $sql = "SELECT Code, Designation FROM list WHERE Code = ^$two.";
        $threes = mysql_query($sql,$conn);
        while($row = mysql_fetch_array($threes))
        {        
            echo ("<option value=\"$row[Code]\" " . ($three == $row["Code"] ? " selected" : "") . ">$row[Designation]</option>");        
        }
        ?>        
    </select>    
    <?php
    }    
    ?>    
    <br><br
    <?php
    if($three != null && is_numeric($three) && $two != null)
    {    
    ?>    
    <select name="four" onChange="autoSubmit();">
        <option value="null"></option>

Re: PHP to retrieve specific data in MySQL

Posted: Sun Mar 20, 2011 5:14 pm
by Jonah Bron
viewtopic.php?f=6&t=128603

Please don't double-post.