php - Dynamic drop down search only returns one row when selecting books based on publisher -


before selection enter image description here

after selection

enter image description here

php code create dropdown list function getpublishers (){

$sql = "select distinct bookid, publisher book group publisher order publisher asc"; $rs = mysql_query($sql) or die(mysql_error()); $rows = mysql_fetch_assoc($rs); $tot_rows = mysql_num_rows($rs); if($tot_rows>0){     echo "<select name=\"srch_publisher\" id=\"srch_publisher\">\n";     echo "<option value=\"\">any publisher&hellip;</option>\n";     do{         echo "<option value=\"".$rows['bookid']."\"";         getsticky(2, 'srch_publisher', $rows['bookid']);         echo ">".$rows['publisher']."</option>";     } while($rows = mysql_fetch_assoc($rs));     echo "</select>"; } mysql_free_result($rs); 

}

php code carries out query based on selection

$sql =  "select distinct bk.title title, 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.bookid = $srch_publisher)");                 break;                 case 'srch_author':                     array_push($queries, "(bk_aut.author_id = $srch_author)");                 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"; } 

it shows 1 book publishers bloomsbury, see there 2 records in example. had same problem authors show 2 books had more 1 author. solved using group bk.title. problem similar. in matter.

you using $rows['bookid'] in publishers select value

echo "<option value=\"".$rows['bookid']."\"";     getsticky(2, 'srch_publisher', $rows['bookid']);     echo ">".$rows['publisher']."</option>"; 

and in query selecting book bk.bookid = $srch_publisher

case 'srch_publisher':      array_push($queries, "(bk.bookid = $srch_publisher)"); break; 

so selecting book bookid, not publisher


i believe want use publisher

echo "<option value=\"".$rows['publisher']."\"";     getsticky(2, 'srch_publisher', $rows['publisher']);     echo ">".$rows['publisher']."</option>"; 

and search publisher

case 'srch_publisher':      array_push($queries, "(bk.publisher = '$srch_publisher')"); break; 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -