mysql - PHP - Search results not paginating -
i'm trying search results paginate if there greater 10 items found in database. reason, though code recognises there more 10 items , creates links subsequent pages, search results listed on first page only. able please? code below:
for($i = 0; $i < $terms_count; $i++) { $search_terms_array[$i] = trim($search_terms_array[$i]); ${"query".$i} = $this->mysqli_link->query("select prod_id, prod_tags table prod_tags '%" . $search_terms_array[$i] . "%'"); if(${"query".$i}->num_rows < 1) { $zerocount++; } else { $rows = array(); while($row = ${"query".$i}->fetch_array()) { $rows[] = $row; } foreach($rows $row) { $search_id_results[] = $row['prod_id']; } } } if($zerocount == $terms_count) { echo $this->err_handle->fetch_error_text("search_terms_0_results"); return; } else { $search_results = array_values(array_unique($search_id_results)); $search_results_count = count($search_results); $search_page_count = ceil($search_results_count / 10); $search_page_first_result = ($search_page - 1) * 10; echo '<p>go page: '; for($i = 1; $i <= $search_page_count; $i++) { if($i == $search_page) { echo " <strong>" . $i . "</strong>"; } else { echo ' <a href="index.php?section=products&action=search&page=' . $i . '">' . $i . '</a>'; } } echo '</p><p> </p><p> </p>'; for($i = 0; $i < $search_results_count; $i++) { $query = $this->mysqli_link->query("select * table prod_id='" . $search_results[$i] . "' limit " . $search_page_first_result . ", 10"); while($row = $query->fetch_array()) { echo "<h4><a href=\"index.php?section=products&subsection=" . $row['prod_category'] . "&prodid=" . $row['prod_id'] . "\">" . $row['prod_name'] . "</h4></a><p><img src=\"includes/images/product_images/" . $row['prod_category'] . "/" . $row['prod_pic_filename'] . "\" alt=\"\" width=\"150\" height=\"200\" /></p><p>price: £" . $row['prod_price'] . "</p><p>" . $row['prod_code'] . "</p><input type=\"number\" name=\"prod_qty\" maxlength=\"2\" /><input type=\"submit\" name=\"add_to_basket\" value=\"add basket\" /></form></p><p> </p><p> </p>"; } } echo '<p>go page: '; for($i = 1; $i <= $search_page_count; $i++) { if($i == $search_page) { echo " <strong>" . $i . "</strong>"; } else { echo ' <a href="index.php?section=products&action=search&page=' . $i . '">' . $i . '</a>'; } } echo '</p><p> </p><p> </p>'; }
ok found solution problem , full function follows:
public function product_search($search_terms, $search_page, $search_flag_check) { if($search_flag_check == "invalid") { echo $this->err_handle->fetch_error_text("invalid_ns_term"); return; } if($search_terms == "") { echo $this->err_handle->fetch_error_text("no_search_string"); return; } $search_terms = htmlspecialchars($search_terms); $search_terms = $this->mysqli_link->real_escape_string(filter_var($search_terms, filter_sanitize_full_special_chars, filter_flag_no_encode_quotes)); $search_terms_array = explode(" ", $search_terms); $terms_count = count($search_terms_array); $zerocount = 0; $search_id_results = array(); for($i = 0; $i < $terms_count; $i++) { $search_terms_array[$i] = trim($search_terms_array[$i]); ${"query".$i} = $this->mysqli_link->query("select prod_id, prod_tags table prod_tags '%" . $search_terms_array[$i] . "%'"); if(${"query".$i}->num_rows < 1) { $zerocount++; } else { $rows = array(); while($row = ${"query".$i}->fetch_array()) { $rows[] = $row; } foreach($rows $row) { $search_id_results[] = $row['prod_id']; } } } if($zerocount == $terms_count) { echo $this->err_handle->fetch_error_text("search_terms_0_results"); return; } else { $search_results = array_values(array_unique($search_id_results)); $search_results_count = count($search_results); $search_page_count = ceil($search_results_count / 10); $search_page_first_result = ($search_page - 1) * 10; $search_page_results_limit = 10; if($search_page_first_result < 1) { $search_page_first_result = 1; } echo '<p>go page: '; for($i = 1; $i <= $search_page_count; $i++) { if($i == $search_page) { echo " <strong>" . $i . "</strong>"; } else { echo ' <a href="index.php?section=products&action=search&page=' . $i . '">' . $i . '</a>'; } } echo '</p><p> </p><p> </p>'; $search_page_upper_limit = $search_page_first_result + 9; if(array_key_exists($search_page_upper_limit, $search_results)) { $search_results_limit = $search_page_first_result + $search_page_results_limit; } else { end($search_results); $search_results_limit = key($search_results); reset($search_results); } for($i = $search_page_first_result; $i <= $search_results_limit; $i++) { $query2 = $this->mysqli_link->query("select * table prod_id='" . $search_results[$i] . "'"); $row = $query2->fetch_array(); echo "<h4><a href=\"index.php?section=products&subsection=" . $row['prod_category'] . "&prodid=" . $row['prod_id'] . "\">" . $row['prod_name'] . "</h4></a><p><img src=\"includes/images/product_images/" . $row['prod_category'] . "/" . $row['prod_pic_filename'] . "\" alt=\"\" width=\"150\" height=\"200\" /></p><p>price: £" . $row['prod_price'] . "</p><p>" . $row['prod_code'] . "</p><input type=\"number\" name=\"prod_qty\" maxlength=\"2\" /><input type=\"submit\" name=\"add_to_basket\" value=\"add basket\" /></form></p><p> </p><p> </p>"; } echo '<p>go page: '; for($i = 1; $i <= $search_page_count; $i++) { if($i == $search_page) { echo " <strong>" . $i . "</strong>"; } else { echo ' <a href="index.php?section=products&action=search&page=' . $i . '">' . $i . '</a>'; } } echo '</p><p> </p><p> </p>'; } } it took bit of thinking , give up, thought using array keys calculate limits each search page , after bit of googling on relevant php array functions fell place quite well. understand code may not tidy right , there may ways optimize/improve it, time being job.
Comments
Post a Comment