Page 1 of 1

Delete all PM's

Posted: Fri Aug 13, 2004 1:03 pm
by dwfait
I have a checkbox for each Private Message on a page. Their on a form. How would i make it so that it gets all of the name of all of the checkboxes, which the names of would be the ID of the PM's in the database, and then delete that row from the table?

Here is the code that makes the checkbox for each provate message:

Code: Select all

echo "
<form method="POST" action="index.php?action=msgcsrd">
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">
  <tr>
    <td width="20%" align="center">From:</td>
    <td width="20%" align="center">Subject:</td>
    <td width="20%" align="center">Date:</td>
  </tr>

    ";

    $link = mysql_connect("localhost", "root", "")
        or die("Could not connect");


mysql_select_db("stormst_sspp")
        or exit("Could not select database");
        
$query = mysql_query("SELECT * FROM pm WHERE toid='$suser' ORDER BY `ID` DESC") or die(mysql_error());
$pmc='0';
  while($row = mysql_fetch_assoc($query)) {
$pmc++;
$fromid=$row['fromid'];
$subject=$row['subject'];
$date=$row['date'];
$id=$row['id'];
echo "
<tr>
<td width="20%">
    <p align="center">
    <input type="checkbox" name="C$id" value="ON" style="float: left">$fromid</td>
    <td width="20%">
    <p align="center"><a href="index.php?action=msgcrf&id=$id">$subject</a></td>
    <td width="20%">
    <p align="center">$date</td>
  </tr>";

}

  
  echo "

</table>
";
if ($pmc=='0') {
	Echo "You have no private messages.";
} else {
	echo "
<input type="submit" value="Delete Selected Items" name="B1">";
}
echo "  </form>";

  mysql_close($link);

Posted: Fri Aug 13, 2004 1:30 pm
by feyd
you could switch the name to something like:

Code: Select all

delete_pm&#1111;$id]
then just foreach the $_POST['delete_pm'] for each key :)

Posted: Sat Aug 14, 2004 4:35 am
by dwfait
Thanks, but i am a complete noob and wouldnt know how to get the variable of the array in the loop..

Posted: Sat Aug 14, 2004 7:08 am
by m3mn0n
Have a look at [php_man]foreach[/php_man]().

Posted: Sat Aug 14, 2004 8:13 am
by dwfait
With the for statement:

Code: Select all

foreach ($_POST['delete_pm'] as $value) {
echo "$value";
}
It just displays the status of the checkbox. How would i get $id value out of it so i can find that row in the table?

Posted: Sat Aug 14, 2004 9:59 am
by feyd

Code: Select all

foreach($_POST['delete_pm'] as $k => $v)
  echo $k."\n";

Posted: Sat Aug 14, 2004 11:04 am
by dwfait
Thanks feyd :D .