PDA

View Full Version : dealing with checkboxes


red_rose
02-27-2007, 01:21 PM
hello there,

i am having problem with my checkboxes. i have inbox.php which includes a delete button to delete messages which are checked (each message has a unique checkbox).

actually the IF and delete query works well if i check only one checkbox. i tried checking 2-3 checkboxes and click delete button, but it will only delete one message. let say i checked msg1, msg2 and msg3, it will only delete msg3.

below is my code. could anyone tell me what's wrong? any help is appreciated. thank you so much

inbox.php :


<?php
//connection script

$selectMsg = $_POST['selectMsg'];

if(isset($_GET['userid']))
{
$receiver = $_GET['userid'];
$query1 = sprintf("SELECT * FROM message where receiver='$receiver'");
$result1 = @mysql_query($query1);
$row1 = @mysql_fetch_array($result1);
}

if(isset($_POST['delete']))
{
if($selectMsg != '')
{
$sql = "delete from message where msgID = '$selectMsg'";
$result_sql = @mysql_query($sql);
$row_sql = @mysql_fetch_array($result_sql);
}
else echo "No message has been selected";
}

?>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<a href="compose.php">Compose</a>
<form name="form1" method="post" action="inbox.php">
<table width="60%" border="2" cellspacing="2" cellpadding="2">
<tr>
<td width="6%">&nbsp;</td>
<td width="30%">From</td>
<td width="25%">Date</td>
<td width="39%">Subject</td>
</tr>

<?php do {
?>
<tr>
<td><input name="selectMsg" type="checkbox" value="<?php echo $row1['msgID']; ?>"></td>
<td><?php echo $row1['sender']; ?></td>
<td><?php echo $row1['date']; ?></td>
<td><a href="message.php?msgID=<?php echo $row1['msgID']; ?>"><?php echo $row1['subject']; ?></a></td>
</tr>
<?php } while($row1 = @mysql_fetch_array($result1)); ?>

</table>
<input name="delete" type="submit" value="Delete">
</form>
</body>
</html>

davidj
02-27-2007, 03:27 PM
first of all your checkbox naming needs to be in an array. By adding [] will do the trick

<input name="selectMsg[]" type="checkbox"


this array can be accessed using...


$selectMsg =$_POST['selectMsg'];

$selectMsg[0]
$selectMsg[1]
$selectMsg[2]
$selectMsg[3]


then in your php you need to wrap your delete in a foreach loop


foreach ($selectMsg as $value){

delete ...... where id = $value

}

red_rose
02-27-2007, 04:41 PM
hey dj, thank you so much for your help. really appreciate it. now my delete query is working :)

i have another question to ask. can i put a mail image before every message? this means i will have two types of mail images in my folder. when the user sign in and check inbox, the unread messages will have different type of mail image and read messages will have different type of mail image. could you guide me on this? is it possible to do it this way?

i wish to display number of unread messages next to inbox (example : Inbox(5)) could you give me a rough idea on how to do this?

thank you so much for your help

davidj
02-27-2007, 05:31 PM
put a switch inside your checkbox loop. Place the switch inside the <td> you want the image to display

<?php

switch($row['field']){

case '1': echo "<img src=\"mail_image1.jpg\" >"; break;
case '2': echo "<img src=\"mail_image2.jpg\" >"; break;

}


let me explain...

$row['field'] is the database value which you check the contents

case'1' is the control which looks for the value (in this case its 1)

after the case you have : anything after the colon will execute as long as the case == the $row and in this example we are echoing an <img

red_rose
02-28-2007, 09:16 AM
im sorry, actually i don't really understand


put a switch inside your checkbox loop. Place the switch inside the <td> you want the image to display



im confused here because i don't know whether to put the switch in the same <td> as the checkbox or a new <td>. you stated here put it inside checkbox loop. why we need to do this?


$row['field'] is the database value which you check the contents


what do you mean check the contents? i don't understand this. how will i know whether the message has been read or not? do i need to put an extra column for this?

thank you so much for your help

davidj
02-28-2007, 10:41 AM
red you have to start thinking logically about this

what do you mean check the contents? i don't understand this. how will i know whether the message has been read or not? do i need to put an extra column for this?

what do you think you need to do to determine whether a mail has been read??? How would you do it other than having a flag in the db?

get this working so you are displaying different flag value then we will turn this value into a different image