While loop help

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
mrras25
Forum Newbie
Posts: 2
Joined: Thu Jan 17, 2008 12:31 am

While loop help

Post by mrras25 »

I am trying to create a while loop that at the end of the loop it will exclude a , at the end of loop... code looks like this

Code: Select all

 
 
while($data = $DB->Fetch($query)) {
         if(ereg($local,$data["location"])) {
            $str .= "['".$data["location"]."','".$data["array"]."','".$data["model"]."'],";
         }
      }
      echo $str;
   }
 
 
at the end of the loop I dont want the "," after the ] how do I go about doing this?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: While loop help

Post by John Cartwright »

Code: Select all

while($data = $DB->Fetch($query)) {
   $output = array();
   if(ereg($local,$data["location"])) {
      $output[] = "['".$data["location"]."','".$data["array"]."','".$data["model"]."']";
   }
   echo implode(',', $output);
}
The easiest way is to store the results in an array, then apply implode() to glue the pieces.
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

Re: While loop help

Post by jimthunderbird »

Yes, I use this technique also :D
mrras25
Forum Newbie
Posts: 2
Joined: Thu Jan 17, 2008 12:31 am

Re: While loop help

Post by mrras25 »

Makes sense and worked perfectly ... many thanks
Post Reply