Re: using a for loop but it is not working how come
| From: | Monty | Date: | Fri, 06 Jun 2003 07:21:07 +0000 |
| Subject: | Re: using a for loop but it is not working how come | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-150456@lists.php.net to get a copy of this message | ||
> I am having a problem with this script It pulls a list of numbers from
> one field in the database (the numbers are in this format
> (275,277,278,276) It needs to pull each number and run it through the
> function dofunction and then move on to the next one in tell there are
> no more to process. What am I doing wrong or is there a better way to
> do this.
>
> sql="Select Numbers from dom where name = '$name'";
> $results=safe_query($sql);
> $DBRow = mysql_fetch_array($results);
> $numbers = $DBRow["Numbers"];
> $numbers = array($numbers);
> for($i = 0; $i < count($numbers); $i++) {
> $number = $numbers[$i];
> dofunction($number);
> }
You have a few problems in your code. This hasn't been tested, but, is a
start...
sql="Select Numbers from dom where name = '$name'";
$results=safe_query($sql);
$DBRow = mysql_fetch_array($results);
$number_array = explode(",", $DBRow["Numbers"]);
// above splits number list into array using comma as delimiter
foreach($number_array as $digit) {
dofunction($digit);
}
Above foreach() construct loops through the array and assigns the value of
each element to $digit, and continues doing this until it reaches the end of
the array.
http://www.php.net/manual/en/control-structures.foreach.php
http://www.php.net/manual/en/function.explode.php
Monty