Search Pagination Support
This modification replaces the default search function with one that adds pagination support. Although it does not use 'Full Text' search, it loads quickly regardless of the amount of jobs in the database.
I've included two search methods, you can choose and test both at the config.php. Both have pagination. They are:
1. Classic JB Search: The default of JB 1.7. I don't think it's very user-friendly, as it only seems to work good when searching in the format of "keyword1 keyword2" or "keyword1, cityname".
2. Chronos Modified Search: Keywords are seperated by spaces, each of them must be in either the title, cityname or description. There are likely more results than the classic search, but a user can give as many keywords as he likes.
Modification Features:
- Adds pagination support to the search function
- Two search methods are included
- Supports large databases (Full Text may follow if needed)
- Allows user to go back and forth from search pages (when in deeper pages)
1. Open "config.php":
Find:
define('JOBS_PER_PAGE', 50);
Add below:
// Search settings definitions define('SEARCH_METHOD', '2'); //Defines what search type to use -> 1=classic 2=chronos define('SEARCH_RESULTS_PER_PAGE', '10'); //Sets the amount of search results per page define('SEARCH_AMOUNT_PAGES', '8'); //Max. pages to display (must be an even number!)
2. Open "_includes/class.Job.php":
Find:
public function Search($keywords)
Replace entire function with:
public function Search($keywords, $url_query, $start_page = 1) { global $db; $jobs = array(); $conditions = ''; $_SESSION['keywords_array'] = array(); if (SEARCH_METHOD == 1) { $kw1 = $kw2 = $extra_conditions = ''; if (strstr($keywords, ',') || strstr($keywords, ', ')) { $tmp = explode(',', $keywords); $kw1 = trim($tmp[0]); $kw2 = trim($tmp[1]); if ($kw1 == '') { $kw1 = $kw2; $kw2 = ''; } } else if (strstr($keywords, ' ') || strstr($keywords, ' ')) { // filter out empty strings (can happen if there are many whitespaces between two words in the search string) $tmp = array_filter(explode(' ', $keywords)); foreach ($tmp as $word) { // try to find city based on city_id $sql = 'SELECT id FROM cities WHERE name LIKE "%' . $word . '%"'; $result = $db->query($sql); $row = $result->fetch_assoc(); if ($row['id'] != '') { if ($found_city) { $conditions .= ' OR'; } $conditions .= ' city_id = ' . $row['id']; $found_city = true; $keywords = trim(str_replace($word, '', $keywords)); } // try to find city based on postcode or location_details $sql = 'SELECT id FROM jobs WHERE outside_location LIKE "%' . $word . '%"'; $results = $db->QueryArray($sql); if ($db->affected_rows > 0) { if ($found_city) { $conditions .= ' OR '; } $conditions .= ' id IN ('; foreach ($results as $j) { $conditions .= $j['id'] . ','; $found_city = true; } $conditions = rtrim($conditions, ','); $conditions .= ') '; $keywords = trim(str_replace($word, '', $keywords)); } } if ($found_city) { $conditions .= ' AND (title LIKE "%' . $keywords . '%" OR company LIKE "%' . $keywords . '%"' . ' OR description LIKE "%' . $keywords . '%")'; } } if (!$found_city) { if ($kw1 != '') { $conditions .= ' (title LIKE "%' . $kw1 . '%" OR description LIKE "%' . $kw1 . '%")'; $_SESSION['keywords_array'][] = $kw1; } if ($kw2 != '') { $sql = 'SELECT id FROM cities WHERE name LIKE "%' . $kw2 . '%"'; $result = $db->query($sql); $row = $result->fetch_assoc(); if ($row['id'] != '') { $extra_conditions .= ' OR city_id = ' . $row['id']; } $conditions .= ' AND (outside_location LIKE "%' . $kw2 . '%" ' . $extra_conditions . ')'; $_SESSION['keywords_array'][] = $kw2; } if ($kw1 == '' && $kw2 == '') { $sql = 'SELECT id FROM cities WHERE name LIKE "%' . $keywords . '%"'; $result = $db->query($sql); $row = $result->fetch_assoc(); if ($row['id'] != '') { $extra_conditions .= ' OR city_id = ' . $row['id']; } $conditions = 'title LIKE "%' . $keywords . '%" OR company LIKE "%' . $keywords . '%"' . ' OR description LIKE "%' . $keywords . '%" OR outside_location LIKE "%' . $keywords . '%"' . $extra_conditions; $_SESSION['keywords_array'][] = $keywords; } } $sql = 'SELECT id FROM jobs WHERE is_temp = 0 AND is_active = 1 AND (' . $conditions . ') ORDER BY created_on DESC'; $result = $db->query($sql); } else { $cities = array(); $check_cities = ''; $keywords = str_replace(","," ", $keywords); $keywords = str_replace(" "," ", $keywords); $keywords = rtrim($keywords); $keywords_a = preg_split( "/[\s,]*\\'([^\\\"]+)\\'[\s,]*|[\s,]+/", $keywords, 0, PREG_SPLIT_DELIM_CAPTURE ); function array_trim($a) { $j = 0; for ($i = 0; $i < count($a); $i++) { if ($a[$i] != "") { $b[$j++] = $a[$i]; } } return $b; } $keywords_r = array_trim($keywords_a); //Search in Cities for ($i=0; $i < count($keywords_r); $i++) { $sql = 'SELECT id FROM cities WHERE name LIKE "%'. $keywords_r[$i] .'%" ORDER BY ID ASC'; $result = $db->query($sql); $cities_line = ''; while ($row = $result->fetch_assoc()) { $cities_line .= $row['id'].' '; } $cities[$i] = $cities_line; } //Search in Jobs for ($i=0; $i < count($keywords_r); $i++) { if ($cities[$i] != "") { $cities[$i] = rtrim($cities[$i]); $cities_r = explode(' ', $cities[$i]); for ($a=0; $a < count($cities_r); $a++) { $check_cities .= 'OR city_id = "'.$cities_r[$a].'" '; } } $conditions .= 'AND (title LIKE "%' . $keywords_r[$i] . '%" OR description LIKE "%' . $keywords_r[$i] . '%" OR outside_location LIKE "%' . $keywords_r[$i] . '%" '.$check_cities.' ) '; } $sql = 'SELECT id FROM jobs WHERE is_temp = 0 AND is_active = 1 '. $conditions .' ORDER BY created_on DESC'; $result = $db->query($sql); } $pages = ''; $id_array = ''; $max_loop = SEARCH_RESULTS_PER_PAGE; $max_visible_pages = SEARCH_AMOUNT_PAGES; while ($row = $result->fetch_assoc()) $id_array[] = $row['id']; $start_count = (($start_page - 1) * $max_loop) ; $current_loop = 0; $total_results = count($id_array); $total_loop = ($total_results ) - $start_count; $total_pages = ceil($total_results / $max_loop); if ($total_pages > 1) { $pagination_loop = $start_page - ($max_visible_pages / 2); if ($pagination_loop < 1) $pagination_loop = 1; elseif (($pagination_loop - 1) > 0) $pages .= " <a href='".BASE_URL."search/".$url_query."/?p=".($pagination_loop - 1)."'>«</a> "; $pagination_top = $pagination_loop + $max_visible_pages + 1; while (($pagination_loop < ($total_pages+1)) && ($pagination_loop < $pagination_top)) { if ($pagination_loop == $start_page) $pages .= " <a class='current_page' href='".BASE_URL."search/".$url_query."/?p=$pagination_loop'>$pagination_loop</a> "; else $pages .= " <a href='".BASE_URL."search/".$url_query."/?p=$pagination_loop'>$pagination_loop</a> "; $pagination_loop++; } if ($pagination_loop == $pagination_top) $pages .= " <a href='".BASE_URL."search/".$url_query."/?p=".($pagination_loop)."'>»</a> "; } if ($id_array != '') { while (($current_loop < $total_loop) && ($current_loop < ($max_loop ))) { $current_job = new Job($id_array[$start_count]); $jobs[] = $current_job->GetBasicInfo(); $current_loop++; $start_count++; } } $_SESSION['search_results'] = $jobs; $_SESSION['search_pagination'] = $pages; return $jobs; }
3. Open "page_search.php":
Find:
if ($id != '') { $id = urldecode($id); }
Replace with:
if (isset($_GET['p'])) $start_page = $_GET['p']; else $start_page = 1; if ($id != '') { $tmp = explode('|', $id); if (isset($tmp[1])) $url_query = trim($tmp[1]); else $url_query = $id; $id = urldecode($id); }
Find:
$requestKeywords = str_replace('"', '', urldecode($_POST['keywords']));
Add below:
$url_query = trim($_POST['keywords']);
Find:
$smarty->assign('jobs', $job->Search($keywords));
Replace with:
$smarty->assign('jobs', $job->Search($keywords, $url_query, $start_page));
Find:
else { $smarty->assign('keywords', stripslashes($keywords)); $template = 'posts-loop.tpl'; }
Add below:
if (isset($_SESSION['search_pagination'])) { $smarty->assign('pages', $_SESSION['search_pagination']); }






August 7th, 2009 - 10:59
Hi, great site, just wondering what you think about the built-in pagination from jobberbase. How come you didn’t use that one to implement the pagination here?
August 7th, 2009 - 20:26
Nice integration which is looking good. Gonna try it out soon! Keep it up.