Page 1 of 2

tickbox loop

Posted: Wed Aug 20, 2003 6:19 am
by irealms
I am writing an on-site message system and i have a tickbox next to each message to delete them. How do i track each message so that if you tick say the first and third message it will know that the user ticked them so i can run a delete query on each?

Code: Select all

<?php

echo '<p class="main">e-post inbox for '.$_SESSION['valid_user'].'.</p>';
echo '<table class="main" cellspacing="5" cellpadding="5" border="0">';
echo '<form method="post" action="index.php?page=e-post">';
echo '<tr><td><input type="checkbox" name="deleteall"></td><td></td><td><b>From</b></td><td><b>Date sent:</b></td><td><b>Time sent:</b></td><td><b>Subject</b></td></tr>';
$showmail = "SELECT * FROM e_post WHERE username='$_SESSION[valid_user]' AND groupname='$_SESSION[groupname]'";
$showresult = mysql_query($showmail, $db_conn) or die("Query showmail failed".mysql_error());
while ($showrow = mysql_fetch_assoc($showresult))
{
	echo '<tr>';
	echo '<td><input type="checkbox" name="delete[]"></td>';
	echo '<td></td>';
	echo '<td>'.$showrow['from'].'</td>';
    echo '<td>'.$showrow['sentdate'].'</td>';
	echo '<td>'.$showrow['senttime'].'</td>';
	echo '<td>'.$showrow['subject'].'</td>';
	echo '</tr>';
}
echo '</table>';
echo '</form>';

?>

Posted: Wed Aug 20, 2003 6:29 am
by qads
well, i been lately doing lot of check boxes myself, i just put $row[ID] as the check box name and if it is ticked then it will show up in $_POST.

so basicly:


news.php

Code: Select all

&lt;input type="checkbox" name="$row&#1111;ID] value="$row&#1111;ID]"&gt; Delete
delete.php

Code: Select all

<?php
foreach($_POST as $key => $value)
{
if(!$key != "submit button name")
{
$query = mysql_query("delete from table where ID = $key limit 1");
}
}
?>

jus tmake sure put in the submit button other wise it will think of it as a ID too.

Posted: Wed Aug 20, 2003 6:29 am
by greenhorn666
I would have :

Code: Select all

echo '<td><input type="checkbox" name="delete['
        .$showrow['id'].']" value="del"></td>';
then in the form's action script:

Code: Select all

while(list($key, $val) = each($_POST["delete"])) {
    if($val == 'del')
        $deleteIds[] = $key;
}
you could straight away delete each message in turn, but it would be nicer to build a query based on thge ids in $deleteIds that would delete all rows in one query... Be nice to your backend! ;)

Posted: Wed Aug 20, 2003 6:29 am
by pootergeist
delete[' .$showrow['primary_id_number']. ']
...........
post form
...........

Code: Select all

foreach($_POST['delete'] AS $primary_id)
 {
 // $del_row = mysql_query("DELETE FROM `e-post` WHERE id=$primary_id");
 echo 'uncommented would delete row '.$primary_id.'<br />';
 }

Posted: Wed Aug 20, 2003 6:48 am
by irealms
thanks for all the methods people :)
one question on greenhorns reply, how do i make a query to run all the $deleteIds[] ?

Posted: Wed Aug 20, 2003 7:01 am
by will
if you want to do it all in one blow, try

Code: Select all

mysql_query('DELETE FROM table WHERE ID IN ("'. (implode('","', $_POST['deleteIDs']) .'")');
i didn't test that, but i'm pretty sure that is correct

Posted: Wed Aug 20, 2003 7:28 am
by irealms
thanks while we are on same code can you see an error with my date format? it won't show

Code: Select all

<?php
echo '<p class="main">e-post inbox for '.$_SESSION['valid_user'].'.</p>';
echo '<table class="main" cellspacing="5" cellpadding="5" border="0">';
echo '<form method="post" action="index.php?page=e-post">';
echo '<tr><td><input type="checkbox" name="deleteall"></td><td></td><td><b>From</b></td><td><b>Date sent</b></td><td><b>Time sent</b></td><td><b>Subject</b></td></tr>';
$showmail = "SELECT id,groupname,username,whofrom,DATE_FORMAT(date, '%d-%m-%Y'),time,readstate,subject,message FROM e_post WHERE username='$_SESSION[valid_user]' AND groupname='$_SESSION[groupname]'";
$showresult = mysql_query($showmail, $db_conn) or die("Query showmail failed".mysql_error());
while ($showrow = mysql_fetch_assoc($showresult))
{
	$date = $showrow['date'];
	echo '<tr>';
	echo '<td><input type="checkbox" name="delete[]"></td>';
	echo '<td></td>';
	echo '<td>'.$showrow['whofrom'].'</td>';
    echo '<td>'.$date.'</td>';
	echo '<td>'.$showrow['time'].'</td>';
	echo '<td><a href="index.php?page=e-open&&id='.$showrow['id'].'" />'.$showrow['subject'].'</a></td>';
	echo '</tr>';
}
echo '</table>';
echo '</form>';

?>

Posted: Wed Aug 20, 2003 7:36 am
by greenhorn666

Code: Select all

SELECT id, groupname, username, whofrom, 
        DATE_FORMAT(date, '%d-%m-%Y') AS ftime,
        time, readstate, subject, message 
    FROM e_post 
    WHERE username='$_SESSION&#1111;valid_user]' 
        AND groupname='$_SESSION&#1111;groupname]

Code: Select all

<?echo $showrow["ftime"];?>
I guess this is what you want...

Posted: Wed Aug 20, 2003 7:40 am
by irealms
thanks, would i have to use Javascript to make selecting a delete all box select all the other tickboxes?

Posted: Wed Aug 20, 2003 7:43 am
by greenhorn666
Yes, its one way to do it...

Posted: Thu Aug 21, 2003 3:58 am
by irealms
now i have :

Code: Select all

<?php
echo '<p class="main">e-post inbox for '.$_SESSION['valid_user'].'.</p>';
echo '<p class="main">';
echo 'Click the subject to read a message';
echo '<table class="main" cellspacing="5" cellpadding="5" border="0">';
echo '<form method="post" action="index.php?page=e-post">';
echo '<tr><td><b>Delete</b></td><td></td><td><b>From</b></td><td><b>Date sent</b></td><td><b>Time sent</b></td><td><b>Subject</b></td></tr>';
$showmail = "SELECT id,groupname,username,whofrom,DATE_FORMAT(date, '%d-%m-%Y') AS sentdate,time,readstate,subject,message FROM e_post WHERE username='$_SESSION[valid_user]' AND groupname='$_SESSION[groupname]'";
$showresult = mysql_query($showmail, $db_conn) or die("Query showmail failed".mysql_error());
while ($showrow = mysql_fetch_assoc($showresult))
{
	echo '<tr>';
	echo '<td><input type="checkbox" name="delete['.$showrow['id'].']" value="del"></td>'; 
	echo '<td></td>';
	echo '<td><a href="index.php?page=user&&more='.$showrow['whofrom'].'" />'.$showrow['whofrom'].'</a></td>';
    echo '<td>'.$showrow['sentdate'].'</td>';
	echo '<td>'.$showrow['time'].'</td>';
	echo '<td><a href="index.php?page=e-open&&id='.$showrow['id'].'" />'.$showrow['subject'].'</a></td>';
	echo '</tr>';
}
echo '<table><tr><td><input type="submit" class="button" name="e-postmain" value="Delete selected"></td></tr></table>';
echo '</table>';
echo '</form>';
echo '</p>';
if (isset($_POST['e-postmain']))
{
	while(list($key, $val) = each($_POST["delete"])) 
		{ 
    if($val == 'del')
        $deleteIds[] = $key;
	$delmail = "DELETE FROM e_post WHERE id='$deleteIds[]'";
	$delmailq = mysql_query($delmail, $db_conn) or die("Query delmail failed".mysql_error());

	} 
}

?>
is that query right ? it doesn't seem to work :(

Posted: Thu Aug 21, 2003 4:14 am
by greenhorn666
Guess what!
If it doesn't work, it probably wrong ;)

No the query ends up as:
DELETE FROM e-post WHERE id = 'Array()'

Not even sure... :P

But Will gave you a hint a bit higher...

Posted: Thu Aug 21, 2003 4:29 am
by irealms
"But Will gave you a hint a bit higher..."

?

Posted: Thu Aug 21, 2003 4:32 am
by greenhorn666
will wrote:if you want to do it all in one blow, try

Code: Select all

mysql_query('DELETE FROM table WHERE ID IN ("'. (implode('","', $_POST['deleteIDs']) .'")');
i didn't test that, but i'm pretty sure that is correct

Posted: Thu Aug 21, 2003 4:42 am
by irealms
ah, i thought you meant higher in your post and i change it to delete where $key and it worked. Would this cause problems?