Hi Guys
I would like to integrate the FUNCTION prev123Next() listed below in to this one
function search_results() I'm still quite new to php and could really do with a hand.
What would be the best way of doing this?
//#####################################################################
function search_results($search)
{
$searchtext = htmlentities($search);
$search = stripslashes($search);
$links_sql = "SELECT * FROM sslinks WHERE link_validated = 'yes' AND
(link_name LIKE '%$search%' OR link_desc LIKE '%$search%')
ORDER BY link_name";
$cats_sql = "SELECT lcat_id, lcat_name, lcat_ranking FROM sslinkcats WHERE
(lcat_name LIKE '%$search%' OR lcat_header LIKE '%$search%')
ORDER BY lcat_ranking DESC, lcat_name";
$links = links_sql($links_sql);
echo html_header("Search Results");
$cats = cats_sql($cats_sql);
if (!$links)
{
$links = ss_template('search_no_links_found.tmpl');
}
echo ss_template('search_results.tmpl', array('%cats%' => $cats, '%links%' => $links, '%searchtext%' => $searchtext));
echo html_footer();
exit;
}
//################################ PREV 123() ########################
<?php
/*
FUNCTION prev123Next()
array prev123Next (string sql_stmt, int limit)
Function prev123Next() takes in an SQL SELECT query and returns an array
of links (already embedded in anchor tags), based on how many rows were returned by the query.
The main logic for this function is based on code written by Rod Kriesler in his article at
http://www.phpbuilder.com/columns/rod20000221.php3
Here's one way to use this function:
Set up the SQL Statement and the limit variables (limit variable represents how many records you want
per page);
like:
$sql = "SELECT snap, crackle, pop FROM rice_crispies ORDER BY whatever you like";
$resultsPerPage = 10; // number of rows to return per page
// set up a function to print out the array of links
function printArray($element){
echo "$element";
// Call the prev123Next function and assign the return value to a variable
$prev_next_links = prev123Next($sql, $resultsPerPage);
// Now you can print that out anywhere on the page by using PHP's array_walk() function,
// passing it the $links array and the printArray() fuction, like:
array_walk($links, 'printArray');
*/
function prev123Next($sql_stmt, $limit){
// set the $offset variable as global
global $PHP_SELF;
global $offset;
// Execute the query
$numresults = mysql_query($sql_stmt);
// Determine the total number of rows returned.
$numrows = mysql_num_rows($numresults);
// Make sure that the number of rows returned is greater than zero
if($numrows >= 1){
// determine if offset has been passed to script, or if offset has been tampered with.
if (empty($offset) || ($offset < 0) || ($offset > $numrows)) {
$offset=0;
}
// Determine if a "PREV" link is necessary - if so, add it to the links array
if (($offset > 0) && ($offset <= $numrows)) {
$prevoffset = $offset - $limit;
$link_array[] = "<a href=\"$PHP_SELF?offset=$prevoffset\">PREV</a> \n";
}
// Determine the total number of pages needing links
$pages=intval($numrows/$limit);
// $pages variable now contains integer number of pages needed, unless there is a remainder from division
if ($numrows % $limit) {
// There is a remainder, so add one page
$pages++;
}
for ($i=1; $i<=$pages; $i++) { // loop thru
$newoffset=$limit*($i-1);
$link_array[] = "<a href=\"$PHP_SELF?offset=$newoffset\">$i</a> \n";
}
// Determine if this is the last page.
if (!(($offset/$limit)==$pages) && $pages!=1) {
$newoffset=$offset+$limit;
// if not last page give NEXT link
if((($numrows - $offset) > $limit) && ($pages !=1) && ($offset < $numrows)){
$link_array[] = "<a href=\"$PHP_SELF?offset=$newoffset\">NEXT</a><p>\n";
}
}
}else{
; // redirect to error page
}
// return the array containing all of the links.
return $link_array;
}// end function prev123next
?>
//###################################### END ##########################