tickbox loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

tickbox loop

Post 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>';

?>
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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.
User avatar
greenhorn666
Forum Commoner
Posts: 87
Joined: Thu Aug 14, 2003 7:14 am
Location: Brussels, Belgium

Post 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! ;)
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post 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 />';
 }
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

Post by irealms »

thanks for all the methods people :)
one question on greenhorns reply, how do i make a query to run all the $deleteIds[] ?
will
Forum Contributor
Posts: 120
Joined: Fri Jun 21, 2002 9:38 am
Location: Memphis, TN

Post 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
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

Post 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>';

?>
User avatar
greenhorn666
Forum Commoner
Posts: 87
Joined: Thu Aug 14, 2003 7:14 am
Location: Brussels, Belgium

Post 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...
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

Post by irealms »

thanks, would i have to use Javascript to make selecting a delete all box select all the other tickboxes?
User avatar
greenhorn666
Forum Commoner
Posts: 87
Joined: Thu Aug 14, 2003 7:14 am
Location: Brussels, Belgium

Post by greenhorn666 »

Yes, its one way to do it...
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

Post 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 :(
User avatar
greenhorn666
Forum Commoner
Posts: 87
Joined: Thu Aug 14, 2003 7:14 am
Location: Brussels, Belgium

Post 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...
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

Post by irealms »

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

?
User avatar
greenhorn666
Forum Commoner
Posts: 87
Joined: Thu Aug 14, 2003 7:14 am
Location: Brussels, Belgium

Post 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
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

Post 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?
Post Reply