PHP to retrieve specific data in MySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
abdorito
Forum Newbie
Posts: 1
Joined: Sun Mar 20, 2011 4:38 pm

PHP to retrieve specific data in MySQL

Post 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>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP to retrieve specific data in MySQL

Post by Jonah Bron »

viewtopic.php?f=6&t=128603

Please don't double-post.
Post Reply