denla Posted April 8, 2011 Share Posted April 8, 2011 Hey guys, Got another question im hoping someone can help me with. I have a foreach loop (for use in a mysql query): foreach ($interests as $interest) { $query .= "($id, $interest), "; } problem is i do not want the comma(,) in the last loop. Is there some kinda of function i can use so it does not insert it on last loop? Or should i just use a for loop with a nested if loop? something like ; for($i=0; $i < count($interests); $i++){ $query .= "($id, '$interests[$i]')"; if($i + 1 < count($interests)) { $query .= ", "; } } Cheers guys Link to comment https://forums.phpfreaks.com/topic/233064-foreach-loop-how-to-not-loop-witha-on-last-loop/ Share on other sites More sharing options...
gristoi Posted April 8, 2011 Share Posted April 8, 2011 foreach ($interests as $interest) { $query .= "($id, $interest), "; } $query = rtrim($query, ','); Link to comment https://forums.phpfreaks.com/topic/233064-foreach-loop-how-to-not-loop-witha-on-last-loop/#findComment-1198641 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.