0

Hey all.

All I need is to collect every id from servers where active ='1' and then cycle the results in a loop right until the end for each individual id.

(I've written this on daniweb, might be typo's or errors.
I'm not interested in code errors it's the process itself)

so...

$sql = mysql_query("SELECT `id` FROM `servers` WHERE `id` = '1'");
$ids = mysql_fetch_array($sql);

foreach ($ids){

// complete the process in these brackets

}

What I dont know is how you write the for each id do this part:

    foreach ($ids){

What do I need to use here to begin the process for each ID?

Thanks in advance!

2
Contributors
4
Replies
9
Views
5 Years
Discussion Span
Last Post by cereal
1

Try this:

$list = array();
while($ids = mysql_fetch_array($sql)){
    $list[] = $ids['id'];
}

print_r($list);

bye!

Votes + Comments
good shout
0

I have nill experience with foreach. Is this right?

$sql = mysql_query("SELECT id from servers WHERE active = '1'");

       $list = array();

                while($ids = mysql_fetch_array($sql)){

                    $list[] = $ids['id'];
                    foreach($list as $id){

                    $get = mysql_query("SELECT * FROM SERVERS WHERE id = ".$id."");
                    $serv = mysql_fetch_array($get);

                    // rest of process here using $serv['cell_ref'] as a reference.

}

This should cycle through all rows. Is that right? (It's not working... so I'm hoping thats written wrong :P )

2

You can use a single query to do this and get directly the rows of the second query:

$sql = mysql_query("select * from servers, (select id as ids from servers where active = '1') as s where id = s.ids");

Then use the while loop as in my previous example to print/collect the result set:

while($row = mysql_fetch_array())
{
    echo $row['id'];
    # . . .
}
Votes + Comments
true :D
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.