Jump to content
Larry Ullman's Book Forums

Search the Community

Showing results for tags 'pagination'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • Single Editions
    • Modern Javascript: Develop and Design
    • The Yii Book
    • Effortless Flex 4 Development
    • Building a Web Site with Ajax: Visual QuickProject
    • Ruby: Visual QuickStart Guide
    • C++ Programming: Visual QuickStart Guide
    • C Programming: Visual QuickStart Guide
    • Adobe AIR: Visual QuickPro Guide
  • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (5th Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (4th Edition)
    • PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide (3rd Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (1st Edition)
  • PHP for the Web: Visual QuickStart Guide
    • PHP for the Web: Visual QuickStart Guide (5th Edition)
    • PHP for the Web: Visual QuickStart Guide (4th Edition)
    • PHP for the Web: Visual QuickStart Guide (3rd Edition)
    • PHP for the World Wide Web: Visual QuickStart Guide (2nd Edition)
    • PHP for the World Wide Web: Visual QuickStart Guide (1st Edition)
  • Effortless E-commerce with PHP and MySQL
    • Effortless E-Commerce with PHP and MySQL (2nd Edition)
    • Effortless E-Commerce with PHP and MySQL
  • PHP Advanced: Visual QuickPro Guide
    • PHP Advanced and Object-Oriented Programming: Visual QuickPro Guide (3rd Edition)
    • PHP 5 Advanced: Visual QuickPro Guide (2nd Edition)
    • PHP Advanced: Visual QuickPro Guide
  • MySQL: Visual QuickStart Guide
    • MySQL: Visual QuickStart Guide (2nd Edition)
    • MySQL: Visual QuickStart Guide (1st Edition)
  • Other
    • Announcements
    • Newsletter, Blog, and Other Topics
    • Forum Issues
    • Social

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Found 10 results

  1. Hello all, I am busy with a complete user management script and have the following questions: Is it good practice to combine the "view users" and "search users" scripts in the same PHP file? How do I amend the "SELECT COUNT(user_id) FROM users" query for pagination purposes to accommodate the search functionality? In its current form it continues to displays ALL the pagination links after a search query. Is there a more efficient way to formulate the following search query: "SELECT u.user_id, u.username, u.type, c.country, u.email, u.status, DATE_FORMAT($date_created, '%Y-%m-%d %H:%i:%s') AS dc FROM users AS u INNER JOIN countries AS c USING (country_id) WHERE lang_id = {$_SESSION['lid']} AND u.username LIKE '%" . $terms[0] . "%' OR u.type LIKE '%" . $terms[0] . "%' OR c.country LIKE '%" . $terms[0] . "%' OR u.email LIKE '%" . $terms[0] . "%' OR u.status LIKE '%" . $terms[0] . "%' OR date_created LIKE '%" . $terms[0] . "%' ORDER BY $order_by LIMIT $start, $display" I have searched online but could not find any workable solutions. Any help will as always be much appreciated.
  2. I am building a forum from the example in the book, and I am having trouble getting the page numbers to work like they should, right now the thread results don't update when you click on other page numbers, it is always the same query results on every page. Here is some code where I think the problem is, but I can't find it. I would post the whole page's code, but I have done that before and didn't get many helpers for that. The query I am using is $sql = "SELECT t.thread_id, t.subject, u.username, COUNT(p.post_id) - 1 AS responses, MAX(DATE_FORMAT($last, '%e-%b-%y %l:%i %p')) AS last, MIN(DATE_FORMAT($first, '%e-%b-%y %l:%i %p')) AS first, w.forum_id FROM threads AS t INNER JOIN posts AS p USING (thread_id) INNER JOIN forums AS w ON t.forum_id = w.forum_id INNER JOIN users AS u ON t.user_id = u.user_id WHERE w.forum_id=$forumid AND t.lang_id = {$_SESSION['lid']} GROUP BY (p.thread_id) ORDER BY last DESC"; $query = mysqli_query ($dbc, $sql); At the top of my forum.php page I am using this code to identify which forum the threads belong to. if (isset($_GET['id']) && is_numeric($_GET['id'])){ $forumid = $_GET['id']; } elseif (isset($_POST['id']) && is_numeric($_POST['id'])) { $forumid = $_POST['id']; } else { echo '<p class="error">This page has been accessed in error.</p>'; include ('includes/footer.html'); exit(); } Here are the page links, modified to include the forum_id in the url. if ($pages > 1){ echo '<br /><p class="center4">'; $current_page = ($start/$display) + 1; // If it's not the first page, make a Previous button: if ($current_page != 1) { echo '<a href="forum.php?s=' . ($start - $display) . '&p=' . $pages . '&id='.$forumid.'">Previous</a> '; } // Make all the numbered pages: for ($i = 1; $i <= $pages; $i++) { if ($i != $current_page) { echo '<a href="forum.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '&id='.$forumid.'">' . $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="forum.php?s=' . ($start + $display) . '&p=' . $pages . '&id='.$forumid.'">Next</a>'; } echo '</p>'; // Close the paragraph. } // End of links section. thanks for taking a look.
  3. i have went thru this script forward and backwards a million and still have problems, i am not getting any error msgs, i am completely lost. please help. <?php # Script 10.4 - view_users.php #4 // this script retrieves all the records from the user table // the new version paginates the query results $page_title = 'View the Current Users'; include ('include/header.html'); echo '<h1>Registered Users</h1>'; require_once ('mysqli_connect.php'); // 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 be determined //count the number records in the database $q = "SELECT COUNT(user_id) FROM users_info"; $r = @mysqli_query ($dbc, $q); // $row = @mysqli_fetch_array ($r, MYSQLI_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; } //Define the query: $q = "SELECT last_name, first_name, DATE_FORMAT(time, '%M %d, %Y') AS dr, user_id FROM users_info ORDER BY time ASC LIMIT $start, $display"; $r = @mysqli_query ($dbc, $q); // // 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>Last Name</b></td> <td align="left"><b>First Name</b></td> <td align="left"><b>Date Registered</b></td> </tr> '; // Fetch and print all the records..... $bg = '#eeeeee'; // set the initial background color while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color. 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) { // add some spacing and start a paragraph: echo '<br /><p>'; // Determine what page the script is on $current_page = ($start/$display) + 1; // If it's not the first page, make a Previous link: if ($current_page != 1) { echo '<a href="view_users.php?s=' . ($start - $display) '&p=' . $pages . '">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 . '">' . $i . '</a> '; } else { echo $1 . ' '; } } // End of FOR loop. // If it's not the last page, make a Next botton: if ($current_page != $pages) { echo '<a href="view_users.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>'; } echo '</p>'; // close the paragraph. } // End of links section. include ('include/footer.html'); ?>
  4. Hi, I am having problems with pagination. I thought I had script 9.4 working without any problems, so carried on with script 9.5, which I also thought was working OK. I then added another table to my online database which I accessed by a script based on 9.5, but then I found the pagination wasn't working. I then found the pagination in script 9.5 wasn't working. The s and p appear in the url address line, and it would appear the code: if (isset($_GET['s']) && is_numeric($_GET['s'])) { $start = $_GET['s']; } else { $start = 0; } is ignored, as start always stays at 0 (or whatever value I put as $start = ). I've checked and double checked but can't see where the problem is. The full code is included here: ------------------------------------- <?php # Script 9.5 - view_users95.php // This script retrieves all the records from the users table. // This version allows the results to be sorted in different ways. // This version paginates the query results. $page_title = 'View the Current Users'; include ('includes/header8.html'); // Page header: echo '<h1>Registered Users 5</h1>'; require_once ('../mysqli_connect.php'); // Connect to the db. // Number of records to show per page: $display = 5; // 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 ($r, MYSQLI_NUM); $records = $row[0]; // calculate the number of pages... if ($records > $display) { // More than 1 page. $pages = ceil ($records/$display); } else { $pages = 2; } }// 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, '%d %M, %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 border="5" align="center" cellspacing="0" cellpadding="5" width="50%"> <thead><tr> <td align="left"><b>Edit</b></td> <td align="left"><b>Delete</b></td> <td align="left"><b><a href="view_users95.php?sort=ln">Last Name</a></b></td> <td align="left"><b><a href="view_users95.php?sort=fn">First Name</a></b></td> <td align="left"><b><a href="view_users95.php?sort=rd">Date Registered</a></b></td> </tr> </thead>'; // Fetch and print all the records: $bg = '#eeeeee'; // Set the initial background colour. while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { //$bg = ($bg=='#bbbbbb' ? '#ffffff' : '#bbbbbb'); // Switch the background colour, using the Ternary operator. echo '<tr bgcolor="' . $bg . '"> <td align="left"><a href="edit_user93pw.php?id=' . $row['user_id'] . '">Edit</a></td> <td align="left"><a href="delete_user92pw.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>'; // Close the table. mysqli_free_result ($r); // Free up the resources. mysqli_close($dbc); // Close the database connection. //Make the links to other pages, if necessary if ($pages > 1) { // Add some spacing and start a paragraph echo '<br /><p>'; // Determine what page the script is on: $current_page = ($start/$display) + 1; // If it's not the first page, make a previous button: if ($current_page != 1) { echo '<a href="view_users95.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_users95.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_users95.php?s=' . ($start + $display) . ';p=' . $pages . ';sort=' . $sort . '">Next</a> '; } echo '</p>'; // close the paragraph } // end of the links section echo "<p>Users = $records.</p>\n"; // with this line in the pagination doesn't work echo "<p>Pages = $pages.</p>\n"; echo '<br />'; include ('includes/footerx.html'); ?> ----------------------------------- I hope you can shed some light on this problem Thanks Chris
  5. Hi everyone, I'm on chapter 10 right now, I want to add some style to the pagination using <ul> and <li>, i'm trying this way... if ($pages > 1) { echo '<ul class="pagination margin-none pull-right"><li>'; $current_page = ($start/$display) + 1; if($current_page != 1){ echo '<a href="view_users.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a> '; } for($i = 1; $i <= $pages; $i++){ if($i != $current_page){ echo '<a href="view_users.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> '; } else { echo $i . ' '; } } if($current_page != $pages){ echo '<a href="view_users.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>'; } echo '</li></ul>'; } this is how i see it how can i solve this? thanks!
  6. Following the book I have successfully used pagination. It looks great! Now however, Im getting to the point where I have multiple rows of page numbers. What could I start to look at so instead of this, < 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39> I could get this, < 1... 8 9 10 11 12 13 ...39 > Thanks!
  7. Hello all. I am a relative newbie to php and mysql but have been progressing through book with great results up until i have hit a wall with a pagination script. The scripts used are a combination of 9.4 - paginate view users and chapter 17 ecommerce from the PHP 6 and MySQL 5 book. The issue involves the script not creating further links for additional pages should the results returned exceed the $display variable. I don't get any next or previous links at all and for the life of me cannot see why. The page displays with whatever the $display number is set out but no links to additional results. My code is below but the site is local so cannot show you direct links. <?php # browse_books.php // This page displays the available books (products). // Set the page title and include the HTML header: $page_title = 'Browse Books'; include ('includes/header.html'); require_once ('../mysqli_connect.php'); // Number of records to show per page: $display = 15; // 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 (book_id) FROM books"; $r = @mysqli_query ($dbc, $q); $row = @mysqli_fetch_array ($r, MYSQLI_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; } // Default query for this page: $q = "SELECT authors.author_id, CONCAT_WS(' ', first_name, middle_name, last_name) AS author, book_name, price, description, book_id FROM authors, books WHERE authors.author_id = books.author_id ORDER BY authors.last_name ASC, books.book_name ASC LIMIT $start, $display"; // Are we looking at a particular author? if (isset($_GET['aid']) && is_numeric($_GET['aid']) ) { $aid = (int) $_GET['aid']; if ($aid > 0) { // Overwrite the query: $q = "SELECT authors.author_id, CONCAT_WS(' ', first_name, middle_name, last_name) AS author, book_name, price, description, book_id FROM authors, books WHERE authors.author_id = books.author_id AND books.author_id = $aid ORDER BY books.book_name"; } } // Display all the books, linked to URLs: $r = mysqli_query ($dbc, $q); while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) { // Display each record: echo "<div class=\"book_holder\"> <h1><a href=\"view_book.php?pid={$row['book_id']}\">{$row['book_name']}</a></h1> <h2><a href=\"browse_books.php?aid={$row['author_id']}\">{$row['author']}</a></h2> <p>"; $newstring = substr("{$row['description']}",0,100); echo $newstring; echo "....<a href=\"view_book.php?pid={$row['book_id']}\"> more info</a>"; echo "</p> <h2>£{$row['price']}</h2> </div>"; } // End of while loop. mysqli_free_result ($r); mysqli_close($dbc); // Make the links to other pages, if necessary. if ($pages > 1) { // Add some spacing and start a paragraph: echo '<br /><p>'; // Determine what page the script is on: $current_page = ($start/$display) + 1; // If it's not the first page, make a Previous button: if ($current_page != 1) { echo '<a href="browse_books.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a> '; } // Make all the numbered pages: for ($i = 1; $i <= $pages; $i++) { if ($i != $current_page) { echo '<a href="browse_books.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $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="browse_books.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>'; } echo '</p>'; // Close the paragraph. } // End of links section. include ('includes/footer.html'); ?> Thanks in advance
  8. I am practisng how to create pagination in chapter 10 - page: 317 and I want to add class to the current page so I can style it using CSS. When I try to add a class, or use float in CSS, I run into a problem. Can you help me solve this problem? Excerpt of the colde: // Make the links to other pages: if($pages > 1) { echo ' <p id="pagination"> '; $current_page = ($start/$display) + 1 ; // If it is not the first page, make a previous link: 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 is 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>'; }// End of links section. ===================================== Thank you Ghamdan
  9. mint

    Pagination

    There is a for loop which iterates and displays some links, the number of links depends on a database value which lies in range of 100's, is there any way to paginate the links to display 10 links one at a time through pagination. Here is the code foreach ($records as $record): echo CHtml::link('Transaction ID #'.$record->RBT_Transaction_ID,array('mobdistinct','mobtrans'=>$record->RBT_Transaction_ID)); ?> <br> <?php endforeach; ?>
  10. Hi Larry and forum users. I have a problem that I can't resolve. I have set up my pagination script to display 5 blog posts per page. (I'm working on a local server at present so unfortunately I can't provide a link to the page). Currently, there are 15 blog posts in my db. Obviously, the pagination numbers at the bottom of my page should show three digits, '1', '2', and '3', with each page showing 5 posts. The thing is, I see four digits: '1', '2', '3'... and '4'. When I click '1', I see the first 5 records and "index.php?s=0&p=4" is displayed in my url. Clicking '2' shows me records 6 to 10 ("index.php?s=5&p=4" displays in the url), '3' shows 11 to 15 ("index.php?s=10&p=4" displays in the url)... and clicking '4' takes me to a page displaying no posts at all. When I add a sixteenth post, no problem. Clicking '4' shows a page with 1 post, as should be the case, and my URL shows "index.php?s=15&p=4"). So, in summary, the problem seems to arise when the number of records is evenly divisible by the number of records held in the $display variable. When the total number of records is not evenly divisible, the problem resolves itself. Can anyone figure out what might be wrong? I'm sure it's something small... I'd really appreciate your help. My code is below (most of which is directly from your book, Larry, on pp 318, 319): $display = 5; // determine the number of pages if (isset($_GET['p']) && is_numeric ($_GET['p'])) { // already determined $pages = $_GET['p']; } else { // must be determined $q = " SELECT COUNT(post_id) FROM lfbp_blog "; $r = @mysqli_query ($dbc, $q); // Run the query. $row = mysqli_fetch_array ($r, MYSQLI_NUM); $total_records = $row[0]; // Calculate the number of pages that will be required to display all records if ($total_records > $display) { // means that more than 1 page will be required $pages = ceil($total_records/$display); // ceil function returns division result rounded upwards to nearest integer } else { $pages = 1; } } // end if // Determine where in the db to start returning results if (isset($_GET['s']) && is_numeric($_GET['s'])) { $start = $_GET['s']; } else { $start = 0; } // start of news // find blog posts to display get_blog_posts(); // end of news--> // show links to other pages, if necessary if ($pages > 1) { echo '<p>'; $current_page = ($start/$display) + 1; // if it's not the first page, make a Previous button if ($current_page !=1) { echo '<a href="index.php?s=' . ($start - $display) . '&p=' . $pages . '">Newer posts</a> '; } // Make all the numbered pages for ($i = 1; $i <= $pages; $i++) { if ($i != $current_page) { echo '<a href="index.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $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="index.php?s=' . ($start + $display) . '&p=' . $pages . '">Older posts</a>'; } echo '</p>'; // Close the paragraph on the pagination line of content } // End of the links section ?>
×
×
  • Create New...