Forum Moderators: coopster

Message Too Old, No Replies

an array with a for loop inside it

confused yet?

         

Knowles

1:34 am on May 26, 2003 (gmt 0)

10+ Year Member



I am trying to get an array to use a for loop inside it. Like this

$addy = array(
for ($x = 1; $x < count($add); $x++)
{
$add[$x];
}
);

the $add is coming from a database which I am not sure is putting what what I actually want it to but I will deal with that if I can get it to output the wrong info into the array. Anyone ever do this? Is it possibe?

[/edit] Currently if I have that code in the page it does not load =[

Slade

1:39 am on May 26, 2003 (gmt 0)

10+ Year Member



Do you mean you're trying to populate an array with the results of a for loop?

Knowles

1:39 am on May 26, 2003 (gmt 0)

10+ Year Member



Yep thats what I am trying to do. Glad someone can make it sould clear.

willybfriendly

1:45 am on May 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are using mysql_fetch_array(), the result is already in an array, both numeric and associative

[php.net...]

It might be easier to do what you want with your query, or lacking that, to play around with moving the data from one array to the other.

Not really sure just what you are trying to accomplish, so this may not be of much help.

WBF

Slade

1:46 am on May 26, 2003 (gmt 0)

10+ Year Member



your code is close... try this:

$addy = array();
for ($x = 1; $x < count($add); $x++)
{
$addy[]= $add[$x];
};

Slade

1:49 am on May 26, 2003 (gmt 0)

10+ Year Member



Hmm... if the data does in fact come from a table, you probably could squeak better time out of it by using the fetch_array call. That's if you're not doing something to the data first... (your sample doesn't, so it would be a good candidate). But, then again, there's always array_walk [php.net...]

Knowles

1:54 am on May 26, 2003 (gmt 0)

10+ Year Member



I am using mysql_fetch_array already.....

while ($row = mysql_fetch_array($results, MYSQL_BOTH))
{
$add = ($row["3"]);
$por = ($row["4"]);
}
$addy = array();
for ($x = 1; $x < count($add); $x++)
{
$addy[]= $add[$x];
};

Here it is with your change, still not getting data. But I know $por but its not giving me what I want. Actually just tested $add and its not either. Ill have to keep playing with them.

olwen

2:05 am on May 26, 2003 (gmt 0)

10+ Year Member



Are you sure you don't want

$addy = array();
while ($row = mysql_fetch_array($results, MYSQL_BOTH))
{
$add = ($row["3"]);
$por = ($row["4"]);
for ($x = 1; $x < count($add); $x++)
{
$addy[]= $add[$x];
};
}

Knowles

2:07 am on May 26, 2003 (gmt 0)

10+ Year Member



I guess what I really need to do is move it from the array that the fetch is making to another array that can be used. When I tried by having it create an array during the while statment it only pulls the last entry in the DB.

Knowles

2:11 am on May 26, 2003 (gmt 0)

10+ Year Member



I have tried this way:
while ($row = mysql_fetch_array($results, MYSQL_ASSOC))
{
$addy = array("%s", $row["paddress"]);
$port = array("%s", $row["pport"]);
}
Then when I echo $addy[0] it out puts %s.

Olwen, tried it that way as well but didnt work either. :(

willybfriendly

2:36 am on May 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about

$count = mysql_num_rows($result);

for($i = 0; $i < $count; $i++)
{
$row = mysql_fetch_object($result);
$addy[$i] = $row->add;
}

WBF

(I didn't test it)