开发者

Value not saving in array

开发者 https://www.devze.com 2023-03-10 10:20 出处:网络
I have an array which have this structure: $queues[n] Array ( [id] => integer [idClient] => integer

I have an array which have this structure:

$queues[n] Array (
[id] => integer
[idClient] => integer
[name] => string
[people] => integer )

Which is populated with:

$query = "SELECT clients.idClient AS 'idClient', queues.idQueue AS 'idQueue', queues.name AS 'name' FROM clients, queues WHERE clients.idClient = queues.client";
    $queues = null;

    $result = mysql_query($query);
    if ($result){
        while ($queue = mysql_fetch_assoc($result)){
            $queues[] = array ("idClient" => $queue['idClient'], "id" => $queue['idQueue'], "name" => $queue['name'], "people" => 0);
        }
    }

Each 'n' value match a queue from Database and people, by default is set to 0.

After populating the array I query the database again with each queue to other table to obtain the number of people in queue with a query like this:

SELECT COUNT(*) FROM peoplequeued WHERE queue ='".$queue['name']."'

And then:

$result = mysql_query($query);
        if ($result){
            $num_people = mysql_fetch_row($result);
            $queue['people'] = $num_people[0];
        }

And something strange开发者_Python百科 happens here. If I echo the $queue['people'] in the foreach, it shows fine the value it got but if I preview the full array before returning it, it's back to 0.

What could be happening?


My guess is that you're looping through $queues with foreach loop like this:

foreach ( $queues as $queue ) {
    $result = mysql_query($query);
    if ($result){
        $num_people = mysql_fetch_row($result);
        $queue['people'] = $num_people[0];
    }
}

If so, there's no "link" between $queue and $queues[$n], i.e., by modifying $queue, you do NOT modify $queues.

If this is the case, you should either use $queue as reference variable, or modify $queues[$n] with $n being an index.

foreach ( $queues as &$queue ) { // add & so $queue is a reference to an element in $queues
    $result = mysql_query($query);
    if ($result){
        $num_people = mysql_fetch_row($result);
        $queue['people'] = $num_people[0];
    }
}
unset($queue); // drop the reference, otherwise you might have unexpected results after modifying $queue outside the loop

... or ...

foreach ( $queues as $n => $queue ) { // store index of "current" element in $n
    $result = mysql_query($query);
    if ($result){
        $num_people = mysql_fetch_row($result);
        $queues[$n]['people'] = $num_people[0]; // change $queue to $queues[$n]
    }
}

Alternatively, I would advise you to think about getting all data in a single statement. It looks like something like this might work for you:

select
    baseTable.id,
    baseTable.idClient,
    baseTable.name,
    peopleCount.count as people
from
    baseTable
    left join (
        select
            count(*) as count,
            queue
        from
            peoplequeued
        group by
            queue
    ) as peopleCount on peopleCount.queue = baseTable.name


It sounds like you're probably using a foreach loop to cycle through your $queues array, like this:

foreach($queues as $queue) {

}

Each $queue variable generated is a copy, and changes to it aren't saved to the $queues variable. To save changes as you're intending, you need to do something like:

foreach($queues as $k => $queue) {
    $queue['people'] = 10;
    $queues[$k] = $queue;
    // or, more efficiently...
    $queues[$k]['people'] = 10;
}

In PHP5 you can also reference. See http://uk.php.net/manual/en/control-structures.foreach.php

0

精彩评论

暂无评论...
验证码 换一张
取 消