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!
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.
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!");
}
$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.
Even if your logic was good, using implode() would be more practical than appending a comma
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 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.
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!");
}
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 ++;
}