Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 11-02-2009, 01:19 PM Php Pagination
Super Talker

Posts: 115
Name: Not Telling
Trades: 0
I am getting this error with a script:

Code:
Parse error: syntax error, unexpected T_IF in /home/brian/public_html/pages.php on line 5
PHP Code:
<?php
// Connects to your Database
include("inc/config.php");(
//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum 1;
}

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

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

//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 the range to display in our query
$max 'limit ' .($pagenum 1) * $page_rows .',' .$page_rows

//This is your query again, the same one... the only difference is we add $max into it
$data_p mysql_query("SELECT * FROM accounts $max") or die(mysql_error());

//This is where you display your query results
while($info mysql_fetch_array$data_p ))
{
Print 
$info['Name'];
echo 
"<br>";
}
echo 
"<p>";

// This shows the user what page they are on, and the total number of pages
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> ";
}
?>
Anyone know why?

This is the structure of my table:
Code:
+---------+------------+-----------+
| Table Name: accounts             |
+---------+------------+-----------+
|    id   |  username  |  password |
+---------+------------+-----------+
|       1 | john12     | thepass   |
|       2 | catman17   | cat       |
+---------+------------+-----------+
__________________
MY MSN:
Please login or register to view this content. Registration is FREE

PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
sith717 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-02-2009, 01:25 PM Re: Php Pagination
Junior Talker

Posts: 4
Name: Bob
Trades: 0
Yes, remove the open bracket on line 3:

include("inc/config.php");(
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

|
Please login or register to view this content. Registration is FREE
diymonkey is offline
Reply With Quote
View Public Profile
 
Old 11-02-2009, 01:31 PM Re: Php Pagination
Super Talker

Posts: 115
Name: Not Telling
Trades: 0
I have fixed it:

PHP Code:
<?php
error_reporting
(E_ALL);
ini_set('display_errors''1');
include(
"inc/config.php");
if (!(isset(
$pagenum)));
{
$pagenum 1;
}
$data mysql_query("SELECT * FROM accounts") or die(mysql_error());
$rows mysql_num_rows($data);
$page_rows 4;
$last ceil($rows/$page_rows);
if (
$pagenum 1)
{
$pagenum 1;
}
elseif (
$pagenum $last)
{
$pagenum $last;
}
$max 'limit ' .($pagenum 1) * $page_rows .',' .$page_rows
$data_p mysql_query("SELECT * FROM accounts $max") or die(mysql_error());
while(
$info mysql_fetch_array$data_p ))
{
Print 
$info['username'];
Print 
$info['password'];
echo 
"<br>";
}
echo 
"<p>";
echo 
" --Page $pagenum of $last-- <p>";
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> ";
}
echo 
" ---- ";
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> ";
}
?>
Now for some reason it is not going to the next page.

It is only showing the first 4 rows and it wont show the rest when I click the next or last button.
__________________
MY MSN:
Please login or register to view this content. Registration is FREE

PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
sith717 is offline
Reply With Quote
View Public Profile
 
Old 11-02-2009, 02:16 PM Re: Php Pagination
Novice Talker

Posts: 11
Trades: 0
do a requery... add "LIMIT" on your sql statement.
chieina is offline
Reply With Quote
View Public Profile
 
Old 11-02-2009, 02:28 PM Re: Php Pagination
Super Talker

Posts: 115
Name: Not Telling
Trades: 0
Can you show me how?

Like show me where I need to add a limit and stuff.
__________________
MY MSN:
Please login or register to view this content. Registration is FREE

PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
sith717 is offline
Reply With Quote
View Public Profile
 
Old 11-02-2009, 04:02 PM Re: Php Pagination
Super Talker

Posts: 115
Name: Not Telling
Trades: 0
Thanks its fixed now:

PHP Code:
<?php
error_reporting
(E_ALL);
ini_set('display_errors''1');
include(
"inc/config.php");

$pagenum = (isset($_GET['pagenum'])) ? (int) $_GET['pagenum'] : 1;

$data mysql_query("SELECT * FROM accounts") or die(mysql_error());
$rows mysql_num_rows($data);
$page_rows 4;
$last ceil($rows/$page_rows);
if (
$pagenum 1)
{
$pagenum 1;
}
elseif (
$pagenum $last)
{
$pagenum $last;
}
$max 'limit ' .($pagenum 1) * $page_rows .',' .$page_rows;
$data_p mysql_query("SELECT * FROM accounts $max") or die(mysql_error());
while(
$info mysql_fetch_array$data_p ))
{
echo 
"Username: ";
Print 
$info['username'];
echo 
"<br>";
echo 
"Password: ";
Print 
$info['password'];
echo 
"<br>----";
echo 
"<br>";
}
echo 
"<p>";
echo 
" --Page $pagenum of $last-- <p>";
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> ";
}
echo 
" ---- ";
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> ";
}
?>
----

Now I need help with another thing, How would I do it so instead of it posting it like this when im at the first page:

---- Next -> Last ->>

To like this:

| 1 | 2 | 3| Last ->>

Like it adds a number based on how many pages, its like instead of using the Next -> thing clicking over and over again to get to like page 5.

Same thing with the rest of the pages:

<<-First | 1 | 2 | 3| Last ->>

And the last page:

<<-First | 1 | 2 | 3|

Does anyone know how to do this? I want number pages instead of clicking to get to the next page or the page before.
__________________
MY MSN:
Please login or register to view this content. Registration is FREE

PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
sith717 is offline
Reply With Quote
View Public Profile
 
Old 11-02-2009, 04:24 PM Re: Php Pagination
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Run a query to find out how many rows there are (SELECT count(*) FROM table) and divide by the number of rows per page. Then add 1 to make the page numbers go from 1 to (n+1) instead of from 0 to n.

Print out the numbers as links using the loop of your choice (for, while, do-while).
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 11-02-2009, 04:35 PM Re: Php Pagination
Super Talker

Posts: 115
Name: Not Telling
Trades: 0
How would I do that? Can you show me a code to do that? and maybe integrate it into my script? I would be very happy.

I also got this quote from another person:
Quote:
consider using LIMIT and OFFSET

LIMIT=5, OFFSET=10 (show 5 records, starting from the 10th)

increment offset by LIMIT each time.

As for the URLs, divide the total number of rows by the amount you want to show on each page (100records/20 per page==5 pages)

then do a loop

PHP Code:
$offset=0;
for(
$i=1;$i<=5;$i++) { echo '<a href="?offset='.$offset '">'.$i.</a>;
$offset=$offset+20//20=data per page
}
// do query 
But have no idea how to implement that into my coding.
__________________
MY MSN:
Please login or register to view this content. Registration is FREE

PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
sith717 is offline
Reply With Quote
View Public Profile
 
Old 11-02-2009, 05:33 PM Re: Php Pagination
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I havn't looked at your code at all, but it shouldn't be that hard to integrate it. Something like this would work.

PHP Code:
// define rows per page and current page (if you havn't already)
$rowsPerPage 10;
$currentPage $_GET['page'];
$offset 5;  // Number of pages to display at each side of the current page

$query "SELECT count(*) FROM your_table";
$result mysql_query($query);
$count mysql_result($result0);

$nbrOfPages = ($count $rowsPerPage) + ($count $rowsPerPage 0);
/* If reminder when dividing, add another page. Example:
   $count = 10;
   $nbrOfPages = (10 / 10) + (10 % 10 > 0 ? 1 : 0) =
                 = 1 + (0 > 0 ? 1 : 0)
                 = 1 + 0 = 1 page

   $count = 13;
   $nbrOfPages = (13 / 10) + (13 % 10 > 0 ? 1 : 0) =
                 = 1 + (3 > 0 ? 1 : 0)
                 = 1 + 1 = 2 pages

*/

if ($currentPage 1) {
   echo 
'<a href="yoursite.php?page=1">&lt;&lt; First page</a> ';
}
$start $currentPage $offset;
$end $currentPage $offset;
if (
$start 1) {
   
$start 1;
}
if (
$end $nbrOfPages) {
   
$end $nbrOfPages;
}
for(
$i $start$i <= $end$i++) {
   if (
$i $currentPage) {
      echo 
" <b>$i<b/> ";
   } else {
      echo 
' <a href="yoursite.php?page=' $i '">' $i '</a> ';
   }
}
if (
$currentPage $nbrOfPages) {
   echo 
' <a href="yoursite.php?page=' $nbrOfPages '">Last page &gt;&gt;</a>';

__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 11-02-2009, 06:59 PM Re: Php Pagination
Super Talker

Posts: 115
Name: Not Telling
Trades: 0
Can you integrate that into my script? I have no idea where to add that too.
__________________
MY MSN:
Please login or register to view this content. Registration is FREE

PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
sith717 is offline
Reply With Quote
View Public Profile
 
Old 11-03-2009, 12:48 AM Re: Php Pagination
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Try adding it where you want the page links to appear.
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to Php Pagination
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.14518 seconds with 12 queries