Page 1 of 1

While loop help

Posted: Thu Jan 17, 2008 12:37 am
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?

Re: While loop help

Posted: Thu Jan 17, 2008 12:47 am
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.

Re: While loop help

Posted: Thu Jan 17, 2008 12:50 am
by jimthunderbird
Yes, I use this technique also :D

Re: While loop help

Posted: Thu Jan 17, 2008 1:32 am
by mrras25
Makes sense and worked perfectly ... many thanks