Page 1 of 1

Help needed with paging code not working

Posted: Wed Aug 16, 2006 12:59 am
by Guy303
Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Can someone please help me with my code.  It is supposed to be a search engine that takes  3 parameters (Name, Category,State).  It should print 10 records to a page, and the remainder records should be sent to the next page.  However, it is not working and I dont know whats wrong with the code. I've been on it for 3 days. Please can someone help?

Code: Select all

<?php


	require_once('./mysql_connect.php'); //connect to the db.


$var = @$_GET['business_name'];

$var2 = @$_GET['category'];

$var3 = @$_GET['state'];

//$limit=10;

// Handle the form.

 /* Create an empty new variable.
	
	if (isset($var)) {
		$business_name = TRUE;
	} 
	
	// Check for a category.
	if (isset($var2)) {
		$category = TRUE;
	}
	
	if (isset($var3)) {
		$state = TRUE;
	} 
	
	if (!isset($var) && !isset($var2) && !isset($var3))
  {
  echo "<p>We dont seem to have a search parameter!</p>";

  }
*/	
  
$query = "SELECT * FROM Users WHERE "; // start of string

$sub = NULL; //initialize substring

if (isset($_GET['business_name']) && strlen($_GET['business_name']) > 0)
{
 $sub = "Business_Name LIKE '%" . $_GET['business_name'] . "%'";
}

if (isset($_GET['category']) && strlen($_GET['category']) > 0)
{
 $sub .= is_null($sub) ? " Category = '" . $_GET['category'] . "'" : " AND Category = '" . $_GET['category'] . "'";
}

if (isset($_GET['state']) && strlen($_GET['state']) > 0)
{
 $sub .= is_null($sub) ? " State ='" . $_GET['state'] . "'" : " AND State ='" . $_GET['state'] . "'";
}

$limit = 10;

$s = empty($s) ? 0 : $s;

$query .=  $sub . "ORDER BY Business_Name, City LIMIT $s, $limit"; // end of string

$numresults = @mysql_query($query);

$numrows=mysql_num_rows($numresults);

$result = mysql_query($query) or die("Couldn't execute query");



echo "<p>You searched for:"" .$var . ",&nbsp;". $var2 . ",&nbsp;". $var3 . ""</p>";

echo "Results <br />";
$count = 1 + $s;

while ($row=mysql_fetch_array($result)){
	$title=$row["Business_Name"];
	
	echo "$count &nbsp;$title <br />";
	$count++;
}

$currPage = (($s/$limit)+1);

echo "<br />";

if ($s >= 1){
	$prevs = ($s-$limit);
	print "&nbsp;<a href="$PHP_SELF?s=$prevs&business_name={$_GET['business_name']}&category=${$_GET['category']}&state={$_GET['state']}"><< Prev 10 </a>&nbsp;&nbsp;";
}
$pages=intval($numrows/$limit);

if($numrows % $limit){
	$pages++;
}

if(!((($s + $limit)/$limit)== $pages) && $pages != 1){
	$news = $s + $limit;
	
	echo "&nbsp;<a href = "$PHP_SELF?s=$news&business_name={$_GET['business_name']}&category={$_GET['category']}&state={$_GET['state']}">Next 10 >></a>";
}


$a = $s + ($limit);

if ($a > $numrows)
 {
	 $a = $numrows;
 }
$b = $s + 1;
echo "<p>Showing results $b to $a of $numrows</p>";


?>

Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

[color=red][b]Everah[/b] | I modified your post title to be a little more in line with what you actually need help with.[/color]
[quote="[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1"][b]2.[/b] Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.[/quote]

Posted: Wed Aug 16, 2006 1:34 am
by jito
whats the output? do you have any error message?

Posted: Wed Aug 16, 2006 1:36 am
by Guy303
Theres no error message, it will just print 10 records on the first page and even though there should be more, there is no option to click next. The NExt link does not appear.

Posted: Wed Aug 16, 2006 1:54 am
by RobertGonzalez
This piece of code is not evaluating to true...

Code: Select all

if(!((($s + $limit)/$limit)== $pages) && $pages != 1){
        $news = $s + $limit;
       
        echo "&nbsp;<a href = \"$PHP_SELF?s=$news&business_name={$_GET['business_name']}&category={$_GET['category']}&state={$_GET['state']}\">Next 10 >></a>";
}
Try echoing out the vars involved in this portion of script just before the portion of script to see what the script is seeing.

Posted: Wed Aug 16, 2006 2:08 am
by donvliet
Hi,

are you sure your error_reporting is set to 2047?

Code: Select all

$var = @$_GET['business_name'];
$var2 = @$_GET['category'];
$var3 = @$_GET['state'];
Shouldn't these be checked first as in

Code: Select all

$var3 = !empty($_GET['state']) ?  $_GET['state'] : '';

Code: Select all

if (isset($_GET['business_name']) && strlen($_GET['business_name']) > 0)
This would be than

Code: Select all

if($var3)
as you have checked it before

Code: Select all

$sub = "Business_Name LIKE '%" . $_GET['business_name'] . "%'";
Don't you ever check for SQL injection?

Code: Select all

$numresults = @mysql_query($query);
$numrows=mysql_num_rows($numresults);
$result = mysql_query($query) or die("Couldn't execute query");
Any reason, you do the query twice?
Any reason you surpress errors locally?

Don

Posted: Wed Aug 16, 2006 2:13 am
by donvliet

Code: Select all

$PHP_SELF
This indicates register_globals is set to on which is not a good idea either

Don

Posted: Wed Aug 16, 2006 2:27 am
by jito

Code: Select all

$recs=mysql_num_rows($yuorquery);
$recs_per_page=10;
if($recs%$recs_per_page==0)
$no_pages=$recs/$recs_per_page;
else
$no_pages=($recs/$recs_per_page)+1;

if(!$page)$page=1;

$recs_not_to_be_displayed=($page-1)*$recs_per_page;

$query="...    limit $recs_not_to_be_displayed,$recs_per_page";


if($page>1)
{
--$page;
//link for previous
}
elseif($no_pages>$page)
{
++$page;
//link for next
}
try this. sorry because i don't have the time to taste it.

Posted: Wed Aug 16, 2006 2:37 am
by RobertGonzalez
Guy303 wrote:Theres no error message, it will just print 10 records on the first page and even though there should be more, there is no option to click next. The NExt link does not appear.
Before we get to deep in trying to fix all of the errors in this code (and there are plenty of issues with the code) the issue asked by the original poster needs to be addressed. Next is not showing. 10 results are, but next is not.

Try looking at the code I replied with before. The 'Next' display is controlled by that conditional evaluating to true. Since you are not seeing the next link, that conditional is not evaluating to true.

Posted: Wed Aug 16, 2006 2:58 am
by donvliet
Before we get to deep in trying to fix all of the errors in this code (and there are plenty of issues with the code) the issue asked by the original poster needs to be addressed. Next is not showing. 10 results are, but next is not.
Fair enough, still I think the original poster needs to throw and display errors in the first place (i.e. get rid of all '@' and turn error_reporting(2047))

Don

Posted: Wed Aug 16, 2006 3:10 am
by RobertGonzalez
I totally agree with you. There are number of things that need to be worked on in that code. But he came to us looking for why his 'Next' link wasn't there. I think before we lay the smack down on him for that cruddy code Image, we should at least help figure out his original issue. Image

Posted: Wed Aug 16, 2006 3:27 am
by Guy303
Thank you guys for taking the time to reply. When i get rid of that "if statement" the code works, except that in the last page the next link appears when it shouldn't. I appreciate you guys looking at the code, I just started php and I really am happy you all took the time to respond. I wanted to keep you guys up to date so you dont think I gave up. I will work on this some more and let you know the result. Thanks again.

Posted: Wed Aug 16, 2006 3:40 am
by donvliet
Everah wrote:I totally agree with you. There are number of things that need to be worked on in that code. But he came to us looking for why his 'Next' link wasn't there.
Ok, then let's do some serious debugging

@Guy303
Modify your code like this:

Code: Select all

<?php                                                                                                                                                                              
 error_reporting(2047);                                                                                                                                                                                  
require_once('./mysql_connect.php'); //connect to the db.
And just above the query:

Code: Select all

$s = empty($s) ? 0 : $s;
print 's has the value: ' . $s . "\n"; // where does this mysterious $s come from all of a sudden? Do you refer to $_GET['s'] maybe? 
$query .=  $sub . "ORDER BY Business_Name, City LIMIT $s, $limit"; // end of string                                                                                                
print 'the query is: ' . $query . "\n";                                                                                                                                       
$result = mysql_query($query) or die(mysql_error($connection));   //  this assumes, you called the result of mysql_connect(), $connection, modify this to the real variable name
$numrows = mysql_num_rows($results); 
print 'numrows says it is: '  . $numrows . "\n";
Post everything that has been printed out by now and we'll take it from there

Posted: Wed Aug 16, 2006 3:49 am
by Guy303
thank you very much for your willing to help. Thank you. Here is what I got...


s has the value: 0
Notice: Undefined variable: sub in /home/assyrian/www/www/directory/search.php on line 56

Notice: Undefined variable: limit in /home/assyrian/www/www/directory/search.php on line 56

Notice: Undefined variable: query in /home/assyrian/www/www/directory/search.php on line 56
the query is: ORDER BY Business_Name, City LIMIT 0,
Notice: Undefined variable: connection in /home/assyrian/www/www/directory/search.php on line 58

Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in /home/assyrian/www/www/directory/search.php on line 58

Posted: Wed Aug 16, 2006 4:34 am
by donvliet
s has the value: 0
Can you please explain, where $s comes from?
Notice: Undefined variable: sub in /home/assyrian/www/www/directory/search.php on line 56
Notice: Undefined variable: limit in /home/assyrian/www/www/directory/search.php on line 56
Notice: Undefined variable: query in /home/assyrian/www/www/directory/search.php on line 56
the query is: ORDER BY Business_Name, City LIMIT 0,
You don't use exactly the code you have posted originally, since in this code $sub is at least defined, $limit is 10 and $query is at least "SELECT * FROM Users WHERE ". Are you sure you haven't commented something out?
Notice: Undefined variable: connection in /home/assyrian/www/www/directory/search.php on line 58
As I said, you must rename this to the variable name you have chosen in mysql_connect.php
Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in /home/assyrian/www/www/directory/search.php on line 58
Follow up error


-----------------

I just read the last postings again and figured there must have been a misunderstanding about my 'modify your code' posting. I didn't mean 'Replace your existing code with mine' but 'Modify the areas I have modified'. If you replaced your code, this would explain the strange error messages you posted.

Posted: Wed Aug 16, 2006 6:19 am
by Jenk
Have a look at this shamelessly pimped pagination class (if you are using PHP5)

viewtopic.php?p=297424#297424

Try this with the above class:

Code: Select all

<?php

require_once('./mysql_connect.php'); //connect to the db.
require_once('./jmt_Paginator.class.php'); //include paginator definition

// start query build
$query = "SELECT * FROM Users WHERE "; // start of string

if (!empty($_GET['business_name'])) {
    $sub = "Business_Name LIKE '%" . mysql_real_escape_string($_GET['business_name']) . "%'";
}

if (!empty($_GET['category'])) {
   (isset($sub) ? $sub = " Category = '" . mysql_real_escape_string($_GET['category']) . "'"
                : $sub .= " AND Category = '" . mysql_real_escape_string($_GET['category']) . "'");
}

if (!empty($_GET['state'])) {
   (isset($sub) ? $sub = " State ='" . mysql_real_escape_string($_GET['state']) . "'"
                : $sub .= " AND State ='" . mysql_real_escape_string($_GET['state']) . "'");
}

$query .=  $sub . "ORDER BY Business_Name, City";
// end query build

// start data retrieval
$result = mysql_query($query) or die ('Database error');
$numrows = mysql_num_rows($numresults);

$data = array();

while ($row = mysql_fetch_array($result)){
    $data[] = $row;
}
// end data retrieval

// start pagination
$paginator = new jmt_Paginator($data, 10);

if (!empty($_GET['s'])) $paginator->setPage((int)$_GET['s']); // set page (defaults to 1st page if not set)

echo "Results <br />";
echo "<br />";

foreach ($paginator->getPageData() as $item) {
   echo '<p>' . $item['business_name'] . '</p>';
}

if ($page = $paginator->getPrevPage()){
    $business_name = htmlentities($_GET['business_name']);
    $catgegory = htmlentities($_GET['category']);
    $state = htmlentities($_GET['state']);
    
    print "&nbsp;<a href=\"{$_SERVER['SCRIPT_NAME']}?"
        . "s={$page}&"
        . "business_name={$business_name}&"
        . "category={$category}&"
        . "state={$state}\"><< Prev 10 </a>&nbsp;&nbsp;";
}

if($page = $paginator->getNextPage()){
    $business_name = htmlentities($_GET['business_name']);
    $catgegory = htmlentities($_GET['category']);
    $state = htmlentities($_GET['state']);
    
    print "&nbsp;<a href = \"{$_SERVER['SCRIPT_NAME']}?"
        . "s={$page}&"
        . "business_name={$business_name}&"
        . "category={$category}&"
        . "state={$state}\">Next 10 >></a>";
}

echo '<p>Showing page ' . $paginator->getCurrentPage()
   . ' of ' . $paginator->getTotalPages() . ' pages</p>";
// end pagination

?>
(untested)

NB: I've used $_SERVER['SCRIPT_NAME'] because I would get shot down for using $_SERVER['PHP_SELF'] as it has known vulnerabilities.