jefffan24 Posted October 25, 2009 Share Posted October 25, 2009 So I'm making a feedback sort of section on a website with MySQL and PHP, I've gotten the script to do everything I want except for this. I want the script to only show 5 at a time (which i have accomplished no problem), but I'm also trying to make it so that when the user clicks a button it will show the next 5 records in the database so they can view all records, but only 5 at a time. (I hope this makes sense). So I need the button to add 5 to my $start variable, and then re-run my for loop. $start = 0; echo $start . "<br /><br />"; $result2 = mysql_query("SELECT * FROM testtable LIMIT $start,5") or die(mysql_error()); $row2 = mysql_fetch_array($result2); for($counter = 1; $counter <= 5; $counter += 1) { echo $row2['ID'] . "<br />"; echo " Subject:<b><font color='red'> " . $row2['Subject'] . "</font></b>"; echo "<br />Message: <b>" . $row2['Message'] . "</b><br /><br />"; $row2 = mysql_fetch_array($result2); } mysql_close($con) ?> <br /> <form> <input type="button" value="submit" onclick="<?php $start += 5; ?>" /> </form> <?php echo $start; ?> Any help would be greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/178975-solved-add-5-to-a-variable-when-a-button-is-clicked-and-re-run-a-for-loop/ Share on other sites More sharing options...
smerny Posted October 25, 2009 Share Posted October 25, 2009 onclick is a javascript thing, php works on page load only... you have to create a form with a submit type that will pass the start value to the next page load... might actually want to look into paginations http://www.google.com/#hl=en&source=hp&q=php+pagination&aq=f&aqi=g10&oq=&fp=b8148470ea1f7ec2 Quote Link to comment https://forums.phpfreaks.com/topic/178975-solved-add-5-to-a-variable-when-a-button-is-clicked-and-re-run-a-for-loop/#findComment-944270 Share on other sites More sharing options...
jefffan24 Posted October 25, 2009 Author Share Posted October 25, 2009 Well I mean onlick is just what I had now, I'm willing to try anything. I was just giving my current failed attempt at it. Quote Link to comment https://forums.phpfreaks.com/topic/178975-solved-add-5-to-a-variable-when-a-button-is-clicked-and-re-run-a-for-loop/#findComment-944279 Share on other sites More sharing options...
mikesta707 Posted October 25, 2009 Share Posted October 25, 2009 well without AJAX. you won't be able to do it without the onclick doing a page reload. You could have an ajax request get the next 5 items, and fill out the innerHTML with the info, but it would be easiest to do a simple pagination script. there is a tutorial on this website for one of those Quote Link to comment https://forums.phpfreaks.com/topic/178975-solved-add-5-to-a-variable-when-a-button-is-clicked-and-re-run-a-for-loop/#findComment-944284 Share on other sites More sharing options...
jefffan24 Posted October 25, 2009 Author Share Posted October 25, 2009 Ok so I have gotten the pagination script to work somewhat now I need help with that :/ I've got it to show how many pages there are based on the results but when I go to the next page it is only changing 1 result. Like I have my primary key auto incrementing. And I have the field descending. So on the first 5 it shows 20-16. Then I go to page 2, and its showing 19-15. Any help would be greatly appreciated. //Get how many rows there are: $sql = "SELECT COUNT(*) FROM testtable"; $result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR); $r = mysql_fetch_row($result); $numrows = $r[0]; // number of rows to show per page $rowsperpage = 5; // find out total pages $totalpages = ceil($numrows / $rowsperpage); ########## // get the current page or set a default if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { // cast var as int $currentpage = (int) $_GET['currentpage']; } else { // default page num $currentpage = 1; } // end if ########## // if current page is greater than total pages... if ($currentpage > $totalpages) { // set current page to last page $currentpage = $totalpages; } // end if // if current page is less than first page... if ($currentpage < 1) { // set current page to first page $currentpage = 1; } // end if ########### // the offset of the list, based on current page $offset = ($currentpage - 1); ########## // get the info from the db $sql = "SELECT * FROM testtable LIMIT $offset, $rowsperpage"; $result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR); ######### //Number fetch array for $result $row2 = mysql_fetch_array($result); for($counter = 1; $counter <= 5; $counter += 1) { echo $row2['ID'] . "<br />"; echo " Subject:<b><font color='red'> " . $row2['Subject'] . "</font></b>"; echo "<br />Message: <b>" . $row2['Message'] . "</b><br /><br />"; $row2 = mysql_fetch_array($result); } ######## /****** build the pagination links ******/ // if not on page 1, don't show back links if ($currentpage > 1) { // show << link to go back to page 1 echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; // get previous page num $prevpage = $currentpage - 1; // show < link to go back to 1 page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> "; } //End IF ######### // range of num links to show $range = 3; // loop to show links to range of pages around current page for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { // if it's a valid page number... if (($x > 0) && ($x <= $totalpages)) { // if we're on current page... if ($x == $currentpage) { // 'highlight' it but don't make a link echo " [<b>$x</b>] "; // if not current page... } else { // make it a link echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> "; } // end else } // end if } // end for //Show next page link even if page is 1 if ($currentpage <= $totalpages - 1) { //get next page num $nextpage = $currentpage + 1; //show > to go to next page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; //show >> to go to last page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> "; } // end if mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/178975-solved-add-5-to-a-variable-when-a-button-is-clicked-and-re-run-a-for-loop/#findComment-944322 Share on other sites More sharing options...
jefffan24 Posted October 26, 2009 Author Share Posted October 26, 2009 I know it hasn't been long since my last post but the quicker I can get this the better. If somebody doesn't understand what I'm asking please let me know. I know I can be difficult to understand sometimes. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/178975-solved-add-5-to-a-variable-when-a-button-is-clicked-and-re-run-a-for-loop/#findComment-944575 Share on other sites More sharing options...
smerny Posted October 26, 2009 Share Posted October 26, 2009 well, "LIMIT $offset, $rowsperpage" is the part which determines what is shown... rows per page is set here "$rowsperpage = 5;" offset is set here "$offset = ($currentpage - 1);" if you are on page two, i'm guessing you want your limit to be want your limit to be ($currentpage - 1) * $rowsperpage, $rowsperpage which means you change your $offset to: $offset = ($currentpage - 1) * $rowsperpage; Quote Link to comment https://forums.phpfreaks.com/topic/178975-solved-add-5-to-a-variable-when-a-button-is-clicked-and-re-run-a-for-loop/#findComment-944641 Share on other sites More sharing options...
jefffan24 Posted October 26, 2009 Author Share Posted October 26, 2009 That worked thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/178975-solved-add-5-to-a-variable-when-a-button-is-clicked-and-re-run-a-for-loop/#findComment-944644 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.