Hello, i am relatively new to PHP and i am struggling with printing multiple search results on to different pages. The code below works ok but when you click on next page button, it brings up a blank screen. I think it might have something to do with resetting the $found variable. Can you have a quick look? any suggestions would be greatly appreciated.
<?
if ($_GET) $vars=$_GET;
else $vars=$_POST;
$found = $vars['search'];
if (!($limit)){
$limit = 10;} // Default results per-page.
if (!($page)){
$page = 0;} // Default page value.
if ($found == "")
{
echo "<p>You forgot to enter a search term<br>";
exit;
}
$username="**** ";
$password="**** **";
$database="**** **";
$host="******** *********";
mysql_connect($ host,$username, $password);
@mysql_select_d b($database) or die( "Unable to select database");
//convert to uppercase to allow for case sensisivities
$found = strtoupper($fou nd);
//strip code from search term
$found = strip_tags($fou nd);
//strip spaces from search term
$found = trim ($found);
$check = mysql_query("SE LECT * FROM song WHERE upper(descripti on) LIKE '%$found%'");
$anymatches=mys ql_num_rows($ch eck);
if ($anymatches == 0)
{
echo "Sorry, no matching results were found<br><br><b r>";
}
$pages = intval($anymatc hes/$limit); // Number of results pages.
// $pages now contains int of pages, unless there is a remainder from division.
if ($anymatches % $limit) {
$pages++;} // has remainder so add one page
$current = ($page/$limit) + 1; // Current page number.
if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.
else {
$total = $pages;} // Else total pages is $pages value.
$first = $page + 1; // The first result.
if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.
else{
$last = $anymatches;} // If last results page, last result equals total number of results
echo "Results <b>$first</b> - <b>$last</b> of <b>$anymatche s</b><br><br>";
$search = mysql_query("SE LECT * FROM song WHERE upper(descripti on) LIKE '%$found%' LIMIT $page, $limit");
while($result = mysql_fetch_arr ay($search))
{
PRINT RESULTS!!!
}
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SEL F?query=$found& page=$back_page &limit=$limit\" >back</a> \n");}
for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
$ppage = $limit*($i - 1);
if ($ppage == $page){
echo("<b>$i</b> \n");} // If current page don't give link, just text.
else{
echo("<a href=\"$PHP_SEL Fquery=$found&p age=$ppage&limi t=$limit\"> $i</a> \n");}
}
if (!((($page+$lim it) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo(" <a href=\"$PHP_SEL F?query=$found& page=$next_page &limit=$limit\" >next</a>\n");}
?>
Thanks!
Bigalan
<?
if ($_GET) $vars=$_GET;
else $vars=$_POST;
$found = $vars['search'];
if (!($limit)){
$limit = 10;} // Default results per-page.
if (!($page)){
$page = 0;} // Default page value.
if ($found == "")
{
echo "<p>You forgot to enter a search term<br>";
exit;
}
$username="**** ";
$password="**** **";
$database="**** **";
$host="******** *********";
mysql_connect($ host,$username, $password);
@mysql_select_d b($database) or die( "Unable to select database");
//convert to uppercase to allow for case sensisivities
$found = strtoupper($fou nd);
//strip code from search term
$found = strip_tags($fou nd);
//strip spaces from search term
$found = trim ($found);
$check = mysql_query("SE LECT * FROM song WHERE upper(descripti on) LIKE '%$found%'");
$anymatches=mys ql_num_rows($ch eck);
if ($anymatches == 0)
{
echo "Sorry, no matching results were found<br><br><b r>";
}
$pages = intval($anymatc hes/$limit); // Number of results pages.
// $pages now contains int of pages, unless there is a remainder from division.
if ($anymatches % $limit) {
$pages++;} // has remainder so add one page
$current = ($page/$limit) + 1; // Current page number.
if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.
else {
$total = $pages;} // Else total pages is $pages value.
$first = $page + 1; // The first result.
if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.
else{
$last = $anymatches;} // If last results page, last result equals total number of results
echo "Results <b>$first</b> - <b>$last</b> of <b>$anymatche s</b><br><br>";
$search = mysql_query("SE LECT * FROM song WHERE upper(descripti on) LIKE '%$found%' LIMIT $page, $limit");
while($result = mysql_fetch_arr ay($search))
{
PRINT RESULTS!!!
}
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SEL F?query=$found& page=$back_page &limit=$limit\" >back</a> \n");}
for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
$ppage = $limit*($i - 1);
if ($ppage == $page){
echo("<b>$i</b> \n");} // If current page don't give link, just text.
else{
echo("<a href=\"$PHP_SEL Fquery=$found&p age=$ppage&limi t=$limit\"> $i</a> \n");}
}
if (!((($page+$lim it) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo(" <a href=\"$PHP_SEL F?query=$found& page=$next_page &limit=$limit\" >next</a>\n");}
?>
Thanks!
Bigalan
Comment