Jump to content


Photo

Having a Pagination Problem


  • Please log in to reply
15 replies to this topic

#1 SkyRanger

SkyRanger
  • Members
  • PipPipPip
  • Advanced Member
  • 649 posts

Posted 17 October 2006 - 04:50 PM

//start Top Pagination Code
if (!(isset($pagenum))) 
{ 
$pagenum = 1; 
} 

//Here we count the number of results 
//Edit $data to be your query 
$data = mysql_query("SELECT * FROM quest where auth='1'") or die(mysql_error()); 
$rows = mysql_num_rows($data); 

//This is the number of results displayed per page 
$page_rows = 10; 

//This tells us the page number of our last page 
$last = ceil($rows/$page_rows); 

//this makes sure the page number isn't below one, or more than our maximum pages 
if ($pagenum < 1) 
{ 
$pagenum = 1; 
} 
elseif ($pagenum > $last) 
{ 
$pagenum = $last; 
} 

//This sets range that we will display in our query 
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

$result = mysql_query( "SELECT * FROM quest where auth='1' order by 'id' desc $max") or die( "Unable to select database");
$wordnum = mysql_num_rows( $result );

while( $row = mysql_fetch_array( $result ) )

{
//End Pagination Code
			?><b>Username:</b> <?
			echo $row["uname"]; 
			?>&nbsp;<b>|</b>&nbsp;<b>Age:</b> <?
			echo $row["age"];
			?>&nbsp;<b>|</b>&nbsp;<b>Date:</b> <?
			echo $row["pdate"];
			?><br><?
			echo $row["msgpost"]; ?>
			<br><br><hr>
			<? 
			}
			?><center>
            <?


			echo " --Page $pagenum of $last-- <p>";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1) 
{
} 
else 
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
} 

//just a spacer
echo " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last) 
{
} 
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
} 

The problem is that I am getting a "Unable to select database" unless I go into mysql and manually insert an entry.  Is there a way to fix this so I don't have to do this?
If you search for it, it may be there.

Ensure you use < > button before inserting code for easy reading.

A wise Lady Guru keeps reminding me if you have a select problem: READ THIS

#2 SkyRanger

SkyRanger
  • Members
  • PipPipPip
  • Advanced Member
  • 649 posts

Posted 17 October 2006 - 06:32 PM

Does anybody have an idea of why I am getting this error?
If you search for it, it may be there.

Ensure you use < > button before inserting code for easy reading.

A wise Lady Guru keeps reminding me if you have a select problem: READ THIS

#3 SkyRanger

SkyRanger
  • Members
  • PipPipPip
  • Advanced Member
  • 649 posts

Posted 17 October 2006 - 11:39 PM

I am getting a feeling by the look of people who have viewed this post is just as lost as I am.  Well thanks guys for trying to help, but if you can come up with something please let me know.
If you search for it, it may be there.

Ensure you use < > button before inserting code for easy reading.

A wise Lady Guru keeps reminding me if you have a select problem: READ THIS

#4 rab

rab
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts

Posted 17 October 2006 - 11:44 PM

Well are you connected to a mysql database?

#5 SkyRanger

SkyRanger
  • Members
  • PipPipPip
  • Advanced Member
  • 649 posts

Posted 17 October 2006 - 11:47 PM

Yes, I just left that garbage out.  Connecting is no problem, pulling is no problem.  Having the script not show ""Unable to select database"" when there is nothing in the table is the problem.

If I manually add something through phpmysql then it works, but if empty I get the error
If you search for it, it may be there.

Ensure you use < > button before inserting code for easy reading.

A wise Lady Guru keeps reminding me if you have a select problem: READ THIS

#6 rab

rab
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts

Posted 17 October 2006 - 11:50 PM

$result = mysql_query( "SELECT * FROM quest where auth='1' order by 'id' desc $max");

if( $result === FALSE ) { print "MySQL Error: "; mysql_error($result); }

I'm pretty sure that that might be the problem

#7 SkyRanger

SkyRanger
  • Members
  • PipPipPip
  • Advanced Member
  • 649 posts

Posted 18 October 2006 - 12:01 AM

Ok this is what I did:

$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

 $result = mysql_query( "SELECT * FROM tricks where auth='1' order by 'id' desc $max"); //removed or die
//added
Line:287 if( $result === FALSE ) { print "MySQL Error: "; mysql_error($result); }
Link:288 $wordnum = mysql_num_rows( $result );

Line:290 while( $row = mysql_fetch_array( $result ) )

And end up getting:

MySQL Error:
Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in /public_html/poster.php on line 287

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in public_html/poster.php on line 288

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in public_html/poster.php on line 290
If you search for it, it may be there.

Ensure you use < > button before inserting code for easy reading.

A wise Lady Guru keeps reminding me if you have a select problem: READ THIS

#8 rab

rab
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts

Posted 18 October 2006 - 12:29 AM

gah, stupid mistake. give mysql_error() the mysql resource link

#9 SkyRanger

SkyRanger
  • Members
  • PipPipPip
  • Advanced Member
  • 649 posts

Posted 18 October 2006 - 12:47 AM

gah, stupid mistake. give mysql_error() the mysql resource link


Not a prob rab, I am a noob, I am surprised I got this far...lol

Not sure if this is right, this time I posted the whole code:

include "inc/dbinfo.inc.php";
$connection=mysql_connect ("$dblocation", "$dbusername", "$dbpassword") or die ('I cannot connect to the database because: ' . mysql_error());
        mysql_select_db ("$dbname");

//start Top Pagination Code
if (!(isset($pagenum))) 
{ 
$pagenum = 1; 
} 

//Here we count the number of results 
//Edit $data to be your query 
$data = mysql_query("SELECT * FROM tricks where auth='1'") or die(mysql_error()); 
$rows = mysql_num_rows($data); 

//This is the number of results displayed per page 
$page_rows = 10; 

//This tells us the page number of our last page 
$last = ceil($rows/$page_rows); 

//this makes sure the page number isn't below one, or more than our maximum pages 
if ($pagenum < 1) 
{ 
$pagenum = 1; 
} 
elseif ($pagenum > $last) 
{ 
$pagenum = $last; 
} 

//This sets range that we will display in our query 
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

$result = mysql_query( "SELECT * FROM tricks where auth='1' order by 'id' desc $max"); 
if( $result === FALSE ) { print "MySQL Error: "; mysql_error($connection); }
$wordnum = mysql_num_rows( $result );

while( $row = mysql_fetch_array( $result ) )

{
//End Pagination Code
			?><b>Username:</b> <?
			echo $row["uname"]; 
			?>&nbsp;<b>|</b>&nbsp;<b>Age:</b> <?
			echo $row["age"];
			?>&nbsp;<b>|</b>&nbsp;<b>Date:</b> <?
			echo $row["pdate"];
			?><br><?
			echo $row["msgpost"]; ?>
			<br><br><hr>
			<? 
			}
			?><center>
            <?


			echo " --Page $pagenum of $last-- <p>";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1) 
{
} 
else 
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
} 

//just a spacer
echo " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last) 
{
} 
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
} 

If that is right then I am now getting:

MySQL Error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/squirt/public_html/poster.php on line 288

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/squirt/public_html/poster.php on line 290

$result = mysql_query( "SELECT * FROM tricks where auth='1' order by 'id' desc $max"); 
if( $result === FALSE ) { print "MySQL Error: "; mysql_error($connection); }
line:288 $wordnum = mysql_num_rows( $result );

line:290 while( $row = mysql_fetch_array( $result ) )

If you search for it, it may be there.

Ensure you use < > button before inserting code for easy reading.

A wise Lady Guru keeps reminding me if you have a select problem: READ THIS

#10 rab

rab
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts

Posted 18 October 2006 - 01:08 AM

Try this, there __may__ be some typos

<?php
include "inc/dbinfo.inc.php";

$connection = mysql_connect ($dblocation, $dbusername, $dbpassword);

if( !$connection )  // Error checking 
{
	die("Error connecting to MySQL Host: ". mysql_error());
}

$db = mysql_select_db ($dbname, $connection); // You NEED the MySQL link ID to select a DB

if( !$db ) // Error Checking
{
	die("Error selecting a database: ". mysql_error($connection));
}

// Whats $pagenum? Defined? No
if( !( isset($pagenum) ) ) 
{ 
	$pagenum = 1; 
} 

$data = mysql_query("SELECT * FROM tricks where auth='1'");

if( !$data ) 
{
	die("Error selecting: ". mysql_error($connection));
}

$rows = mysql_num_rows($data);

if( $rows === FALSE ) // Regular num rows can return 0 for 0 rows, this checks if it is FALSE
{
	die("Error fetching num rows");
}

//This is the number of results displayed per page 
$page_rows = 10; 

//This tells us the page number of our last page 
$last = ceil($rows/$page_rows); 

//this makes sure the page number isn't below one, or more than our maximum pages 
if( $pagenum < 1 ) 
	$pagenum = 1; 
if( $pagenum > $last ) 
	$pagenum = $last; 

$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

$result = mysql_query( "SELECT * FROM tricks where auth='1' order by 'id' desc $max"); 

if( !$result ) 
{
	die("Error Selecting: ". mysql_error($connection));
}

$wordnum = mysql_num_rows( $result );

if( $wordnum === FALSE ) 
{ 
	die("Error selecting num rows");
}

while( $row = mysql_fetch_array( $result ) )
{
	?>
		<b>Username:</b> <?=$row["uname"]; ?>
		<b>Age:</b> <?=$row["age"]; ?>
		<b>Date:</b> <?=$row["pdate"]; ?>
		<b><?=$row["msgpost"]; ?>
		<br><br><hr>
	<? 
}
echo " --Page $pagenum of $last-- <p>";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if( $pagenum != 1 )
{
	echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
	echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=".$pagenum-1."'> <-Previous</a> ";
} 

//just a spacer
echo " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if( $pagenum != $last ) 
{
	echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=".$pagenum+1."'>Next -></a> ";
	echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
} 


#11 SkyRanger

SkyRanger
  • Members
  • PipPipPip
  • Advanced Member
  • 649 posts

Posted 18 October 2006 - 01:34 AM

Thanks rab,

just left with this:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in /home/squirt/public_html/tips_tricks.phtml on line 364

echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=".$pagenum-1."'> <-Previous</a> ";

It may or may not be that, but I will dig though the code and see if something is missing.
If you search for it, it may be there.

Ensure you use < > button before inserting code for easy reading.

A wise Lady Guru keeps reminding me if you have a select problem: READ THIS

#12 rab

rab
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts

Posted 18 October 2006 - 01:37 AM

Could be, I take it that the script worked. I think your problem was when you were selecting a database you didnt supply a MySQL link ID.

#13 SkyRanger

SkyRanger
  • Members
  • PipPipPip
  • Advanced Member
  • 649 posts

Posted 18 October 2006 - 02:12 AM

Not sure, I copied and pasted exactly what you put, double check to make sure everything was closed, check all the syntax.

reuploaded and got this: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' on line 332


Line 332: echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=".$pagenum-1."'> <-Previous</a> ";

Starting to beat head off keyboard.  I am going to beat this...lol

oh, and that error is the only thing I get on the page, rest of it is white.

If you search for it, it may be there.

Ensure you use < > button before inserting code for easy reading.

A wise Lady Guru keeps reminding me if you have a select problem: READ THIS

#14 xsist10

xsist10
  • Members
  • PipPipPip
  • Advanced Member
  • 114 posts

Posted 18 October 2006 - 09:04 AM

echo " <a href='". $_SERVER['PHP_SELF'] ."?pagenum=".$pagenum-1."'> &lt;-Previous</a> ";

SA PHP Archives - www.phparchives.za.org

#15 JasonLewis

JasonLewis
  • Members
  • PipPipPip
  • Advanced Member
  • 3,351 posts
  • LocationVictoria, Australia

Posted 18 October 2006 - 09:46 AM

if you ask me, that is an extremely long pagination code. it could be a lot simplier in the same style.
Good luck with your coding.
Jason / ProjectFear / Jaysonic

#16 SkyRanger

SkyRanger
  • Members
  • PipPipPip
  • Advanced Member
  • 649 posts

Posted 19 October 2006 - 08:36 PM

Thanks guys, I got it working, with alot smaller script, had alot of useless garbage in there.
If you search for it, it may be there.

Ensure you use < > button before inserting code for easy reading.

A wise Lady Guru keeps reminding me if you have a select problem: READ THIS




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users