Snids
Snids

Reputation: 41

PHP- MySQL Search Criteria

I've developed a database having multiple fields-- approx 65 fields. I wish to give a multiple search criteria to my users,i.e. by selecting various field names in the drop down menu. I'm unable to execute it by using simple IF-ELSEIF- ELSE Statements. It gives results for only the first IF statement, while ignores the rest.

Following is the html page:

<form name="frm" action="search_plants.php" method="get">
            <table>
                <tr>
                    <td width="155" height="24">Select Scientific Name:</td>
      <td width="275">
                        <select name="spnm" size="1">       
                        <option value="0">  All</option>    
                        <option value="Abrus precatorius">  Abrus precatorius</option>  
                        <option value="Abutilon indicum">  Abutilon indicum</option>                    
                        <option value="Abutilon theophrasti"> Abutilon theophrasti</option>                 



                        </select>
                        </td>
              </tr>
                <tr>
                    <td>Select Family Name:</td>
                    <td>
                        <select name="fmnm" size="1">   
                        <option value="0">  All</option>
                        <option value="Fabaceae">Fabaceae</option>
                        <option value="Malvaceae">Malvaceae</option>
                        <option value="Mimosaceae">Mimosaceae</option>                      

                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Select Geographic Location:</td>
                    <td>
                        <select name="znm" size="1">                    
                        <option value="0">  All</option>
                        <option value="North Gujarat">North Gujarat</option>
                        <option value="South Gujarat">South Gujarat</option>
                        <option value="South East Gujarat">South East Gujarat</option>      
                        <option value="Central Gujarat">Central Gujarat</option>
                        <option value="Kachchh">Kachchh</option>
                        <option value="Saurashtra">Saurashtra</option>

                        </select>
                    </td>
                </tr>
                <tr>
                <tr>
                    <td>Select Marker:</td>
                    <td>
                        <select name="pnm" size="1">                    
                        <option value="0">  All</option>
                        <option value="rbcL">rbcL</option>      
                        <option value="psbA-trnH">psbA-trnH</option>    
                        <option value="matK">matK</option>  
                        <option value="rpoC1">rpoC1</option>    
                        <option value="ITS2">ITS2</option>  
                        <option value="ycf5">ycf5</option>      

                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="submit" value="Go" name="action">
                        <label>
                        <input type="reset" name="Reset" id="button" value="Reset">
                        </label>                        </td>
                </tr>
            </table>
        </form>

And the form action page is as follows, depicting only a part of the entire coding. I want to give the user an option of selecting any/all fields and be able to display it by selective search from the database. Please help me out.

if (($species == 0) || ($family == 0) || ($zones ==0 ) || ($marker == 0))   
    {
        if(($species==0) && ($family==0) && ($zones==0))
        {
            $sql="SELECT * FROM `ncbi_bold_ plants` WHERE Marker LIKE '$marker%'" ;
        }

        elseif($species==0 &&  $family==0 &&  $marker==0)
        {
        $sql="SELECT * FROM `ncbi_bold_ plants` WHERE Zones LIKE '%$zones%'";
        }

        elseif($family==0 &&  $zones==0 &&  $marker==0)
        {
        $sql="select * from `ncbi_bold_ plants` where Speciesname like '$species%'";

        }

        elseif($species==0 &&  $zones==0 &&  $marker==0)
        {
            $sql="select * from `ncbi_bold_ plants` where Family like '$family%'";

        }
    /*
    elseif($species==0 && $marker==0)
    {
        $sql="select * from `ncbi_bold_ plants` where Family like '$family%' AND Zones like '%$zones%'";
        $search_sql=mysql_query($sql) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$sql}");
    }       
    elseif($family==0 && $zones==0 && $marker==0)
    {
        $sql="select * from `ncbi_bold_ plants` where Speciesname like '$species%'";
        $search_sql=mysql_query($sql) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$sql}");
    }
    elseif($species==0 && $family==0)
    {
        $sql="select * from `ncbi_bold_ plants` where Zones like '%$zones%' AND marker like '$marker%'";
        $search_sql=mysql_query($sql) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$sql}");
    }
    elseif($species==0 && $zones==0)
    {
        $sql="select * from `ncbi_bold_ plants` where Family like '$family%' AND marker like '$marker%'";
        $search_sql=mysql_query($sql) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$sql}");
    }
    elseif($species==0 && $marker==0)
    {
        $sql="select * from `ncbi_bold_ plants` where Family like '$family%' AND Zones like '%$zones%'";
        $search_sql=mysql_query($sql) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$sql}");
    }
    */          
        else
        {
        $sql="SELECT * FROM `ncbi_bold_ plants` " ;
        }

    }

    else 
    {
        $sql="select * from `ncbi_bold_ plants` where Speciesname like '$species%' and Family like '$family%' and Zones like '%$zones%' and Marker like '$marker%'";


    }

    $search_sql=mysql_query($sql) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$sql}");

please help me for the same. I'm unable to find a flaw in the if statement.

Upvotes: 4

Views: 4898

Answers (4)

Wh1T3h4Ck5
Wh1T3h4Ck5

Reputation: 8509

Try this simple function:

function create_query($p_species, $p_family, $p_zones, $p_marker) {
  $options = array(
    'Speciesname' => $p_species,
    'Family' => $p_family,
    'Zones' => $p_zones,
    'Marker' => $p_marker
    );
  $cond = '';
  $noopt = true;
  foreach ($options as $column => $value) {
    if ($value !== 0) {
      $noopt = false;
      if ($cond != '') $cond .= ' AND ';
      $cond .= "$column LIKE '%$value%'";
      }
    }
  return $noopt ? false : "SELECT * FROM ncbi_bold_ plants WHERE $cond;";
  }

This function returns false if all parameters are zero, otherwise it returns query string.

Example #1:

$species = 'spectest';
$family = 0;
$zones = 'zonestest';
$marker = 0;

echo create_query($species, $family, $zones, $marker);

Output:

SELECT * FROM ncbi_bold_ plants WHERE Speciesname LIKE '%spectest%' AND Zones LIKE '%zonestest%';

Example #2:

$species = 0;
$family = 0;
$zones = 0;
$marker = 0;

var_dump(create_query($species, $family, $zones, $marker));

Output:

bool(false)

Or, if you want to return query string without conditions instead of false, just replace return line in the function with:

return $noopt ? "SELECT * FROM ncbi_bold_ plants;" : "SELECT * FROM ncbi_bold_ plants WHERE $cond;";


Now if you want to do this in your way with a lot of IF, ELSEIF and ELSE statements, there's solution:

Solution without function

(it works the same way like function above):

  if ($species == 0 && $family == 0 && $zones == 0 && $marker == 0) {
    $sql = "SELECT * FROM ncbi_bold_plants";
    }
  elseif ($species == 0 && $family == 0 && $zones == 0) {
    $sql = "SELECT * FROM ncbi_bold_plants WHERE Marker LIKE '$marker%'";
    }
  elseif ($species == 0 && $family == 0 && $marker == 0) {
    $sql = "SELECT * FROM ncbi_bold_plants WHERE Zones LIKE '$zones%'";
    }
  elseif ($species == 0 && $zones == 0 && $marker == 0) {
    $sql = "SELECT * FROM ncbi_bold_plants WHERE Family LIKE '$family%'";
    }
  elseif ($family == 0 && $zones == 0 && $marker == 0) {
    $sql = "SELECT * FROM ncbi_bold_plants WHERE Speciesname LIKE '$species%'";
    }
  elseif ($species == 0 && $family == 0) {
    $sql = "SELECT * FROM ncbi_bold_plants WHERE Zones LIKE '$zones%' AND Marker LIKE '$marker%'";
    }
  elseif ($species == 0 && $zones == 0) {
    $sql = "SELECT * FROM ncbi_bold_plants WHERE Family LIKE '$family%' AND Marker LIKE '$marker%'";
    }
  elseif ($species == 0 && $marker == 0) {
    $sql = "SELECT * FROM ncbi_bold_plants WHERE Family LIKE '$family%' AND Zones LIKE '$zones%'";
    }
  elseif ($family == 0 && $zones == 0) {
    $sql = "SELECT * FROM ncbi_bold_plants WHERE Speciesname LIKE '$species%' AND Marker LIKE '$marker%'";
    }
  elseif ($family == 0 && $marker == 0) {
    $sql = "SELECT * FROM ncbi_bold_plants WHERE Speciesname LIKE '$species%' AND Zones LIKE '$zones%'";
    }
  elseif ($zones == 0 && $marker == 0) {
    $sql = "SELECT * FROM ncbi_bold_plants WHERE Speciesname LIKE '$species%' AND Family LIKE '$family%'";
    }
  elseif ($species == 0) {
    $sql = "SELECT * FROM ncbi_bold_plants WHERE Family LIKE '$family%' AND Zones LIKE '$zones%' AND Marker LIKE '$marker%'";
    }
  elseif ($family == 0) {
    $sql = "SELECT * FROM ncbi_bold_plants WHERE Speciesname LIKE '$species%' AND Zones LIKE '$zones%' AND Marker LIKE '$marker%'";
    }
  elseif ($zones == 0) {
    $sql = "SELECT * FROM ncbi_bold_plants WHERE Speciesname LIKE '$species%' AND Family LIKE '$family%' AND Marker LIKE '$marker%'";
    }
  elseif ($marker == 0) {
    $sql = "SELECT * FROM ncbi_bold_plants WHERE Speciesname LIKE '$species%' AND Family LIKE '$family%' AND Zones LIKE '$zones%'";
    }
  else {
    $sql = "SELECT * FROM ncbi_bold_plants WHERE Speciesname LIKE '$species%' AND Family LIKE '$family%' AND Zones LIKE '$zones%' AND Marker LIKE '$marker%'";
    }

  // here you have your $sql ... now do whatever you want with your query
  echo $sql;

This code has been tested with every possible situation so if it still doesn't work, your problem is not in PHP example you posted above. I don't recommend this way because this is totally wrong approach, even for beginners.

If you still want to make it with IF/ELSE/ELSEIF consider condition(s) checking IF variable IS NOT EQUAL to zero. You'll get less code and easier implementation. Something like this:

Different aproach

$species = 0;
$family = 0;
$zones = 0;
$marker = 0;

// default query w/out conditions
$sql = "SELECT * FROM ncbi_bold_plants";

// conditions array
$conditions = array();

// adding condition to array for every parameter <> 0
if ($species != 0) $conditions[] = "Speciesname LIKE '$species%'";
if ($family != 0) $conditions[] = "Family LIKE '$family%'";
if ($zones != 0) $conditions[] = "Zones LIKE '$zones%'";
if ($marker != 0) $conditions[] = "Marker LIKE '$marker%'";

// all we need now is to concatenate array elements with " AND " glue
$sql_cond = join(" AND ", $conditions);

// last thing, adding condition(s) to the main query (if there's any)
if ($sql_cond != '') $sql .= " WHERE $sql_cond";

// let see what we have now
echo $sql;

First 4 lines are for testing only. Now, we have same solution like previous ones but with less code (8 lines only).

Hope this helps.

Upvotes: 3

Saeed M.
Saeed M.

Reputation: 2361

you can do like this code.first customize for yourself :

<?php
$where = '';
if(isset($_POST))
{
    foreach($_POST as $k=>$v)
    {
        $q[] = $k.' LIKE \'%'.$v.'%\'';
    }
    $where = implode(' AND ',$q);
}
if(!empty($where))
    $sql = 'SELECT * FROM `ncbi_bold_ plants` WHERE '.$where;
else
    $sql = 'SELECT * FROM `ncbi_bold_ plants`"; 
    ......
    ......

Upvotes: 0

Sumit Neema
Sumit Neema

Reputation: 480

try using following approach

if($species)
{
  $colum='Species';
  $val=$family;
}
else
if($family)
{
  $colum='Family';
  $val=$family;
}
else
if($zones)
{
  $colum='Zones';
  $val=$zones;
}
else
if($marker)
{
  $colum='Marker';
  $val=$marker;
}
else
{
$column='';
$val='';

}

$sql="SELECT * FROM `ncbi_bold_ plants` WHERE $column LIKE '%".$val."%'" ;

Upvotes: 0

hjpotter92
hjpotter92

Reputation: 80639

Why not create dynamic dropdown menu from the fields? For eg. (I am using this on my own page, you'll have to edit the SQL syntax/query accordingly

echo "\n\t<select name='cat'>";
echo "\n\t\t<option value=''>Search all...</option>";
$connect = mysql_connect( <connection settings> );
$ctgTableCmd = "SELECT name FROM offliner.ctgtable ORDER BY name ASC";
$getCtg = mysql_query( $ctgTableCmd );
while( $ctgTable = mysql_fetch_array( $getCtg ) )
{
    $ctgVal = $ctgTable[0];
    echo "\n\t\t<option value=$ctgTable[0]>" . ucfirst( $ctgTable[0] ) . "</option>";
}

Upvotes: 1

Related Questions