lovephp Posted March 5, 2016 Share Posted March 5, 2016 friends if a page number does not exist how do i get the user back to page=1? here is my following code <table class="table" width="100%"> <tbody> <tr class="top nodrop nodrag"> <th class="checkbox" style="width: 12px;"></th> <th>Image/Video</th> <th>Title/Description</th> <th class="action">Action</th> </tr> <?php $allRecords = mysql_query('select * from article'); $total_rows = mysql_num_rows($allRecords); $base_url = 'https://localhost/pagi/'; $per_page = 1; $num_links = 4; $total_rows = $total_rows; $cur_page = 1; if(isset($_GET['page'])) { $cur_page = $_GET['page']; $cur_page = ($cur_page < 1)? 1 : $cur_page; } $offset = ($cur_page-1)*$per_page; $pages = ceil($total_rows/$per_page); $start = (($cur_page - $num_links) > 0) ? ($cur_page - ($num_links - 1)) : 1; $end = (($cur_page + $num_links) < $pages) ? ($cur_page + $num_links) : $pages; $res = mysql_query("SELECT * FROM article LIMIT ".$per_page." OFFSET ".$offset); if(is_resource($allRecords)) { while($row = mysql_fetch_assoc($res)) { $img = $row['image']; $yt = $row['youtube']; if(!empty($img)){ $img = '<img src="../uploads/images/'.$row['image'].'" height="120" width="120" />'; }elseif(!empty($yt)){ $img = ''; }else{ $img = '<img src="../uploads/images/noimg.png" height="120" width="120" />'; } if(!empty($yt)){ $yt = '<img src="../uploads/images/youtube.png" height="120" width="120" />'; }else{ $yt = ''; } ?> <tr> <td class="checkbox"><input type="checkbox" value="<?php echo $row['id'];?>" name="ids[]" class="case"></td> <td><?php echo $img.$yt; ?></td> <td><a href="<?php echo $site_path.$row['url']; ?>" target="_blank"><?php echo ucfirst($row['title']); ?></a><br/><?php echo shortenString($row['article']); ?></td> <td class="action"><a href="post-article.php?id=<?php echo $row['id']; ?>"><img src="media/layout/edit.png" alt="Edit" /></a><br/><a href="javascript:delete_id(<?php echo $row['id']; ?>)"><img src="media/layout/x.png" alt="Delete" /></a></td> </tr> <?php } } ?> <tr> <td> </td> <td class="checkbox" colspan="4"><br/><br/> <select name="action" class="select" style="width:143px;"> <option selected="selected" disabled="disabled">Choose an action</option> <option value="delete">Delete</option> </select> <input type="submit" name="Go" class="submit" value="Apply action"> </td> </tr> </tbody> </table> <div id="pagination"> <div id="pagiCount"> <?php if(isset($pages)) { if($pages > 1) { if($cur_page > $num_links) { $dir = "first"; echo '<span id="prev"> <a href="'.$_SERVER['PHP_SELF'].'?page='.(1).'">'.$dir.'</a> </span>'; } if($cur_page > 1) { $dir = "prev"; echo '<span id="prev"> <a href="'.$_SERVER['PHP_SELF'].'?page='.($cur_page-1).'">'.$dir.'</a> </span>'; } for($x=$start ; $x<=$end ;$x++) { echo ($x == $cur_page) ? '<strong>'.$x.'</strong> ':'<a href="'.$_SERVER['PHP_SELF'].'?page='.$x.'">'.$x.'</a> '; } if($cur_page < $pages ) { $dir = "next"; echo '<span id="next"> <a href="'.$_SERVER['PHP_SELF'].'?page='.($cur_page+1).'">'.$dir.'</a> </span>'; } if($cur_page < ($pages-$num_links) ) { $dir = "last"; echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.$pages.'">'.$dir.'</a> '; } } } ?> thanks in advance appreciate your time Quote Link to comment https://forums.phpfreaks.com/topic/300933-pagination-help-if-page-number-dont-exist/ Share on other sites More sharing options...
QuickOldCar Posted March 5, 2016 Share Posted March 5, 2016 (edited) If is no result mysql query or row count is less than 1...do a header redirect to url with a ?page=1 Don't forget to use exit; Edited March 5, 2016 by QuickOldCar 1 Quote Link to comment https://forums.phpfreaks.com/topic/300933-pagination-help-if-page-number-dont-exist/#findComment-1531704 Share on other sites More sharing options...
lovephp Posted March 5, 2016 Author Share Posted March 5, 2016 is this the right way? $allRecords = mysql_query('select * from article'); $total_rows = mysql_num_rows($allRecords); $base_url = 'https://localhost/pagi/'; $per_page = 1; $num_links = 4; $total_rows = $total_rows; if($total_rows<1){ header("Location:article.php?page=1"); } $cur_page = 1; Quote Link to comment https://forums.phpfreaks.com/topic/300933-pagination-help-if-page-number-dont-exist/#findComment-1531705 Share on other sites More sharing options...
lovephp Posted March 5, 2016 Author Share Posted March 5, 2016 (edited) nope $total_rows = mysql_num_rows($allRecords); //$base_url = 'https://localhost/pagi/'; //Provide location of you index file $per_page = 1; //number of results to shown per page $num_links = 4; // how many links you want to show $total_rows = $total_rows; if(empty($total_rows)){ header("Location:articles.php?page=1"); exit(); } $cur_page = 1; this did not work Edited March 5, 2016 by lovephp Quote Link to comment https://forums.phpfreaks.com/topic/300933-pagination-help-if-page-number-dont-exist/#findComment-1531706 Share on other sites More sharing options...
lovephp Posted March 5, 2016 Author Share Posted March 5, 2016 it just wont do a header redirect i dunno why Quote Link to comment https://forums.phpfreaks.com/topic/300933-pagination-help-if-page-number-dont-exist/#findComment-1531708 Share on other sites More sharing options...
Solution QuickOldCar Posted March 6, 2016 Solution Share Posted March 6, 2016 You would want to do the row count for this query $res = mysql_query("SELECT * FROM article LIMIT ".$per_page." OFFSET ".$offset); The $allrecords is being used to create pagination links all records with no offset or limit. Also check row like this...a number. if(mysql_num_rows($res) < 1){ header("Location: https://localhost/pagi/articles.php"); exit; } Can link back to the main script since you already set page to 1 if not set 1 Quote Link to comment https://forums.phpfreaks.com/topic/300933-pagination-help-if-page-number-dont-exist/#findComment-1531716 Share on other sites More sharing options...
lovephp Posted March 6, 2016 Author Share Posted March 6, 2016 You would want to do the row count for this query $res = mysql_query("SELECT * FROM article LIMIT ".$per_page." OFFSET ".$offset); The $allrecords is being used to create pagination links all records with no offset or limit. Also check row like this...a number. if(mysql_num_rows($res) < 1){ header("Location: https://localhost/pagi/articles.php"); exit; } Can link back to the main script since you already set page to 1 if not set thanks alot bro works like a charm Quote Link to comment https://forums.phpfreaks.com/topic/300933-pagination-help-if-page-number-dont-exist/#findComment-1531731 Share on other sites More sharing options...
benanamen Posted March 6, 2016 Share Posted March 6, 2016 You are using Mysql code that has been removed from Php. You need to use PDO with prepared statements. Here is a really good tutorial (Posted by @Jaques1) https://phpdelusions.net/pdo Quote Link to comment https://forums.phpfreaks.com/topic/300933-pagination-help-if-page-number-dont-exist/#findComment-1531736 Share on other sites More sharing options...
lovephp Posted March 7, 2016 Author Share Posted March 7, 2016 You are using Mysql code that has been removed from Php. You need to use PDO with prepared statements. Here is a really good tutorial (Posted by @Jaques1) https://phpdelusions.net/pdo but me do not like PDO i still use php, will this matter in near future? no still not familiar with PDO though i understand how its done still php is far better for me Quote Link to comment https://forums.phpfreaks.com/topic/300933-pagination-help-if-page-number-dont-exist/#findComment-1531758 Share on other sites More sharing options...
QuickOldCar Posted March 7, 2016 Share Posted March 7, 2016 (edited) PHP is totally different different. PDO is a database wrapper allowing you to connect and manipulate a database. Similar to mysql or mysqli. Mysql functions are deprecated and removed in new versions of php. Edited March 7, 2016 by QuickOldCar 1 Quote Link to comment https://forums.phpfreaks.com/topic/300933-pagination-help-if-page-number-dont-exist/#findComment-1531759 Share on other sites More sharing options...
Jacques1 Posted March 7, 2016 Share Posted March 7, 2016 The old MySQL extension is dead and has been removed from the current PHP version. You had 12(!) years to switch to one of the modern extensions, so, really, it's about time. And it's not only about keeping your code up-to-date. The old extension is notorious for causing SQL injection vulnerabilities, because most programmers simply don't understand the concept the SQL-escaping. PDO fixed that by introducing prepared statements. I understand that PDO looks a bit different, but it's very easy to learn with the above tutorial, and it actually makes a lot of sense. Prepared statements are a major improvement with regards to security. Instead of having to constantly write down the clunky while ($row = mysql_fetch_(...)) pattern, you now now use a simple foreach loop to iterate over rows. PDO works with all mainstream SQL systems, not just MySQL. That means you won't have to re-learn everything if you switch to a different systems (yes, this can happen). 1 Quote Link to comment https://forums.phpfreaks.com/topic/300933-pagination-help-if-page-number-dont-exist/#findComment-1531761 Share on other sites More sharing options...
lovephp Posted March 8, 2016 Author Share Posted March 8, 2016 PHP is totally different different. PDO is a database wrapper allowing you to connect and manipulate a database. Similar to mysql or mysqli. Mysql functions are deprecated and removed in new versions of php. guess ill have to give a serious thought on ;earning PDO Quote Link to comment https://forums.phpfreaks.com/topic/300933-pagination-help-if-page-number-dont-exist/#findComment-1531775 Share on other sites More sharing options...
lovephp Posted March 8, 2016 Author Share Posted March 8, 2016 The old MySQL extension is dead and has been removed from the current PHP version. You had 12(!) years to switch to one of the modern extensions, so, really, it's about time. And it's not only about keeping your code up-to-date. The old extension is notorious for causing SQL injection vulnerabilities, because most programmers simply don't understand the concept the SQL-escaping. PDO fixed that by introducing prepared statements. I understand that PDO looks a bit different, but it's very easy to learn with the above tutorial, and it actually makes a lot of sense. Prepared statements are a major improvement with regards to security. Instead of having to constantly write down the clunky while ($row = mysql_fetch_(...)) pattern, you now now use a simple foreach loop to iterate over rows. PDO works with all mainstream SQL systems, not just MySQL. That means you won't have to re-learn everything if you switch to a different systems (yes, this can happen). will soon get me a book on PDO Quote Link to comment https://forums.phpfreaks.com/topic/300933-pagination-help-if-page-number-dont-exist/#findComment-1531776 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.