Jump to content

[SOLVED] Add 5 to a variable when a button is clicked, and re-run a for loop


Recommended Posts

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!

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

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

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);
?>

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.

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;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.