Check which text box and select forms have values and get results accordingly from mysql

Discussion in 'PHP' started by Evaldas, Jun 27, 2013.

  1. #1
    Hello,
    Okey I am stuck a bit on one thing:

    <form action="index.php" method="post">
    Choose city: <input id="tags" type="text" name="select1">
    <br />
    Select County
    <select id="select2" name="select2">
    <option value="x" selected="selected1">Select County</option>
    <option value="Derbyshire">Derbyshire</option>
    <option value="Londonshire">Londonshire</option>
    <option value="Birminghamshire">Birminghamshire</option>
    </select>
    <br />

    Choose style:
    <select id="select3" name="select3">
    <option value="Muy thai">Muy thai</option>
    <option value="Boxing">Boxing</option>
    </select><hr />
    <input type="submit" value="Go"/>
    </form>

    It is a small form containing one text box and two selections. Now with PHP help I need to check
    the following things -
    1) If text box contains value AND another two selections don't, then get results from mysql by Cities.
    2) If text box has value AND second selection has the value but the third does not, then get results from mysql accordingly.
    3) If text box has value AND third selection as well, but second does not, then ....

    So is there a way to make it simple? Without lots of IF's because I was trying to make it work, but I could not. It's confusing a bit. There is my php:

    <?php
    mysql_connect("localhost","root","") or die("Could not connect to database");
    mysql_select_db("test");

    $joint = '';
    $output = '';
    $counter = '';
    //collect
    if(isset($_POST['select1'])){

    $search1 = $_POST['select1'];
    $search2 = $_POST['select2'];
    $search3 = $_POST['select3'];

    //First text box for cities
    if ($search1 != '')
    {
    $joint = $joint + "City = '$search1' ";
    $counter = $counter + 1;
    }else
    {
    $joint = '';
    $counter = $counter;
    }
    //Second item ----- checking selection box
    if ($search2 != 'Select Country' AND $counter != '')
    {
    $joint = $joint + " AND County = '$search2' ";
    $counter = $counter + 1;
    }else if($search2 != 'Select Country' AND $counter == ''){

    $joint = $joint + "County = '$search2' ";
    $counter = $counter + 1;
    }else if($search2 == 'Select Country' AND $counter == ''){
    $joint = '';
    $counter = '';
    }

    $query = mysql_query("SELECT * FROM cities WHERE" .$joint) or die("could not search");

    $count = mysql_num_rows($query);
    if($count == 0){
    $output='There was no results';
    }else{
    while($row = mysql_fetch_array($query)){
    $county = $row['County'];
    $city = $row['City'];
    $style = $row['Style'];
    $output.='<div>'.$county.' '.$city.' '.$style.'</div>';
    }
    }
    }

    ?>

    This is the way I was trying to do it, now I deleted some of the code (IF's and etc.) So the question would be - is there any easier and more efficient way to do this thing right? And if possible, could you give me links with examples for this thing, because I could not find it anywhere..

    Thanks in advance!
     
    Evaldas, Jun 27, 2013 IP
  2. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #2
    Unless you'll be able to describe this in a manner you feel is easier to understand, describing it in PHP won't be any different.
     
    ActiveFrost, Jun 27, 2013 IP
  3. aidanriley629

    aidanriley629 Banned

    Messages:
    429
    Likes Received:
    23
    Best Answers:
    3
    Trophy Points:
    175
    #3
    This code gives me a headache, I don't really get it. Where is the textbox? Send me a PM and I'll try to help you on Skype or via e-mail if you'd like.
     
    aidanriley629, Jun 30, 2013 IP
  4. Evaldas

    Evaldas Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #4
    Thanks for answers! I figured out how to do it (and this is my php search if anyone would be in the same position):

    <?php
    mysql_connect("localhost","root","") or die("Could not connect to database");
    mysql_select_db("test");

    $output = '';
    $string = '';
    //collect
    if(isset($_POST['select1'])){

    $search1 = $_POST['select1'];
    $search2 = $_POST['select2'];
    $search3 = $_POST['select3'];

    $search1 = stripslashes($search1);
    $search1 = mysql_real_escape_string($search1);

    //First entry
    if($search1 != ''){
    $string = $string . " WHERE City = '$search1'";
    echo "$string";
    }else{
    $string = '';
    echo "empty";
    }

    //Second entry
    if($string != ''){
    if($search2 != 'All Counties'){$string = $string . " AND County = '$search2'";}
    }else{
    if($search2 != 'All Counties'){$string = $string . " WHERE County = '$search2'";}
    }

    //Third entry
    if($string != ''){
    if($search3 != 'All Styles'){$string = $string . " AND Style = '$search3'";}
    }else{
    if($search3 != 'All Styles'){$string = $string . " WHERE Style = '$search3'";}
    }


    $query = mysql_query("SELECT * FROM cities".$string) or die("could not search");

    $count = mysql_num_rows($query);
    if($count == 0){
    $output='There was no results';
    }else{
    while($row = mysql_fetch_array($query)){
    $county = $row['County'];
    $city = $row['City'];
    $style = $row['Style'];
    $output.='<div>'.$county.' '.$city.' '.$style.'</div>';
    }
    }
    }

    ?>
     
    Evaldas, Jun 30, 2013 IP