programming.name Posted June 16, 2010 Share Posted June 16, 2010 Hi, please consider the following code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php function get_current_page() { $result = explode('?', $_SERVER['REQUEST_URI']); $str = $result[0]; $result = explode('/' ,$str); return $result[2]; } function get_prev_page() { return get_lid() - get_per_page(); } function get_next_page() { return get_lid() + get_per_page(); } function get_lid() { if(empty($_GET['lid'])) { return 0; } else { return $_GET['lid']; } } function get_per_page() { return 10; } function show_content() { $lid = get_lid(); $db_conn = new MySQLi('localhost', 'root', '', 'db'); $thumbnail = $db_conn->query("SELECT * FROM link ORDER BY no DESC LIMIT " . get_lid() .', 1'); while($row = $thumbnail->fetch_assoc()) { $link = $row['content']; echo $link . '<br />'; } } function show_link() { $lid = get_lid(); $db_conn = new MySQLi('localhost', 'root', '', 'db'); $thumbnail = $db_conn->query("SELECT * FROM link ORDER BY no DESC LIMIT " . get_lid() .', '. get_per_page()); echo '<a href="'. '?lid=' . get_prev_page() . '">' . 'Prev' .'</a> | '; //the condition that index gets negative values were NOT considered for simplicity of code; I have already done it in my real coding while($row = $thumbnail->fetch_assoc()) { $link = $row['no']; echo '<a href="'. '?lid=' . $lid++ . '">' . $lid .'</a> | '; } echo '<a href="'. '?lid=' . get_next_page() . '">' . 'Next' .'</a> | '; //the condition that index gets over max num_rows wwere NOT considered for simplicity of code; I have already done it in my real coding } ?> <?php get_next_page(); show_content(); ?> <p> </p> <?php show_link(); ?> </body> </html> And the DB structure is like so: DB name: db table name: link ---------------------------------------- no | content | ---------------------------------------- 1 | content 1 | ---------------------------------------- 2 | content 2 | ---------------------------------------- 3 | content 3 | ---------------------------------------- . . . ---------------------------------------- 30 | content 30 | ---------------------------------------- As you can see this code performs simple pagination. But it has one little problem. I want the link to be stable until I click the next or prev button. I mean if I click any link, the link just clicked becomes the first link. For example, on initial page load, the page might appear like this: -------------------------------- content 1 prev 1 2 3 4 5 6 7 8 9 10 next -------------------------------- But when I click on '3' the link changes as follows: ---------------------------------------- content 3 prev 3 4 5 6 7 8 9 10 11 12 13next ---------------------------------------- Whichever links are clicked I want these links stayed and moved by 10 when the prev or next button is clicked. Please HELP!! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/204938-question-about-pagination/ Share on other sites More sharing options...
katierosy Posted June 18, 2010 Share Posted June 18, 2010 If you will read number of pagination scripts from web, you will be helped to do it the way you want. Quote Link to comment https://forums.phpfreaks.com/topic/204938-question-about-pagination/#findComment-1074089 Share on other sites More sharing options...
Psycho Posted June 18, 2010 Share Posted June 18, 2010 I'm having a difficult time really understanding your request. It would have been helpful if you also showed what you expected the result to be when the user selects "3" instead of just showing what you are getting. I assume you want the result to be such that the selected page is indicated in some manneer (e.g. bold) and that the avaialble pages - up to 10 - are displayed to the left and the right of the selected page. That page has some other problems with inefficiency and other things (e.g. a function to return a static variable?!). Here is some code based upon what I think you want along with other improvements. This is NOT tested. The logic should be good, but I didn't go to the trouble of create a database table for this. <?php //Config variables $records_per_page = 10; $max_page_links = 10; //Connect to DB $db_conn = new MySQLi('localhost', 'root', '', 'db'); //Determine total records and total pages $query = "SELECT COUNT(`content`) as total FROM `link`"; $result = $db_conn->query($query); $row = $result->fetch_assoc(); $total_records = $row['total']; $total_pages = (ceil($total_records/$records_per_page)); //Determine the current page $page = (isset($_GET['page'])) ? (int) $_GET['page'] : 1; if($page<1 || $page>$total_pages) { $page = 1; } //Detemine the start for the limit in query $limit_start = ($page - 1) * $records_per_page; //Get & display data for currently selected page $query = "SELECT `content` FROM `link` ORDER BY `no` DESC LIMIT {$limit_start}, {$records_per_page}"; $thumbnail = $db_conn->query($query); while($row = $thumbnail->fetch_assoc()) { $page_output = "{$row['content']}<br />\n"; } //Create array of the navigation links //--------------------------- $nav_links_ary = array(); $first_page_link = max(1, $page-$max_page_links); $last_page_link = min($total_pages, $max_page_links); //Add page links to array if($page>1) { $nav_links_ary[] = "<a href=\"?page=" . ($page-1) . ">Prev</a>"; } for ($pg=$first_page_link; $pg<$last_page_link; $pg++) { $nav_links_ary[] = "<a href=\"?page={$pg}\">{$pg}</a>"; } if($page<$total_pages) { $nav_links_ary[] = "<a href=\"?page=" . ($page+1) . ">Next</a>"; } //Create final output for navigation links $nav_links = implode(' | ', $nav_links_ary); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php echo $page_output; ?> <p> </p> <?php $nav_links; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/204938-question-about-pagination/#findComment-1074141 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.