mysql - PHP BETWEEN Clause usage -


php

i have having problem case, statements. trying search books between 2 years having trouble can search 1 year using code trying 2 not working. understand more going wrong way desired result appreciated. getting error notice: undefined variable: year1 else part of last case. thanks.

if year , year1 have value should bettwen 2 years if year has value find books in year.

<?php include 'header.php'; include 'searchscript.php';  $sql =  "select distinct bk.title title, bk.bookid bookid, bk.year year, bk.publisher publisher, aut.authorname author           book bk            join book_category bk_cat           on bk_cat.book_id = bk.bookid           join categories cat           on cat.id = bk_cat.category_id           join books_authors bk_aut           on bk_aut.book_id = bk.bookid           join authors aut          on aut.id = bk_aut.author_id";  if(isset($_get['searchinput'])){ $input = $_get['searchinput']; $input = preg_replace('/[^a-za-z0-9]/', '', $input); } if (isset($input)){      $getters = array();     $queries = array();      foreach ($_get $key => $value) {         $temp = is_array($value) ? $value : trim($value);         if (!empty($temp)){         if (!in_array($key, $getters)){             $getters[$key] = $value;             }         }     }      if (!empty($getters)) {          foreach($getters $key => $value){             ${$key} = $value;             switch ($key) {                 case 'searchinput':                     array_push($queries,"(bk.title '%$searchinput%'                      || bk.description '%$searchinput%' || bk.isbn '%$searchinput%'                      || bk.keywords '%$searchinput%' || aut.authorname '%$searchinput%')");                 break;                 case 'srch_publisher':                     array_push($queries, "(bk.publisher = '$srch_publisher')");                 break;                 case 'srch_author':                     array_push($queries, "(bk_aut.author_id = '$srch_author')");                 break;                 case 'srch_category':                     array_push($queries, "(bk_cat.category_id = '$srch_category')");                 break;                 **case 'year' && 'year1':                        if("$year1" ==""){                         array_push($queries, "(bk.year = '$year')");                     } else {                         array_push($queries, "(bk.year between '$year' , '$year1')");                     }                 break;**         }     } }  if(!empty($queries)){     $sql .= " ";     $i = 1;     foreach ($queries $query) {         if($i < count($queries)){             $sql .= $query." , ";         } else {             $sql .= $query;         }            $i++;     } } $sql .= " group bk.title order bk.title asc";  }else{     $sql .= " group bk.title order bk.title asc"; }   $rs = mysql_query($sql) or die(mysql_error()); $rows = mysql_fetch_assoc($rs); $tot_rows = mysql_num_rows($rs); ?> 

your code:

foreach($getters $key => $value)     switch ($key) {         case 'year' && 'year1':             if("$year1" ==""){                 array_push($queries, "(bk.year = '$year')");             } else {                 array_push($queries, "(bk.year between '$year' , '$year1')");             }         break;     } } 

shows 2 issues:

  1. case statements don't work way. can't use boolean operators same way here when using if() statement. (see manual)
  2. you cannot expect iterator variable $key in foreach($getters $key=>$value) hold both values @ same time, imply saying 'year' && 'year1'!

to solve issues, like:

foreach($getters $key => $value)     switch ($key) {         case 'year':             if($getters["year1"] ==""){                 array_push($queries, "(bk.year = '{$value}')");             } else {                 array_push($queries, "(bk.year between '{$value}' , '{$getters['year1']}')");             }         break;     } } 

in case block executed when foreach($getters) hits key 'year'. if statement handles 'year1' correctly accessing value in array directly instead of looking @ iterator variables.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -