Jump to content

[PHP & MySQL] Pagination + Update/Delete buttons


Tinuva

Recommended Posts

Hey everyone, I was writing a PHP page that runs a MySQL query and then puts the data properly formatted on multiple pages.

Now I also wanted to have buttons next to each result to delete that record from the DB and one to update one field to 1 if it's 0 or 0 if it's 1. And also an image for that field to show image X when it's 1 and image Y when it's 0.

 

But I'm quite stuck on how to do that, as you can't call the variables since it'd only use the ones of the last result. Same goes for variable variables since you'd need to put the name you'd update your variable variable with in a variable with a unique name but you can't since then you don't know the name anymore ;)

 

So I hope that you guys can help me out; here's the part of my code in which the problem is:

echo "<table width='100%' border='0'>
<tr>
  <td width='50%' align='left'>
<font color='#0404B4' face='Tahoma' size=2>Results <b>" . $first . "</b> - <b>" . $last . "</b> of <b>" . $numrows . "</b>
  </td>
  <td width='50%' align='right'>
<font color='#0404B4' face='Tahoma' size=2>Page <b>" . $current . "</b> of <b>" . $total . "</b>
  </td>
</tr>
<tr>
  <td colspan='2' align='right'>
 
  </td>
</tr>
</table></font>";
// Now we can display results.
$results = mysql_query("SELECT * FROM table ORDER BY id DESC");
while ($data = mysql_fetch_array($results))
{
if ($data['done'] == 0) {
	$check = "<img src='X.png' border=0>";
	$done = 1;
}
else {
	$check = "<img src='Y.png' border=0>";
	$done = 0;
}
echo "<p><font color='#0404B4' face='Tahoma' size=3>ID:<font color='#848484'> " . $data['id'] . " <font color='#0404B4'>XXXX:<font color='#848484'> " . $data['XXXX'] . " <font color='#0404B4'>XXXXXXX:<font color='#848484'> " . $data['XXXXXXX'] . " <font color='#0404B4'>XXXXXX:<font color='#848484'> XXXX " . $data['XXXXXX'] . " <font color='#0404B4'>XXXXXX XXXXXX:<font color='#848484'> " . $data['XXXXXX'] . " <font color='#0404B4'>XXXX:<font color='#848484'> " . $data['XXXX'] . " <font color='#0404B4'>Done: " . $check . " <form name='form' method='post'><input type='submit' name='done' value='Done?'><input type='submit' name='delete' value='Delete?'></form></p>";
if(isset($_POST['done'])){
mysql_query("UPDATE donations SET done=" . $done . " WHERE id=" . $data['id']);
}//if isset
if(isset($_POST['delete'])){
mysql_query("DELETE * FROM donations WHERE id=" . $data['id']);
}//if isset 
}
echo "<br>";
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SELF?page=$back_page&limit=$limit\">back</a>    \n");}

for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
$ppage = $limit*($i - 1);
if ($ppage == $page){
echo("<b>$i</b>\n");} // If current page don't give link, just text.
else{
echo("<a href=\"$PHP_SELF?page=$ppage&limit=$limit\">$i</a> \n");}
}

if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo("    <a href=\"$PHP_SELF?page=$next_page&limit=$limit\">next</a>");}

 

Thank you in advance,

~Tinuva

Link to comment
Share on other sites

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.