PHP Results on multiple pages

Hello guys,
I am having a search script which supose to take results out of my mysql
database and display on a page 10 results
if the results are more then 10 it should create another page
or pages with Next >> and Prev << link so anyone can move
forward and backward on those results page.

This script do everything fine counting total records
it display's 1 to 10 records on first page with numbers but
second location where i want it to display Showing results 1 to 10 it shows
17 to 26 second it displays a link to Next but not working.

// begin to show results set
echo " found $numrows results <p>";
$count = 1 + $s;

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {

$title = $row["sname"];
$url = $row["url"];
$message = $row["massage"];
// <a href=\"$row['url']\">"$row['sname']</a>

$row['massage']<br><hr>'\

echo "$count.) ";
echo "<b><a href=\"$url\" target=\"_blank\">$title</a></b>";
echo "<br>";
echo "$message<br><hr height=12 color=lightblue><br>";
$count++;

}

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

//break before paging
echo "
";

// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <a href='{$_SERVER['PHP_SELF']}?s=$prevs&q=$var'> <<<
Prev 10</a>  ";
}

// calculate number of pages needing links
// $s=intval($numrows/$limit);
$s=ceil($numrows/$limit);


// $s now contains int of pages needed unless there is a remainder from
division

if ($numrows%$limit) {
// has remainder so add one page
$s++;
}

// check to see if last page
if (!((($s+$limit)/$limit)==$s) && $s!=1) {

// not last page so give NEXT link $PHP_SELF >>
$news="$s+$limit";

echo " <a href='{$_SERVER['PHP_SELF']}?s=$news&q=$var'>Next 10 >>>
</a>";

}

$a = $s+$limit;
if ($a > $numrows) { $a = $numrows; }
$b = $s+1;
echo "<p>Showing results $b to $a of $numrows</p>";
include ('footer.inc');
?>

I shall highly appreciate if anyone shall have a look at it
and let me know where i am wrong.

Thank you.


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tariq Ismail Dalvi [ Mo, 26 Januar 2009 04:20 ] [ ID #1986677 ]

Re: PHP Results on multiple pages

> This script do everything fine counting total records
> it display's 1 to 10 records on first page with numbers but
> second location where i want it to display Showing results 1 to 10 it shows
> 17 to 26 second it displays a link to Next but not working.

It takes roughly 20 secs to do a search? Perhaps that should also be
looked at.

Try to narrow down your problem.

> // begin to show results set
> echo " found $numrows results <p>";
> $count = 1 + $s;

Where does $s come from? What is it supposed to mean? (Name your
variables properly).

> // now you can display the results returned
> while ($row= mysql_fetch_array($result)) {
>
> $title = $row["sname"];
> $url = $row["url"];
> $message = $row["massage"];
> // <a href=\"$row['url']\">"$row['sname']</a>

> $row['massage']<br><hr>'\
>
> echo "$count.) ";
> echo "<b><a href=\"$url\" target=\"_blank\">$title</a></b>";
> echo "<br>";
> echo "$message<br><hr height=12 color=lightblue><br>";
> $count++;
>
> }
>
> $currPage = (($s/$limit) + 1);
>
> //break before paging
> echo "
";
>
> // next we need to do the links to other results
> if ($s>=1) { // bypass PREV link if s is 0
> $prevs=($s-$limit);
> print " <a href='{$_SERVER['PHP_SELF']}?s=$prevs&q=$var'> <<<
> Prev 10</a>  ";
> }
>
> // calculate number of pages needing links
> // $s=intval($numrows/$limit);
> $s=ceil($numrows/$limit);
>
>
> // $s now contains int of pages needed unless there is a remainder from
> division
>
> if ($numrows%$limit) {
> // has remainder so add one page
> $s++;
> }

What is $s set to here when there's extra pages to display?

> // check to see if last page
> if (!((($s+$limit)/$limit)==$s) && $s!=1) {

That's just too complicated to read. The first part seems redundant
since you're calculating $s anyway, why re-do it?

// are there extra pages to display?
if ($s > 1) {
echo "show extra pages";
}


> // not last page so give NEXT link $PHP_SELF >>
> $news="$s+$limit";
>
> echo " <a href='{$_SERVER['PHP_SELF']}?s=$news&q=$var'>Next 10 >>>
> </a>";
>
> }
>
> $a = $s+$limit;
> if ($a > $numrows) { $a = $numrows; }
> $b = $s+1;
> echo "<p>Showing results $b to $a of $numrows</p>";
> include ('footer.inc');
> ?>
>
> I shall highly appreciate if anyone shall have a look at it
> and let me know where i am wrong.
>
> Thank you.
>
>


--
Postgresql & php tutorials
http://www.designmagick.com/


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
dmagick [ Mo, 26 Januar 2009 22:58 ] [ ID #1986680 ]

Re: PHP Results on multiple pages

Hello Chris,

I am inserting complete script for you to have a look at and was using
$s = $pages

// Get the search variable from URL

$var = [at] $_GET['q'] ;
$trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10;


// check for an empty string and display a message.

if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
exit;
}

// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}

//connect to your database **

mysql_connect("localhost","username","password"); //(host, username,
password)

//specify database **

mysql_select_db("_database") or die("Unable to select database"); //select
which database we're using

// Build SQL Query
$query = "select * from mytable where massage like '%".$trimmed."%' order by
id";
// EDIT HERE and specify your table and field names for the SQL query

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);


// If we have no results, offer a google search as an alternative

if ($numrows == 0)
{

echo "<h4>Results</h4>";
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero
results</p>";

}

// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo "<p>You searched for :<font color=blue size=+2> "" . $var .
""</font>";


// begin to show results set
echo " found $numrows results <p>";
$count = 1 + $s;

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {

$title = $row["sname"];
$url = $row["url"];
$message = $row["massage"];
// <a href=\"$row['url']\">"$row['sname']</a>

$row['massage']<br><hr>'\

echo "$count.) ";
echo "<b><a href=\"$url\" target=\"_blank\">$title</a></b>";
echo "<br>";
echo "$message<br><hr height=12 color=lightblue><br>";
$count++;

}

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

//break before paging
echo "
";

// next we need to do the links to other results <<
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <a href='{$_SERVER['PHP_SELF']}?s=$prevs&q=$var'> <<<
Prev 10</a>  ";
}

// calculate number of pages needing links
// $s=intval($numrows/$limit);
$s=ceil($numrows/$limit);


// $s now contains int of pages needed unless there is a remainder from
division

if ($numrows%$limit) {
// has remainder so add one page
$s++;
}

// check to see if last page
if (!((($s+$limit)/$limit)==$s) && $s!=1) {

// not last page so give NEXT link $PHP_SELF >>
$news="$s+$limit";

echo " <a href='{$_SERVER['PHP_SELF']}?s=$news&q=$var'>Next 10 >>>
</a>";

}

$a = $s+$limit;
if ($a > $numrows) { $a = $numrows; }
$b = $s+1;
echo "<p>Showing results $b to $a of $numrows</p>";
include ('footer.inc');
?>


"Chris" <dmagick [at] gmail.com> wrote in message
news:497E31E9.2040808 [at] gmail.com...
>
>> This script do everything fine counting total records
>> it display's 1 to 10 records on first page with numbers but
>> second location where i want it to display Showing results 1 to 10 it
>> shows
>> 17 to 26 second it displays a link to Next but not working.
>
> It takes roughly 20 secs to do a search? Perhaps that should also be
> looked at.
>
> Try to narrow down your problem.
>
>> // begin to show results set
>> echo " found $numrows results <p>";
>> $count = 1 + $s;
>
> Where does $s come from? What is it supposed to mean? (Name your variables
> properly).
>
>> // now you can display the results returned
>> while ($row= mysql_fetch_array($result)) {
>>
>> $title = $row["sname"];
>> $url = $row["url"];
>> $message = $row["massage"];
>> // <a href=\"$row['url']\">"$row['sname']</a>

>> $row['massage']<br><hr>'\
>>
>> echo "$count.) ";
>> echo "<b><a href=\"$url\" target=\"_blank\">$title</a></b>";
>> echo "<br>";
>> echo "$message<br><hr height=12 color=lightblue><br>";
>> $count++;
>>
>> }
>>
>> $currPage = (($s/$limit) + 1);
>>
>> //break before paging
>> echo "
";
>>
>> // next we need to do the links to other results
>> if ($s>=1) { // bypass PREV link if s is 0
>> $prevs=($s-$limit);
>> print " <a href='{$_SERVER['PHP_SELF']}?s=$prevs&q=$var'> <<<
>> Prev 10</a>  ";
>> }
>>
>> // calculate number of pages needing links
>> // $s=intval($numrows/$limit);
>> $s=ceil($numrows/$limit);
>>
>>
>> // $s now contains int of pages needed unless there is a remainder from
>> division
>>
>> if ($numrows%$limit) {
>> // has remainder so add one page
>> $s++;
>> }
>
> What is $s set to here when there's extra pages to display?
>
>> // check to see if last page
>> if (!((($s+$limit)/$limit)==$s) && $s!=1) {
>
> That's just too complicated to read. The first part seems redundant since
> you're calculating $s anyway, why re-do it?
>
> // are there extra pages to display?
> if ($s > 1) {
> echo "show extra pages";
> }
>
>
>> // not last page so give NEXT link $PHP_SELF >>
>> $news="$s+$limit";
>>
>> echo " <a href='{$_SERVER['PHP_SELF']}?s=$news&q=$var'>Next 10 >>>
>> </a>";
>>
>> }
>>
>> $a = $s+$limit;
>> if ($a > $numrows) { $a = $numrows; }
>> $b = $s+1;
>> echo "<p>Showing results $b to $a of $numrows</p>";
>> include ('footer.inc');
>> ?>
>>
>> I shall highly appreciate if anyone shall have a look at it
>> and let me know where i am wrong.
>>
>> Thank you.
>>
>>
>
>
> --
> Postgresql & php tutorials
> http://www.designmagick.com/
>


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tariq Ismail Dalvi [ Do, 29 Januar 2009 05:45 ] [ ID #1987126 ]

Re: PHP Results on multiple pages

Tariq Ismail Dalvi wrote:
> Hello Chris,
>
> I am inserting complete script for you to have a look at and was using
> $s = $pages
>
> // Get the search variable from URL
>
> $var = [at] $_GET['q'] ;
> $trimmed = trim($var); //trim whitespace from the stored variable
>
> // rows to return
> $limit=10;
>
>
> // check for an empty string and display a message.
>
> if ($trimmed == "")
> {
> echo "<p>Please enter a search...</p>";
> exit;
> }
>
> // check for a search parameter
> if (!isset($var))
> {
> echo "<p>We dont seem to have a search parameter!</p>";
> exit;
> }

I'd change that to:

if (!isset($_GET['q'])) {
echo "Search for something - or some other error message";
exit;
}

$search_term = trim($_GET['q']);


> // Build SQL Query
> $query = "select * from mytable where massage like '%".$trimmed."%'
> order by
> id";

You have sql injection here. You need to use mysql_real_escape_string:

$query = "select * from table where message like '%" .
mysql_real_escape_string($search_term) . "%'";

>
> $numresults=mysql_query($query);
> $numrows=mysql_num_rows($numresults);
>
>
> // If we have no results, offer a google search as an alternative
>
> if ($numrows == 0)
> {
>
> echo "<h4>Results</h4>";
> echo "<p>Sorry, your search: "" . $trimmed . "" returned zero
> results</p>";
>
> }

You have an xss injection problem here. You need to use htmlentities or
htmlspecialchars when you display user supplied input:

echo "Your search for "" . htmlspecialchars($trimmed) . ""
returned no results";
exit;

> // next determine if s has been passed to script, if not use 0
> if (empty($s)) {
> $s=0;
> }
> // get results
> $query .= " limit $s,$limit";
> $result = mysql_query($query) or die("Couldn't execute query");

You're re-running your query - this time with a limit.

The first query should either be a 'COUNT' (so it doesn't actually
retrieve all the results and return them it just does a count), or if
this is a mysql specific query (and will only ever be), possibly use
their special 'SQL_CALC_ROWS_FOUND' function (search
http://dev.mysql.com for it).

If it's a count, the first part will be something like:
$query = "select count(message_id) AS message_count from table where
message like '%" . mysql_real_escape_string($search_term) . "%'";
$results = mysql_query($query);
$row = mysql_fetch_assoc($results);
$messages_found = $row['message_count'];

Then run your actual search query with the limit so you only fetch 10
results.

> // display what the person searched for
> echo "<p>You searched for :<font color=blue size=+2> "" . $var .
> ""</font>";

XSS issue here again.


From here, rewrite it so it's a little easier to follow (and please use
variable names that make sense! $s and $q do not).

// work out pagination
// number_of_pages will be:
// $messages_found / $number_of_results_per_page

$number_of_pages = $messages_found / $number_of_results_per_page;

$current_page = 0;
if (isset($_GET['page'])) {
$current_page = (int)$_GET['page'];
}

// if we're on page 0, don't show a prev link
// if it's less than 0, someone is trying to be nasty!
if ($current_page > 0) {
echo '<a href="' . $_SERVER['PHP_SELF'] . '?q=' . $search_term .
'&page=' . ($current_page - 1) . '>Prev</a>';
}

// if we're not on the last page, show a next link
if ($current_page < $number_of_pages) {
echo "Next link here";
}

--
Postgresql & php tutorials
http://www.designmagick.com/


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
dmagick [ Do, 29 Januar 2009 06:24 ] [ ID #1987127 ]

Re: PHP Results on multiple pages

Hello Guys,
Thank you for your help and suggestions the search script with pagination is
working now i may add bells and rebins to it later.

Thanks again to Chris you really put me on track.

Best regards
Tariq Dalvi


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tariq Ismail Dalvi [ Di, 03 Februar 2009 15:40 ] [ ID #1987733 ]
PHP » gmane.comp.php.database » PHP Results on multiple pages

Vorheriges Thema: printable pdf
Nächstes Thema: Problems with INNER JOIN