Jump to content

foreach loop - how to not loop witha "," on last loop


denla

Recommended Posts

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.