comparing values

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

Post Reply
taldos
Forum Commoner
Posts: 39
Joined: Mon Aug 23, 2004 8:47 am
Location: Philadelphia

comparing values

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
taldos
Forum Commoner
Posts: 39
Joined: Mon Aug 23, 2004 8:47 am
Location: Philadelphia

Post by taldos »

figured it out. Had to do with bad logic. thanks for your help.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Even if your logic was good, using implode() would be more practical than appending a comma :P
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: comparing values

Post 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 ++;
	}
Post Reply