Next page button script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
imimin
Forum Commoner
Posts: 38
Joined: Thu Oct 18, 2007 5:44 pm

Next page button script

Post by imimin »

The following code is supposed to go to the next page of selected items from a DB, but when I click on the ">>" on the php page, nothing happens. You know, like when you do a search on Google and you want to go the next page of results you click on the little arrow.

Can someone help me with this or tell me how to troubleshoot it? Or, maybe someone knows of a good script that will work?

Thanks!

Code: Select all

    <?php
    $num_pages = $number_of_rows/$items_to_view;
    $num_pages = ceil($num_pages);
    ?>
    <CENTER>
    <FORM name="previews_num" method=post>
    <INPUT TYPE="button" NAME="next" VALUE="<<">
    [Page <?php echo $start_view + 1 ?> of <?php echo $num_pages ?>]
    <INPUT TYPE="button" NAME="next" VALUE=">>">
    <BR><BR>
    <SELECT name="previews_per_page" onchange="javascript&#058;document.previews_num.submit()">
    <?php
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Next page button script

Post by califdon »

This is referred to as "pagination" and if you use that term in a Google search, you will find a host of tutorials and discussion on how to do it. Here's a 4-part tutorial I wrote on the subject: http://www.developerbarn.com/blogs/don9 ... art-1.html
imimin
Forum Commoner
Posts: 38
Joined: Thu Oct 18, 2007 5:44 pm

Re: Next page button script

Post by imimin »

Thanks Jack

Nice tutorial, very nice. I think I have most everything set up on my page to make this function (I am pretty much a rooky at PHP and SQL) work correctly. However, I have a problem with the AS function value of 'mycount'. Keeping that in the script as you have it I get the following error when refreshing my browser:

Code: Select all

Notice: Use of undefined constant mycount - assumed 'mycount' in c:\program files\easyphp1-8\www\patternsofjoy\index.php on line 109
Query failed.
Line 109 on index page:

Code: Select all

$numrecs=mysql_result($result, 0, mycount);
Can you tell me what is going on here?

Thanks!

Gary
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Next page button script

Post by califdon »

Well, in my script, I defined an alias mycount in my query, using AS to do so:

Code: Select all

$sql="SELECT COUNT(*) AS mycount FROM xyz" or die("Query failed.");
$result=mysql_query($sql);
$numrecs=mysql_result($result, 0, mycount);
so there will be a value for mycount. Actually, since I almost never use the mysql_result() function, I may be in error on the syntax. I just looked it up and it isn't clear whether the alias field name should be within quotes or not. But it also says that the 3rd argument is optional, in which case it will return the first column of the query, so you could just leave out the 3rd argument and it should work:

Code: Select all

$sql="SELECT COUNT(*) AS mycount FROM xyz" or die("Query failed.");
$result=mysql_query($sql);
$numrecs=mysql_result($result, 0);
Try it.
imimin
Forum Commoner
Posts: 38
Joined: Thu Oct 18, 2007 5:44 pm

Re: Next page button script

Post by imimin »

Thanks!

As you suggested, I changed my code to:

Code: Select all

    $sql="SELECT COUNT(*)AS mycount FROM poj_products" or die("Query failed.");
        $result=mysql_query($sql);
        $numrecs=mysql_result($result, 0);
and now I don't get an error, but I do get NO table population from the DB and a small statement that says:

Code: Select all

Query failed.
Do you know what might be up? Is there a way to turn on more error reporting?

Thanks!
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Next page button script

Post by califdon »

Sure. Instead of just "Query failed.", substitute mysql_error(). That will show you the mysql error message that will tell you what's wrong with your query.
Post Reply