www.webdeveloper.com
Results 1 to 4 of 4

Thread: pull data from table using multiple drop down menu

  1. #1
    Join Date
    Apr 2010
    Posts
    13

    pull data from table using multiple drop down menu

    Hello, I'm building a website that has 2 drop down menu, Category menu and Subcat menu, so that user can display data according to the 2 menu. On the mysql table there is a column call Category and SubCat. I want php to pull row from database according to what user select on the menus. in another word menu1 equals menu2. lets say Category has suv, pickup, sedan, hatchback, etc. SubCat has Nissan, Toyota, Ford, Benz, etc.
    Not sure if I code it right. Any suggestion appreciated.
    PHP Code:
    <?php
    //insert code to connect to the database 
    $connection mysql_connect ("localhost","root") or die (mysql_error());     
                  
    mysql_select_db ("nindu") or die (mysql_error());

    //get the form data 
    $Category $_POST["Category"] ;  
    $SubCat $_POST["SubCat"] ; 

    //build the appropriate query 
    if($Category == $SubCat) { 
        
    $query "select * from jobs WHERE Category ='$Category'";
        
    $query "select * from jobs WHERE SubCat ='$SubCat'";
    } else { 
        echo 
    "No Record Found";


    //run the query 
    $result mysql_query($query$connection) or die("Unable to retrieve jobs - server is busy");
     
    //output the results 
        
    while($row mysql_fetch_assoc($result)) {
    ?>
    Got this when I run the code: Notice: Undefined variable: query in C:\

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    Unless $Category == $SubCat then $query will be undefined. And when the 2 are equal, you are overwriting the first $query with the secon $query.

  3. #3
    Join Date
    Apr 2010
    Posts
    13
    hehe I got it figured out, thanks tirna. now got problem with pagination

  4. #4
    Join Date
    Jun 2012
    Posts
    1
    hello! i am php newbie! i am working on a site that pulls data from the database using a drop down menu. everything else worked except it does not show result from my database

    here is the code:

    HTML Code:
    <form action="job.php" method="POST">
    <select name="sector">
    <option value="&lt;Demo Sector, Please Ignore III&gt;">&lt;Demo Sector, Please Ignore III&gt</option>
    <option value="Agriculture, Fisheries &amp; Animal Care">Agriculture, Fisheries &amp; Animal Care</option>
    <option value="Built Environment">Built Environment</option>
    <option value="Creative Arts and Culture">Creative Arts and Culture</option>
    <option value="Education">Education</option>
    <option value="Financial Services">Financial Services</option>
    <option value="General Commerce">General Commerce</option>
    <option value="Health, Safety &amp; Social Care">Health, Safety &amp; Social Care</option>
    <option value="Hospitality, Tourism &amp; Sport">Hospitality, Tourism &amp; Sport</option>
    <option value="Information &amp; Communications Technology">Information &amp; Communications Technology</option>
    <option value="Manufacturing">Manufacturing</option>
    <option value="Media and Communications">Media and Communications</option>
    <option value="Mining &amp; Steel">Mining &amp; Steel</option>
    <option value="NGOs and Developmental Organisations">NGOs and Developmental Organisations</option>
    <option value="Oil &amp; Gas">Oil &amp; Gas</option>
    <option value="Power &amp; Utilities">Power &amp; Utilities</option>
    <option value="Professional/Advisory Services">Professional/Advisory Services</option>
    <option value="Public Sector/Government">Public Sector/Government</option>
    <option value="Transportation &amp; Logistics">Transportation &amp; Logistics</option>
    </select>
    <input type="Submit" value="Submit" name="Submit">
    </form>


    PHP Code:
    <table width="100%" border="1">
    <tr align="center" bgcolor="#CCCCCC">
    <td width="18%"><strong>title</strong></td>
    <td width="65%"><strong>description</strong></td>

    <?php

    mysql_connect ("localhost","root","") or die (mysql_error());
    mysql_select_db ("whatnext") or die (mysql_error());

    //some basic input data validation

    //get the form data
    $sector = $_POST["sector"];
    //build the appropriate query
    if($sector == "All") {
    $query = 'select * from sector';
    } else {
    $query = 'select * from sector where title = "'.$sector.'"';
    }
    $query = mysql_real_escape_string($query);

    //then run the query

    {
    echo "<tr align=\"center\">
    <td>$query[title]</td>
    <td>$query[description]</td>
    </tr>";
    }
    ?>
    </table>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles