Paginatating PHP

Paginatating PHP

am 20.12.2006 18:24:41 von Chris Carter

Why is this code that I have written to paginate the result from database
jumping to else condition where the NEXT or PREVIOUS is not hyperlinked as
in the if condition. There is enough data to display this result and to
paginate but its simply showing the else condition which does not have NEXT
or PREVIOUS hyperlinked.

--------------------- CODE -------------------------


$host = 'xxx';
$user = 'xxx';
$password = 'xxx';
$database = 'xxx';

@mysql_connect($host, $user, $password) or die("ERROR--CAN'T CONNECT TO
SERVER");
@mysql_select_db($database) or die("ERROR--CAN'T CONNECT TO DB");

$limit = 15;
$query_count = "SELECT count(*) FROM students";
$result_count = mysql_query($query_count);
$totalrows = mysql_num_rows($result_count);

if(empty($page)){
$page = 1;
}


$limitvalue = $page * $limit - ($limit);
$query = "SELECT * FROM students LIMIT $limitvalue, $limit";
$result = mysql_query($query) or die("Error: " . mysql_error());

if(mysql_num_rows($result) == 0){
echo("Nothing to Display!");
}

$bgcolor = "#E0E0E0"; // light gray

echo("

");

while($row = mysql_fetch_array($result)){
if ($bgcolor == "#E0E0E0"){
$bgcolor = "#FFFFFF";
}else{
$bgcolor = "#E0E0E0";
}

echo(" nn");
}

echo("
");
echo($row["name"]);
echo("
");
echo($row["major"]);
echo("
");

if($page != 1){
$pageprev = $page--;

echo(" \"$PHP_SELF&page=$pageprev\" PREV".$limit." ");
}else{
echo("PREV".$limit." ");
}

$numofpages = $totalrows / $limit;

for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo($i." ");
}else{
echo(" \"$PHP_SELF?page=$i\" $i ");
}
}


if(($totalrows % $limit) != 0){
if($i == $page){
echo($i." ");
}else{
echo(" \"$PHP_SELF?page=$i\" $i ");
}

--
View this message in context: http://www.nabble.com/Paginatating-PHP-tf2861083.html#a79941 64
Sent from the Php - Database mailing list archive at Nabble.com.

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Paginatating PHP

am 21.12.2006 10:18:01 von Oskar

Chris Carter wrote:
>There is enough data to display this result and to paginate but its
simply showing the
>else condition which does not have NEXT or PREVIOUS hyperlinked.
>...
>$query_count = "SELECT count(*) FROM students";
>$result_count = mysql_query($query_count);
>$totalrows = mysql_num_rows($result_count);
well, the result is always 1 (even if no students are present), you need
to use some mysql_fetch_* function to get the count of students
ie.
$tmp=mysql_fetch_row($result_count);
$totalrows=$tmp[0];


OKi98

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Paginatating PHP

am 21.12.2006 11:35:07 von Ondrej Kohout

It's better to use SQL_CALC_FOUND_ROWS and information function
FOUND_ROWS().

For more information see
http://dev.mysql.com/doc/refman/4.1/en/information-functions .html

Find: FOUND_ROWS()

Iky


OKi98 wrote:

> Chris Carter wrote:
> >There is enough data to display this result and to paginate but its
> simply showing the
> >else condition which does not have NEXT or PREVIOUS hyperlinked.
> >...
> >$query_count = "SELECT count(*) FROM students";
> >$result_count = mysql_query($query_count);
> >$totalrows = mysql_num_rows($result_count);
> well, the result is always 1 (even if no students are present), you
> need to use some mysql_fetch_* function to get the count of students
> ie.
> $tmp=mysql_fetch_row($result_count);
> $totalrows=$tmp[0];
>
>
> OKi98
>

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Paginatating PHP

am 22.12.2006 07:17:10 von Chris Smith

On 12/21/06, Ondrej Kohout wrote:
> It's better to use SQL_CALC_FOUND_ROWS and information function
> FOUND_ROWS().

Has nothing to do with the problem.

"its simply showing the else condition which does not have NEXT
or PREVIOUS hyperlinked."

Try "OKi98's" suggestion, it will most likely fix the problem for 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