SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Creating a sql query to show an option selected on form

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating a sql query to show an option selected on form

    Apologies on the vauge title. I have a form - http://www.outcomesforchildren.org/f...port-form.html

    The first select drop down has a list of services (named 'services'). I'm trying to write a query that allows the option selected to throw back the results from the DB that are of that service type.

    The DB has each option (service) listed as a INT field - e,g 'Carer' is an INT field in the DB 1 = Yes its a Carer 2 = Not this service.

    I have no options on the backend and can only adjust the query I currently have to display the results which is;

    PHP Code:

    <?php
    $org_name
    =$_POST['org_name'];
    $area=$_POST['area'];
    //$services=$_POST['services'];
    $hardiker=$_POST['hardiker'];


     
    //organisation name feedback
    if (isset ($org_name))
    {
    echo 
    '<h3>Organisation Name: <span style="color: #000; font-weight:bold">'.$org_name.'</span></h3>';
    }
    else
    {
    echo 
    '<h3>You have searched for all organisation names.</h3>';
    }
        
    /*service type feedback
    if (isset ($services))
    {
    echo '<h3>Services: <span style="color: #000; font-weight:bold">'.$services.'</span></h3>';
    }
    else
    {
    echo '<h3 >You have searched for all services.</h3>';
    }*/
        //area selected feedback
    if (isset ($area))
    {
    echo 
    '<h3>Area: <span style="color: #000; font-weight:bold">'.$area.'</span></h3>';
    }
    else
    {
    echo 
    '<h3>You have searched all areas.</h3>';
    }
        
    //hardiker feedback
    if (isset ($hardiker))
    {
    echo 
    '<h3>Hardiker: <span style="color: #000; font-weight:bold">'.$hardiker.'</span></h3>';
    }
    else
    {
    echo 
    '<h3>You have searched all hardiker levels.</h3>';
    }

    echo 
    $services;

    //mysql query here

    $sql_result"SELECT * FROM family_support WHERE org_name LIKE '%$org_name%'";

    if (
    $area != 'All') {
    $sql_result .=" AND area LIKE '%$area%'";
    }

    if (
    $services != 'All') {
    $sql_result .=" AND services LIKE '%$services%'";
    }

    if (
    $hardiker != 'All') {
    $sql_result .=" AND hardiker = '$hardiker'";
    }

    //echo result and order by name
    $sql_result .=" ORDER BY org_name";

    //echo $sql_result;
    $result =  mysql_query($sql_result$dbh) or die ('Database currently being updated, please try again later!'); 
    $num=mysql_numrows($result);

    //echo $sql_result;

    //get the count 
    echo '<h3><span style="color: #000; font-weight:bold">Total search results: </span>'.$num.'</h3><br/>';

    if (
    $num 0)
    {

    ?>
    I desperaty need help in how I can add to the query above to allow the option selected displayed along with the other if statements. Many thanks in advance!

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    70
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    There are a couple of things you could do.

    First you can modify the select dropdown to use the int value of the service as the value. This will place the int value in the $_POST array, which should make it easier to construct your query.
    HTML Code:
    <option value="1">Carer</option>
    Secondly I don't know how you have the DB set up, but it may be good to create a second table for services that will relate the int value to the actual name. This will allow you to do a join on the tables with the query. I'm not sure what your plans are long term for this form but having separate tables to hold all the information would make it much more flexible if you need to make changes or additions in the future.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the delay - I cant change the DB as its set up to be populated from a word form already sent out!!!!

    The drop down options are in the DB as integers.

    All I want is the query to pick up the drop down field?

Tags for this Thread

Bookmarks

Posting Permissions

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