sasori Posted April 24, 2009 Share Posted April 24, 2009 help, im trying to learn pagination using native php. i think my code is correct by the browser keeps saying i have an invalid mysql resource. here's my code require('./includes/mysql_connect.php'); $display = 5; if(isset($_GET['np'])){ $num_pages = $_GET['np']; }else{ $query = "SELECT COUNT(*) FROM users ORDER BY registration_date ASC"; $result = mysql_query($query); $row = mysql_fetch_array($result, MYSQL_NUM); $num_records = $row[0]; } if($num_records > $display){ $num_pages = ceil($num_records/$display); }else{ $num_pages = 1; } if(isset($_GET[s])){ $start = $_GET[s]; }else{ $start = 0; } $query = "SELECT first_name, last_name, email FROM users ORDER BY registration_date ASC LIMIT $start,$display"; $result = mysql_query($query); $num = mysql_num_rows($result); if($num > 0); echo '<table>'; echo '<tr> <tr><td><b>First</b></td> <td><b>Last</b></td> <td><b>Email</b></td> </tr>'; while($row = mysql_fetch_array($result,MYSQL_ASSOC)){ echo '<tr>'; echo '<td>'.$row[first_name].'</td>'; echo '<td>'.$row[last_name].'</td>'; echo '<td>'.$row[email].'</td>'; echo '</tr>'; } echo '</table>'; mysql_close(); if($num_pages > 1){ $current_page = ($start/$display) + 1; if($current_page != 1){ echo '<a href="test.php?s=' .($start - $display) .'&np='. $num_pages.'">Previous</a>'; } } for($i=1; $i <= $num_pages; $i++){ if($i != $current_page){ echo '<a href="test.php?s"'. (($display * ($i - 1))) . '&np=' . $num_pages . '">' . $i. '</a>'; }else{ echo $i. ''; } } please tell me what is wrong and why does it says wrong. Link to comment https://forums.phpfreaks.com/topic/155453-solved-pagination-help/ Share on other sites More sharing options...
mikesta707 Posted April 24, 2009 Share Posted April 24, 2009 EDIT: NVM read your post wrong Link to comment https://forums.phpfreaks.com/topic/155453-solved-pagination-help/#findComment-818015 Share on other sites More sharing options...
sasori Posted April 24, 2009 Author Share Posted April 24, 2009 EDIT: nevermind read your post wrong you EDIT: nevermind read your post wrong yours is wrong..the problem is here $query = "SELECT first_name, last_name, email FROM users ORDER BY registration_date ASC LIMIT $start,$display"; if i will remove the $start and $display variable, the error is gone..but the output is useless, the main purpose of the LIMIT, is for pagination Link to comment https://forums.phpfreaks.com/topic/155453-solved-pagination-help/#findComment-818016 Share on other sites More sharing options...
.josh Posted April 24, 2009 Share Posted April 24, 2009 did you echo $start to see what it's holding? Link to comment https://forums.phpfreaks.com/topic/155453-solved-pagination-help/#findComment-818026 Share on other sites More sharing options...
sasori Posted April 24, 2009 Author Share Posted April 24, 2009 did you echo $start to see what it's holding? i forgot to add single quotes on $_GET['s'] , but it doesn't change anything..there is still error Link to comment https://forums.phpfreaks.com/topic/155453-solved-pagination-help/#findComment-818029 Share on other sites More sharing options...
alphanumetrix Posted April 24, 2009 Share Posted April 24, 2009 You don't need the single quotes. It'll work with/without/and with double quotes. As for the code, you need to echo out your $start to see what you're getting. At first glance, I don't think that algorithim of your's is going to work properly. Try this one instead: if($num_pages > 1){ $current_page = $start * $display; if($current_page != 1){ echo '<a href="test.php?s=' .$start.'&np='. $num_pages.'">Previous</a>'; } } I am really tired right now, but your algorithim looked like it would display a decimal if let alone, so that *would* be an invalid mysql resource if I'm right. As for the rest of your code, I'm too tired to even look at it... Link to comment https://forums.phpfreaks.com/topic/155453-solved-pagination-help/#findComment-818040 Share on other sites More sharing options...
.josh Posted April 24, 2009 Share Posted April 24, 2009 did you echo $start to see what it's holding? i forgot to add single quotes on $_GET['s'] , but it doesn't change anything..there is still error I'm sorry, what I meant to say was, did you echo $start to see what it's holding? I understand how one could have been confused by what I said before. Link to comment https://forums.phpfreaks.com/topic/155453-solved-pagination-help/#findComment-818046 Share on other sites More sharing options...
sasori Posted April 28, 2009 Author Share Posted April 28, 2009 did you echo $start to see what it's holding? i forgot to add single quotes on $_GET['s'] , but it doesn't change anything..there is still error I'm sorry, what I meant to say was, did you echo $start to see what it's holding? I understand how one could have been confused by what I said before. Hi Mr. Crayon Violent, I was googling for an easier pagination tutorial.. I found yours in PHPfreaks. but I have a problem, the "<<" and "<" links aren't working, here's the link to my live practice page http://practice.uuuq.com and here's the code (it's not a full copy paste, I made sure I understand what you are talking about in the tutorial) $sql = "SELECT COUNT(*) FROM users"; $result = mysql_query($sql) or die (mysql_error()); $r = mysql_fetch_row($result); $numrows = $r[0]; //rows per page $rowsperpage = 5; $totalpages = ceil($numrows/$rowsperpage); //check if current page is set or set a default if(isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])){ $currentpage = (int)$_GET['currentpage']; }else{ $currentpage = 1; } //if current page is greater than total pages if($currentpage > $totalpages){ $currentpage = $totalpages; } //if current page is less than the total page if($currentpage < 1){ $currentpage = 1; } //set offset $offset = ($currentpage - 1) * $rowsperpage; //select datas $sql = "SELECT first_name, last_name, email, date_format(registration_date,'%M %e, %Y') as date FROM users ORDER BY first_name ASC LIMIT $offset, $rowsperpage"; $result = mysql_query($sql) or die (mysql_error()); while($row = mysql_fetch_assoc($result)){ echo $row[first_name]." : ".$row[last_name]." : ".$row[email]." : ".$row[date]."<br />"; } //set pagination links if($currentpage > 1 ){ echo "<a href='{$_SERVER['PHP_SELF']}currentpage=1'><<</a>"; $prevpage = $currentpage - 1; echo "<a href='{$_SERVER['PHP_SELF']}currentpage=$prevpage'><</a>"; } //range num links to show $range = 3; //numlinks for($x =($currentpage - $range); $x < (($currentpage + $range) + 1); $x++){ //check if valid page if($x > 0 && $x <= $totalpages){ ///check if the iteration is on the current page if($x == $currentpage){ echo "[<b>$x</b>]"; }else{ echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a>"; } } } //if not on last page if($currentpage != $totalpages){ $nextpage = $currentpage + 1; echo "<a href='{$_SERVER['PHP_SELF']}currentpage=$nextpage'>></a>"; //echo forward link to last page echo "<a href='{$_SERVER['PHP_SELF']}currentpage=$totalpages'>>></a>"; } Link to comment https://forums.phpfreaks.com/topic/155453-solved-pagination-help/#findComment-820778 Share on other sites More sharing options...
.josh Posted April 28, 2009 Share Posted April 28, 2009 you forgot the ? in the query string for the links. Link to comment https://forums.phpfreaks.com/topic/155453-solved-pagination-help/#findComment-820779 Share on other sites More sharing options...
sasori Posted April 28, 2009 Author Share Posted April 28, 2009 you forgot the ? in the query string for the links. haha..ok great..its now working sir. what a very nice tutorial!!!.. two thumbs up..it helped me alot.. problem solved.. Link to comment https://forums.phpfreaks.com/topic/155453-solved-pagination-help/#findComment-820781 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.