Page 1 of 1

comparing values

Posted: Wed Aug 24, 2005 7:24 pm
by taldos
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.

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!");

}
Thanks.

Posted: Wed Aug 24, 2005 8:08 pm
by s.dot
Use implode to glue your string together instead of adding , to the end.

Code: Select all

$somearray = array('1','2','3','4','5');
$string = implode(",",$somearray);

echo $string;

// The results is 1,2,3,4,5
String could now be used in your SQL query .... WHERE something IN ($string)

Posted: Wed Aug 24, 2005 8:25 pm
by taldos
figured it out. Had to do with bad logic. thanks for your help.

Posted: Wed Aug 24, 2005 8:38 pm
by s.dot
Even if your logic was good, using implode() would be more practical than appending a comma :P

Re: comparing values

Posted: Thu Aug 25, 2005 1:03 am
by jmut
taldos wrote: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.

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!");

}
Thanks.

untested

Code: Select all

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(!next($msg_delete))
				$test_delete .= $my_value.",";
			else
				$test_delete .= $my_value;
		}
	       //$my_count ++;
	}