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 09-08-2011, 09:36 PM Pagination issue..
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
Hi Big Fish,

I don't know if this is really a big issue (something to worry about), but I would like to know why it's occurring, and possibly a fix for the code below. The issue is if there are no results, the code displays the previous and next buttons and shows two empty pages. Previous 1 2 Next (both pages are empty). It also shows this for pages that have only one result. Currently the most results I have in the index is two results, so maybe that is where the issue is???

The code in question:

PHP Code:
<?php

/* Include your code to connect to DB. */
include('connect.php');

/* Your DB table name */
$tbl_name="amp_ads";

/* How many adjacent pages should be shown on each side? */
$adjacents 1;

/* 
First get total number of rows in data table. 
If you have a WHERE clause in your query, make sure you mirror it here.
*/

$query "SELECT COUNT(*) as num FROM $tbl_name WHERE title LIKE 'd%' LIMIT 1";
$total_pages mysql_fetch_array(mysql_query($query));
$total_pages $total_pages[num];

/* Setup vars for query. */
$targetpage "az.php"/* your file name(the name of this file) */

$limit 1;/* how many items to show per page */

$page $_GET['page'];
if(
$page
$st = ($page 1) * $limit/* first item to display on this page */

else
$st 0/* if no page var is given, set st to 0 */

/* Get data. */
$sql "SELECT title FROM $tbl_name LIMIT $st$limit WHERE title LIKE 'd%' ";
$result mysql_query($sql);

/* Setup page vars for display. */

if ($page == 0$page 1/* if no page var is given, default to 1. */
$prev $page 1;/* previous page is page - 1 */
$next $page 1/* next page is page + 1 */
$lastpage ceil($total_pages/$limit);/* lastpage is = total pages / items per page, rounded up. */
$lpm1 $lastpage 1;/* last page minus 1 */

/*
Now we apply our rules and draw the pagination object. 
We're actually saving the code to a variable in case we want to draw it more than once.
*/

$pagination "";
if(
$lastpage 1)
{
$pagination .= "";

/* previous button */

if ($page 1
$pagination.= "<a href=\"$targetpage?page=$prev\" class=\"current\"> previous </a>";
else
$pagination.= "<span class=\"disabled\"> previous </span>";

/* pages */

if ($lastpage + ($adjacents 2))/* not enough pages to bother breaking it up */
{
for (
$counter 1$counter <= $lastpage$counter++)
{
if (
$counter == $page)
$pagination.= "<span class=\"number\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\" class=\"number current\">$counter</a>";
}
}
elseif(
$lastpage + ($adjacents 2))/* enough pages to hide some */
{

/* close to beginning; only hide later pages */

if($page + ($adjacents 2))
{
for (
$counter 1$counter + ($adjacents 2); $counter++)
{
if (
$counter == $page)
$pagination.= "<span class=\"number\">$counter </span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\" class=\"number current\">$counter</a>";
}
$pagination.= "<span class=\"dots\">...</span>";
$pagination.= "<a href=\"$targetpage?page=$lpm1\" class=\"number\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\" class=\"number current\">$lastpage</a>";
}

/* in middle; hide some front and some back */

elseif($lastpage - ($adjacents 2) > $page && $page > ($adjacents 2))
{
$pagination.= "<a href=\"$targetpage?page=1\" class=\"number\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\" class=\"number\">2</a>";
$pagination.= "<span class=\"dots\">...</span>";
for (
$counter $page $adjacents$counter <= $page $adjacents$counter++)
{
if (
$counter == $page)
$pagination.= "<span class=\"number\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\" class=\"number current\">$counter</ a>";
}
$pagination.= "<span class=\"dots\">...</span>";
$pagination.= "<a href=\"$targetpage?page=$lpm1\" class=\"number\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\" class=\"number current\">$lastpage</a>";
}

/* close to end; only hide early pages */

else
{
$pagination.= "<a href=\"$targetpage?page=1\" class=\"number\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\" class=\"number\">2</a>";
$pagination.= "<span class=\"dots\">...</span>";
for (
$counter $lastpage - (+ ($adjacents 2)); $counter <= $lastpage$counter++)
{
if (
$counter == $page)
$pagination.= "<span class=\"number\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\" class=\"number current\">$counter</a>";
}
}
}

/* next button */

if ($page $counter 1)
$pagination.= "<a href=\"$targetpage?page=$next\" class=\"number current\"> next </a>";
else
$pagination.= "<span class=\"disabled\"> next </span>";
$pagination.= "</div>\n";
}

/* End of Pagination Script */

?>

<!-- Table Output Code -->

<?php

$result 
mysql_query("SELECT * FROM amp_ads WHERE title LIKE 'd%' ") or die(mysql_error());
//echo mysql_num_rows($result);exit;

$numCols 1;
$numPerCol ceil(mysql_num_rows($result) / $numCols);

echo 
"<table valign=\"top\"><tr>";

for(
$col 1$col <= $numCols$col++) {

echo 
"<td valign=\"top\">";

for(
$row 0$row $numPerCol$row++) {

$resultRow mysql_fetch_assoc($result);

echo 
$resultRow[preview];

if(
$resultRow == false) {

break;
}
/*
$f = fopen('http://reunitemysite.com/scripts/amp/show.php?z=1&incl=1&ip='.getenv('REMOTE_ADDR').'&url='.urlencode(getenv('HTTP_HOST').getenv('REQUEST_URI')),'r');
echo stripslashes(fread($f,100000));
fclose($f);
*/

 
}
 echo 
"</td>";
}
echo 
"</tr></table>"

?>

<?=$pagination?> 
</div>
The page in question:
http://www.reunitemysite.com/az.php?letter=d

Hey, even if you could suggest a new pagination code, that would be great! I am just looking for a solution, it doesn't have to be a fix to this pagination code, but that would even be better, but not a requirement, as I had another professional coder do some work to it, and even they had some problems.

Thank you again!
Brian
__________________
Made2Own

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

Last edited by Brian07002; 09-09-2011 at 12:17 PM..
Brian07002 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-09-2011, 03:35 PM Re: Pagination issue..
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
You want it to now show the buttons if the result is empty? Just measure the length of the string returned, and if it is under 2 characters, don't show the buttons.

I also have an amazing pagination code somewhere on my laptop that I didn't create, but is still awesome. I'll post it back here when I get on my laptop later
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 09-09-2011, 08:03 PM Re: Pagination issue..
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
Quote:
Originally Posted by Physicsguy View Post
snip..snip...

Just measure the length of the string returned, and if it is under 2 characters, don't show the buttons.
I don't understand what you mean by that, could you show an example?

Quote:

I also have an amazing pagination code somewhere on my laptop that I didn't create, but is still awesome. I'll post it back here when I get on my laptop later
I'd be very greatful for that script if you could post that as I have really been put to the test with the current pagination code, and have had a love/hate relationship with it as you could probably tell.

Thank you!
Brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 09-10-2011, 12:34 PM Re: Pagination issue..
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
PHP Code:
//Code minified from http://stackoverflow.com/questions/3744794/how-can-i-improve-this-php-pagination-algorithm/3744799#3744799, Thanks!!
    
function generate_pages($total,$current){
    if(
$total>1){
            
$total=intval($total);
            
$output='<div id="pages">Pages:&nbsp;&nbsp;&nbsp;';
            
$current_page=(false==isset($current))?1:$current;
            for(
$page=1;$page<$total+1;$page++){
                
$lower=$current_page-3;
                
$upper=$current_page+3;
                
$special=($page==$current_page)?" style=\"background:#c9c9c9;\"" "";
                if((
$page>$lower&&$page<$upper)||$page<2||$page>($total-1)){
                    if(
$last_done_page+1!=$page)$output.= '... ';
                    
$output.='<a'.$special.' href="?page='.$page.'">'.$page.'</a>';
                    
$last_done_page=$page;
                }
            }
            
$output.='</div>';
            return 
$output;
        }
    } 
Here ya go! Simply use it like this:

echo generate_pages(40,3);

if you had 40 pages in total, and you were on page 3.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 09-10-2011, 04:23 PM Re: Pagination issue..
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
Quote:
Originally Posted by Physicsguy View Post
PHP Code:
//Code minified from http://stackoverflow.com/questions/3744794/how-can-i-improve-this-php-pagination-algorithm/3744799#3744799, Thanks!!
    
function generate_pages($total,$current){
    if(
$total>1){
            
$total=intval($total);
            
$output='<div id="pages">Pages:&nbsp;&nbsp;&nbsp;';
            
$current_page=(false==isset($current))?1:$current;
            for(
$page=1;$page<$total+1;$page++){
                
$lower=$current_page-3;
                
$upper=$current_page+3;
                
$special=($page==$current_page)?" style=\"background:#c9c9c9;\"" "";
                if((
$page>$lower&&$page<$upper)||$page<2||$page>($total-1)){
                    if(
$last_done_page+1!=$page)$output.= '... ';
                    
$output.='<a'.$special.' href="?page='.$page.'">'.$page.'</a>';
                    
$last_done_page=$page;
                }
            }
            
$output.='</div>';
            return 
$output;
        }
    } 
Here ya go! Simply use it like this:

echo generate_pages(40,3);

if you had 40 pages in total, and you were on page 3.
Hi Physicsguy,

I don't think you understood what I meant (and I am not insulting your intelligence at all, in fact, I appreciate your effort very much)...

Let me try to get this correct this time:

I have the $limit set to display one result per page, and there's only one to display in letter 'D' so there should only be one page, correct? So in my situation, I have 2 pages but the same image displaying on that second page, and I am * ABSOLUTELY SURE * that ad is not duplicated. So it should NOT display the previous / Next navigation on this page:

http://reunitemysite.com/az.php?letter=d&page=1

I'm not sure if I mentioned that I was using ad manager pro to get the images for the a-z index so there you have it.

Sorry for the trouble...
Brian
__________________
Made2Own

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

Last edited by Brian07002; 09-10-2011 at 04:49 PM..
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 09-10-2011, 04:47 PM Re: Pagination issue..
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
No problem. I'm an idiot, that's a given. Derp?

Anyway, , I see what you're saying (I hope). I also found that on your site, the 't' page has two ads on one page.

You want one ad to show per page. So that's a LIMIT 1,1 to one right there.
Then you go to the next page. You want to see the second ad there (if there is one), but not the first? Then you'd need to use LIMIT 2, 1 (start at two, show one result).

This has helped me numerous times when trying to get pagination to work. I haven't gone through your code and picked it all apart, but I'm pretty sure that that's your problem, and this could be a solution.

I have, however, noticed that you're using straight-up LIMITS, ie limits with no range. If you supply a second parameter to LIMIT, you can set how many results to show, starting at the first parameter. So if you had 1,2,3,4,5,6,7,8,9,10 in a DB and used LIMIT 2,5, you'd get results 3,4,5,6,7.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 09-13-2011, 07:32 PM Re: Pagination issue..
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
I think I can say I actually gave up trying to get this on my own. If someone has some time to take on this challenge, please do so I am curious to find out what my lacks are in understanding php programming.

To sum this up, here's what I have done:

1. I have read *alot* of tutorials online / offline (php bible) believe me I'm different
2. Asked many times on different forums
3. I tried modifying the code myself
4. Even had the owner of the ad manager pro script try his luck modifying the code

All these options (except #4) didn't turn out successful. Option #4 worked to an extent, but not 100%. Don't worry, I won't explain it in detail, just enough, and show you the resulting page.

Ok, Once again from the top...I am trying to produce an Alpha(betical) Nav(igation) -- Alphanav with pagination, which will display ads from code generated by ad manager pro.

The layout would look something like:

0-9 | A B C D E F G H I J K L M N O P Q R S T U V W X Y Z | Show All ads

The 0-9 would display all ads beginning with a number. 0 - 9 (only)
The A-Z would display ads based on the letter clicked A-Z (only)
The Show all ads would display all ads.

Note: There will be no ads displayed when the navigation is initially displayed ex: before any link (letter / number) has been clicked.

Ok, option #4 explanation:
Here's the page in question: http://reunitemysite.com/az.php

Have a look at that page , and choose either C D or T as those letters have ads in them, and sorted correctly. The thing is, on C OR D there's only ONE ad, and the pagination shows two pages, limit is set to 1 per page btw. But for T, there's two ads. Why does the pagination work dependently as in for T there are two ads, and C and D only 1 each, but all three pages have two pages?

Hope you understand, and can fix that issue.

Thanks again, in advance!
Brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 10-04-2011, 02:29 AM Re: Pagination issue..
Skilled Talker

Posts: 96
Name: Joan
Trades: 0
Hello,

this is from the php class i took maybe this would help.

PHP Code:
// Number of records to show per page:
$display 10;

// Determine how many pages there are...
if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined.
    
$pages $_GET['p'];
} else { 
// Need to determine.
     // Count the number of records:
    
$q "SELECT COUNT(user_id) FROM users";
    
$r = @mysqli_query ($dbc$q);
    
$row = @mysqli_fetch_array ($rMYSQLI_NUM);
    
$records $row[0];
    
// Calculate the number of pages...
    
if ($records $display) { // More than 1 page.
        
$pages ceil ($records/$display);
    } else {
        
$pages 1;
    }
// End of p IF.

// Determine where in the database to start returning results...
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
    
$start $_GET['s'];
} else {
    
$start 0;
}

// Determine the sort...
// Default is by registration date.
$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'rd';

// Determine the sorting order:
switch ($sort) {
    case 
'ln':
        
$order_by 'last_name ASC';
        break;
    case 
'fn':
        
$order_by 'first_name ASC';
        break;
    case 
'rd':
        
$order_by 'registration_date ASC';
        break;
    default:
        
$order_by 'registration_date ASC';
        
$sort 'rd';
        break;
}
    
// Make the query:
$q "SELECT last_name, first_name, DATE_FORMAT(registration_date, '%M %d, %Y') AS dr, user_id FROM users ORDER BY $order_by LIMIT $start$display";        
$r = @mysqli_query ($dbc$q); // Run the query.

// Table header:
echo '<table align="center" cellspacing="0" cellpadding="5" width="75%">
<tr>
    <td align="left"><b>Edit</b></td>
    <td align="left"><b>Delete</b></td>
    <td align="left"><b><a href="view_users.php?sort=ln">Last Name</a></b></td>
    <td align="left"><b><a href="view_users.php?sort=fn">First Name</a></b></td>
    <td align="left"><b><a href="view_users.php?sort=rd">Date Registered</a></b></td>
</tr>
'
;

// Fetch and print all the records....
$bg '#eeeeee'
while (
$row mysqli_fetch_array($rMYSQLI_ASSOC)) {
    
$bg = ($bg=='#eeeeee' '#ffffff' '#eeeeee');
        echo 
'<tr bgcolor="' $bg '">
        <td align="left"><a href="edit_user.php?id=' 
$row['user_id'] . '">Edit</a></td>
        <td align="left"><a href="delete_user.php?id=' 
$row['user_id'] . '">Delete</a></td>
        <td align="left">' 
$row['last_name'] . '</td>
        <td align="left">' 
$row['first_name'] . '</td>
        <td align="left">' 
$row['dr'] . '</td>
    </tr>
    '
;
// End of WHILE loop.

echo '</table>';
mysqli_free_result ($r);
mysqli_close($dbc);

// Make the links to other pages, if necessary.
if ($pages 1) {
    
    echo 
'<br /><p>';
    
$current_page = ($start/$display) + 1;
    
    
// If it's not the first page, make a Previous button:
    
if ($current_page != 1) {
        echo 
'<a href="view_users.php?s=' . ($start $display) . '&p=' $pages '&sort=' $sort '">Previous</a> ';
    }
    
    
// Make all the numbered pages:
    
for ($i 1$i <= $pages$i++) {
        if (
$i != $current_page) {
            echo 
'<a href="view_users.php?s=' . (($display * ($i 1))) . '&p=' $pages '&sort=' $sort '">' $i '</a> ';
        } else {
            echo 
$i ' ';
        }
    } 
// End of FOR loop.
    
    // If it's not the last page, make a Next button:
    
if ($current_page != $pages) {
        echo 
'<a href="view_users.php?s=' . ($start $display) . '&p=' $pages '&sort=' $sort '">Next</a>';
    }
    
    echo 
'</p>'// Close the paragraph.
    
// End of links section. 
stivens is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Pagination issue..
 

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.08946 seconds with 12 queries