Jump to content

A couple questions about pagination and deleting records


jefffan24

Recommended Posts

So what I'm trying to do is I have a "feedback" section on a website i'm working on.  It shows 5 records at a time.  What I want to do is add a Delete button/link where it will delete only that row that is being shown:

 

height=300 width=450http://i53.photobucket.com/albums/g52/jefffan24/plaster.png[/img]

 

So when a person clicks Delete Feedback it will only delete the feedback for that row/record.  What I have currently is this:

 

$row2 = mysql_fetch_array($result);
for($counter = 1; $counter <= 5; $counter += 1)
{

$id = $_GET[iD];
$delete = mysql_query("DELETE FROM feedback WHERE ID = $id");
echo "<a href=\"editphpfeedback.php?$delete$ \">Delete Feedback?</a><br />";
echo "<font face='comic sans ms' size='-1'>Subject: <b><font color='red'> " . $row2['Subject'] . "</font></b>";
echo "<br />Message: <b>" . $row2['Message'] . "</b><br /><br />";
echo "<hr color='#EEEEEE' /></font>";
echo "</form>";
$row2 = mysql_fetch_array($result);


}

 

If you can fix that or if you think something else would work please let me know and I will try it.

 

Ok so now for my second question, I currently have my script to show 5 results at a time and pull 5 results from my database at a time.  Right now these 2 process do not work with each other so I my script shows the Subject:, Message: 5 times even if there are no records to be shown.  Is there any way I can control how many times (Subject:, Message:) is shown based on how many rows there are, but still only show 5 at a time?  I have my script doing? pagination so that when there is more then 5 results it will go to the next page.  Any help here would be greatly appreciated.  Currently what I have:

 

$sql = "SELECT  COUNT(*) FROM feedback";
$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) * $rowsperpage;


##########

// get the info from the db 
$sql = "SELECT * FROM feedback ORDER BY ID DESC 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)
{

$id = $_GET[iD];
$delete = mysql_query("DELETE FROM feedback WHERE ID = $id");
echo "<a href=\"editphpfeedback.php?$delete$ \">Delete Feedback?</a><br />";
echo "<font face='comic sans ms' size='-1'>Subject: <b><font color='red'> " . $row2['Subject'] . "</font></b>";
echo "<br />Message: <b>" . $row2['Message'] . "</b><br /><br />";
echo "<hr color='#EEEEEE' /></font>";
echo "</form>";
$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 = 5;


// 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

delete feedback button, needs a ?, with the current id, of the post, and delete that, with a where clause....

 

straight froward easy example, let you delete the messages, based via there message id,

via another delete sql statement....

<?php
// database connection.

//select

$sql="SELECT * FROM messages";

$res=mysql_query($sql)or die(mysql_error());

while($data=mysql_fetch_assoc($res)){

echo "<a href='{$_SESSION['PHP_SELF']}?delete_message={$data['id']}'>dlete this message</a><br>{$data['message']}<br><br>";

}


if(isset($_GET['delete_message'])){

//delete the message where id='$delete_message'"; // from the table need to delete the message.



}
?>

hope you understand me.......

 

 

 

 

 

 

ok but how do I get that into my for loop?  I can't do a while loop within a for loop can I?

 

Also how does that delete without telling it where delete from like DELETE FROM feedback WHERE ID= whatever...

 

Sorry I'm kind of new to the whole php thing I might be missing something, but it isn't working for me :/

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.