Jump to content

loop deleting the checkboxes


ted_chou12

Recommended Posts

I couldnt find the last post, so I reposted this again:
this is my query:
[code]
<form name="deletepm" action="" method="post">
<?php
$sql = "SELECT * FROM privatemessages WHERE type='outbox' AND username='$username'";
$res = mysql_query($sql) or die (mysql_error());
while ($r = mysql_fetch_array($res)){
echo "<tr>\n";
echo "<td align=center><img src=\"images/". $r['entryicon'] .".gif\"></td>\n";
echo "<td align=center><input type=checkbox name=id value=\"". $r['id'] ."\"></td>\n";//this is the check box
echo "<td>". $user ."</td>\n";
echo "<td>". $subject ."</td>\n";
echo "<td>". $date ."</td>\n";
echo "</tr>\n";}?>
<input class="button" name="delete" type="submit" value="Delete Entry" onClick="javascript: return checkDelete()" />
</form>
[/code]
If submit button is deleted, this action below will take place:
[code]
<?php
if(isset($_POST['delete'])){
$id = $_POST['id'];
require("../mysqlconnection.php");
$delete = mysql_query("DELETE FROM privatemessages WHERE id='$id' AND username='$username'");
if($delete === false)
{echo"<p><b>Sorry, the database is temporary down, please come back later!</b></p>";}
else
{header("location: confirm.php?confirm=pm_delete");}}?>
[/code]
However, checkboxes can be multiple selected, that is be selected for at least one. But my code does not run for however much that are selected, I dont know if looping the action will be a nice idea, btw, I posted this before, someone did helped me, but I still couldnt get this right
Thanks
Ted
Link to comment
https://forums.phpfreaks.com/topic/32943-loop-deleting-the-checkboxes/
Share on other sites

Change this line

[code]
echo "<td align=center><input type=checkbox name=id value=\"". $r['id'] ."\"></td>\n";
[/code]

to this

[code]
echo "<td align=center><input type=checkbox name=id[$counter] value=\"". $r['id'] ."\"></td>\n";
[/code]

add this above your while ($r...
[code]
$counter=0;
[/code]


add this above your end php tag in your select query
[code]
$counter++;
[/code]


You will then need to add something like this to your delete action:

[code]
for(i=0;$>$amount_of_check_boxes; $i++) {
  if(isset($_POST['id'][$i]) {
    mysql_query("DELETE FROM privatemessages WHERE id='" . $_POST['$id'][$i] . "' AND username='$username'"); 
  }
}
[/code]

should give you a rough idea.....

;)
nope doesnt work :'( it doesnt delete at all.
This is how i put it together:
<?php for($i=0;$i>$amount_of_check_boxes; $i++) {
if(isset($_POST['delete'][$i])){
$id = $_POST['id'];
while ($id != ""){
require("../mysqlconnection.php");
$delete = mysql_query("DELETE FROM privatemessages WHERE id='" . $_POST['$id'][$i] . "' AND username='$username'");
if($delete === false)
{echo"<p><b>Sorry, the database is temporary down, please come back later!</b></p>";}
else
{header("location: confirm.php?confirm=pm_delete");}}}}?>
But ... :'(
let me toss yah a chunk of code before I head off to bed...

form.. edited a little
[code=php:0]
<form name="deletepm" action="" method="post">
<?php
$sql = "SELECT * FROM privatemessages WHERE type='outbox' AND username='$username'";
$res = mysql_query($sql) or die (mysql_error());
while ($r = mysql_fetch_array($res)){
echo "<tr>\n";
echo "<td align=center><img src=\"images/". $r['entryicon'] .".gif\"></td>\n";
echo "<td align=center><input type='checkbox' name='id[]' value=\"". $r['id'] ."\"></td>\n";//this is the check box
echo "<td>". $user ."</td>\n";
echo "<td>". $subject ."</td>\n";
echo "<td>". $date ."</td>\n";
echo "</tr>\n";}?>
<input class="button" name="delete" type="submit" value="Delete Entry" onClick="javascript: return checkDelete()" />
</form>
[/code]

code... editted a lot

[code=php:0]
<?php
if(isset($_POST['delete'])){
    require("../mysqlconnection.php");
    foreach($_POST['id'] as $id) {
          $delete = mysql_query("DELETE FROM privatemessages WHERE id='$id' AND username='$username'")
              or die('<p><b>Sorry, the database is temporary down, please come back later!</b></p>');
    }
}

header("location: confirm.php?confirm=pm_delete");
?>
[/code]

give that one a try :P I'd really suggested filtering $id though... something like this in the loop...

[code=php:0]
if(!is_numeric($id)) {
    die('Hack Attempt');
}
[/code]

gl!
Just a few things:

The variable $amount_of_check_boxes was simply an example, this could be a hidden form value that contains the number of tickboxes you have.

eg: (this would need to go below your while loop)
[code]
<input type="hidden" name="amount_of_check_boxes" value="<?=$counter?>">
[/code]

This line:
[code]
for($i=0;$i>$amount_of_check_boxes; $i++) {
[/code]

would actually become:
[code]
for($i=0;$i<$_POST['amount_of_check_boxes']; $i++) {
[/code]


This line:
[code]
if(isset($_POST['delete'][$i]) {
[/code]

would actually become:
[code]
if(isset($_POST['id'][$i]) {
[/code]

and would be nested in your original

[code]
//if the form has been submitted
if(isset($_POST['delete'])) {
  //if one of the checkboxes has been ticked
  if(isset($_POST['id'][$i]) {
  ...
[/code]


looks like genericnumber1 has covered it also.....

oh, I understand why now, but it still wouldnt work, and there seems to be some text boxes above, i dont know why, but they are appearing numbers....
this is what I have now, i dont know if I had put it together correctly:
[code]<?php for($i=0;$i<$_POST['amount_of_check_boxes']; $i++) {
require("../mysqlconnection.php");
if(isset($_POST['delete'])) {
if(isset($_POST['delete'])){
    require("../mysqlconnection.php");
    foreach($_POST['id'] as $id) {
          $delete = mysql_query("DELETE FROM privatemessages WHERE id='$id' AND username='$username'") or
              die('<p><b>Sorry, the database is temporary down, please come back later!</b></p>');
    }
}
}}
?>[/code]
[code]
while ($r = mysql_fetch_array($res)){
// Echo out the records with wordwrap, remove if you like
echo "<tr>\n";
echo "<td align=center><img src=\"images/". $r['entryicon'] .".gif\"></td>\n";
echo "<td align=center>><input type='checkbox' name='id[]' value=\"". $r['id'] ."\"></td>\n";
echo "<td>". $user ."</td>\n";
echo "<td>". $subject ."</td>\n";
echo "<td>". $date ."</td>\n";
echo "</tr>\n";
$counter++;?>
<input type=\"hidden\" name=\"amount_of_check_boxes\" value=\"<?=$counter?>\"><?}
[/code]
uhh actually what I posted was meant to be the only thing on your page :P

[code=php:0]
<?php
if(isset($_POST['delete'])){
    require("../mysqlconnection.php");
    foreach($_POST['id'] as $id) {
          mysql_query("DELETE FROM privatemessages WHERE id='$id' AND username='$username'")
              or die('<p><b>Sorry, the database is temporary down, please come back later!</b></p>');
    }
}

header("location: confirm.php?confirm=pm_delete");
?>
[/code]

and the script should work fine ;) Off to bed I go! good luck
[code]
<?php
if(isset($_POST['delete'])){
require("../mysqlconnection.php");
foreach($_POST['id'] as $id) {
mysql_query("DELETE FROM privatemessages WHERE id='$id' AND username='$username'") or die('<p><b>Sorry, the database is temporary down, please come back later!</b></p>');}
header("location: confirm.php?confirm=pm_delete");}

//form starts here, since genericnumber1 didnt mention about the checkbox name, so I used the original.
$counter=0;
while ($r = mysql_fetch_array($res)){
echo "<tr>\n";
echo "<td align=center><img src=\"images/". $r['entryicon'] .".gif\"></td>\n";
echo "<td align=center><input type=checkbox name=id[$counter] value=\"". $r['id'] ."\"></td>\n";
echo "<td>". $user ."</td>\n";
echo "<td>". $subject ."</td>\n";
echo "<td>". $date ."</td>\n";
echo "</tr>\n";
$counter++;}
[/code]
The problem is it doesnt delete at all. Maybe instead of starting with harder form, we should start with something easier, like:
[code]
A<input type=checkbox name=delete value=\"delete1\">
B<input type=checkbox name=delete value=\"delete2\">
C<input type=checkbox name=delete value=\"delete3\">
[/code]
how would you loop something like this?
Thanks
This is everything on my page:
[code]
<h3>My Inbox</h3>
<script language="JavaScript">
<!-- Modified By:  Steve Robison, Jr. ([email protected]) -->
<!-- Begin
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = "true";
return "Uncheck All"; }
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false; }
checkflag = "false";
return "Check All"; }
}
//  End -->
</script>
<?php
if(isset($_POST['delete'])){
require("../mysqlconnection.php");
foreach($_POST['id'] as $id) {
  mysql_query("DELETE FROM privatemessages WHERE id='$id' AND username='$username'")
  or die('<p><b>Sorry, the database is temporary down, please come back later!</b></p>');
}
header("location: confirm.php?confirm=pm_delete");}
?>

<form name="deletepm" action="" method="post">
<?php
//order sort
$order = $_GET['order'];
if ($order == "senderasc") {$orderby = "ORDER BY username"; $menu1 = "v";}
elseif ($order == "senderdes") {$orderby = "ORDER BY username DESC"; $menu1 = "^";}
elseif ($order == "subjectasc") {$orderby = "ORDER BY entrytitle"; $menu2 = "v";}
elseif ($order == "subjectdes") {$orderby = "ORDER BY entrytitle DESC"; $menu2 = "^";}
elseif ($order == "dateasc") {$orderby = "ORDER BY entrydate"; $menu3 = "v";}
else {$order = "datedes";$orderby = "ORDER BY entrydate DESC"; $menu3 = "^";}

if ($order == "senderasc") {$ordersendermenu = "senderdes";}
else {$ordersendermenu = "senderasc";}

if ($order == "subjectasc") {$ordersubjectmenu = "subjectdes";}
else {$ordersubjectmenu = "subjectasc";}

if ($order == "dateasc") {$orderdatemenu = "datedes";}
else {$orderdatemenu = "dateasc";}

// connect to mysql below
require("../mysqlconnection.php");

// Set how many rows to display on each page
$limit = 20;
// Query the database to get the total number of rows
$query_count = "SELECT * FROM privatemessages WHERE username='$username' AND type='inbox'";
  $result_count = mysql_query($query_count) or die (mysql_error());
$totalrows = mysql_num_rows($result_count);
if(isset($_GET['page'])){    // Checks if the $page variable is empty (not set)
$page = $_GET['page'];      // If it is empty, we're on page 1
} else {
$page = 1;
}
// Set the start value
$startvalue = $page * $limit - ($limit);
// Query the database and set the start row and limit
$sql = "SELECT * FROM privatemessages WHERE username='$username' AND type='inbox' $orderby
LIMIT $startvalue, $limit";
  $res = mysql_query($sql) or die (mysql_error());
echo "<table border=0 align=center cellpadding=\"0\" cellspacing=\"1\" width=\"100%\">
<tr height=\"28em\">
<td width=\"28em\" align=center background=\"gradient.gif\"><img src=\"images/xx.gif\"></td>
<td width=\"28em\" align=center background=\"gradient.gif\">
<input type=checkbox value=\"Check All\" onClick=\"this.value=check(action)\"></td>
<td width=\"80em\" background=\"gradient.gif\">
<a href=\"".$_SERVER['PHP_SELF']."?page=".$page."&order=".$ordersendermenu."\"><b>".$menu1." Sender</b></a></td>
<td width=\"160em\" background=\"gradient.gif\">
<a href=\"".$_SERVER['PHP_SELF']."?page=".$page."&order=".$ordersubjectmenu."\"><b>".$menu2." Subject</b></a></td>
<td width=\"70em\" background=\"gradient.gif\">
<a href=\"".$_SERVER['PHP_SELF']."?page=".$page."&order=".$orderdatemenu."\"><b>".$menu3." Date</b></a></td>
</tr>";
// Do a quick check to see if there are any records to display
if(mysql_num_rows($res) == 0){
echo "<tr>
<td colspan=3 align=center>No messages found!!</td>
  </tr>";
}
// Start loop through records
$counter=0;
while ($r = mysql_fetch_array($res)){

$date = strtotime($r['entrydate']) + $row['timeoffset'];
$date = date('Y-m-d',$date);
$subject = substr($r['entrytitle'], 0, 30);
if (strlen($r['entrytitle']) > 30){$subject = "$subject..";}
$user = substr($r['user'], 0, 10);
if (strlen($r['user']) > 10){$user = "$user..";}
if ($r['flag'] == "unread") {$color = "#cccccc";}
else {$color = "#f2f2f2";}


// Echo out the records with wordwrap, remove if you like
echo "<tr>\n";
echo "<td align=center><img src=\"images/". $r['entryicon'] .".gif\"></td>\n";
echo "<td align=center><input type=checkbox name=id value=\"". $r['id'] ."\"></td>\n";
echo "<td>". $user ."</td>\n";
echo "<td>". $subject ."</td>\n";
echo "<td>". $date ."</td>\n";
echo "</tr>\n";
$counter++;}

// Close the table
echo "</table>";
// Start links for pages
echo "<p align=center>";

// Sets link for previous 25 and return to page 1
if($page != 1){
$pageprev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=1&order=$order\"><<</a>&nbsp;&nbsp;";
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$pageprev&order=$order\">PREV&nbsp;</a> ";
}else{
echo "PREV&nbsp;";
}
// Find out the total number of pages depending on the limit set
$numofpages = $totalrows / $limit;
$totalpages = ceil($numofpages);
// Loop thru all the pages and echo out the links
for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo "[".$i."] ";
}else{
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i&order=$order\">$i</a> ";
}
}

// Check for straglers after the limit blocks
if(($totalrows % $limit) != 0){
if($i == $page){
echo "[".$i."] ";
}else{
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i&order=$order\">$i</a> ";
}
}
// Print out the Next 25 and Goto Last page links
if(($totalrows - ($limit * $page)) > 0){
$pagenext = ($page + 1);
  echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$pagenext&order=$order\">NEXT&nbsp;</a>&nbsp;&nbsp;";
  echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$totalpages&order=$order\">>></a>&nbsp;&nbsp;";
}else{
echo("NEXT");
}
echo "</p>";
// Free results
mysql_free_result($res);
// Close mysql connection
mysql_close($mysql_conn);?>
<input class="button" name="delete" type="submit" value="Delete Entry" onClick="javascript: return checkDelete()" />
</form>
[/code]
You foreach() is expecting an array

this line:
[code]
echo "<td align=center><input type=checkbox name=id value=\"". $r['id'] ."\"></td>\n";
[/code]

s/be
[code]
echo "<td align=center><input type=checkbox name=id[$counter] value=\"". $r['id'] ."\"></td>\n";
[/code]

the id var is now an array....

:)

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.