comparing values
Posted: Wed Aug 24, 2005 7:24 pm
I'm having a problem with a piece of code. I use explode to break up a string containing thread id's. I then look through the array created and use $_POST['thread_id'] to determine if the user clicked on a checbox to delete a thread.
everything works except for the check I have to determine if the current element is the last one in the array. it it's not the last element, than a comman is added and continues to the next loop.
The count works but string I pass through sql always has a "," at the end which is causing having with the query. Any ideas.
Thanks.
everything works except for the check I have to determine if the current element is the last one in the array. it it's not the last element, than a comman is added and continues to the next loop.
The count works but string I pass through sql always has a "," at the end which is causing having with the query. Any ideas.
Code: Select all
if ($delete_thread)
{
$msg_delete = explode(",", $_POST['msg_id']);
$tot_count = count($msg_delete); //determine length of array
$my_count = 1;
$test_delete = ""; //string to pass to sql query
foreach($msg_delete AS $my_value)
{
$my_name = "pmsg_".$my_value;
$temp_value = $_POST[$my_name];
if($temp_value == "on")
{
//determine if it's the last element in the array otherwise add "," for the sql querry.
if($my_count == $tot_count)
$test_delete .= $my_value;
else
$test_delete .= $my_value.",";
}
$my_count ++;
}
$sql = "DELETE FROM `table1` WHERE uid in (".$test_delete.")"; //Delete multiple records at once.
$result = mysql_query($sql)
or die("Could not remove comment!");
}