Dynadot โ€” .com Registration $8.99

Explanation needed of a Php line

Spaceship Spaceship
Watch
Impact
19
hey
i am reading a tutorial and it doesnt explain so well..but so far i get almost everything..except for this line..can anyone help me out? heres the entire function:

PHP:
function listpms($uid)
{
        $pms = array();
        $query = mysql_query("SELECT * FROM `messages` WHERE `to` = '$uid' ORDER BY `time` DESC");
        while($r = mysql_fetch_assoc($query))
        {
                $pms[$r['id']] = array('id' => $r['id'], 'to' => $uid, 'from' => $r['from'], 'title' => $r['title'], 'message' => $r['message']);
        }
        return $pms;
}

so it creates an array called $pms..then selects all the enteries from the messages table where the field "to" = "$uid"
but then..here is where i get confused..the entire while array..specially when its filling up the $pms array..can anyone explain it to me?
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
It sets $r to each result row until there are no more rows. Then it adds some of the information to the $pms array with the key being the row's id.
 
0
•••
so it wont be in a nice ascending order right? like 0 1 2 3 4 because the key is the rows id..and its a PM table and its getting only the rows for a specific user..so the id could be : "5,100,105" right?
 
0
•••
Heres:
PHP:
$pms[$r['id']] = array('id' => $r['id'], 'to' => $uid, 'from' => $r['from'], 'title' => $r['title'], 'message' => $r['message']);

For example, $pms[1][to] would be the $uid, $pms[1][from] would be the $r['from']. And then each time it would increase the ID so if you had five records come up:
$pms[232]
Inside: $pms[232][id], $pms[232][to], $pms[232][from], $pms[232][title], $pms[232][message]
$pms[352]
Inside: $pms[352][id], $pms[352][to], $pms[352][from], $pms[352][title], $pms[352][message]
$pms[534]
$pms[673]
$pms[241]

Etc etc etc No it wont be in the ascending order, it will start from the first row id found. And yes theoretically the ID could be that... if you had 5,100,105 records and more in that database.

If you wanted $pms to go up in ascending order you could use:
PHP:
$pms[] = array('id' => $r['id'], 'to' => $uid, 'from' => $r['from'], 'title' => $r['title'], 'message' => $r['message']);
But that would probably muck up something else in the script :)
 
0
•••
unknowngiver said:
so it wont be in a nice ascending order right? like 0 1 2 3 4 because the key is the rows id..and its a PM table and its getting only the rows for a specific user..so the id could be : "5,100,105" right?
Right, but that doesn't really matter at all. You can still loop through them all normally.
PHP:
foreach ($pms as $id => $pm) { ...
 
0
•••
If you change the phrase ORDER BY `time` to ORDER BY `id`, it will give you the 0 1 2 3 4 5 6 ... ordered list you are talking about.
 
0
•••
JRBHosting said:
If you change the phrase ORDER BY `time` to ORDER BY `id`, it will give you the 0 1 2 3 4 5 6 ... ordered list you are talking about.
Don't do that :) You will muck up the way the script orders things :)
 
0
•••
Alternatively, you could just run a sort() call on the array with the results...

Anyways, couldnt you do ORDER BY `time` AND `id`, or have I not read up on SQL in too long?
 
0
•••
You don't need to use an order statement in the SQL because of the each row is refference in the $pms[] array.

$pms[1] will always return the row where the id is equal to one
 
0
•••
No... $pms[HERE] is reference to the ID in the database... the script may be written to use the order of the sql result to order the array and print the results.

ie, if id's were 234 283 199 in that order then the script COULD be programmed to go:
Code:
foreach($pms as $key=>$value) {
echo $value['id'];
}

And it would print
234
283
199

It could do it like that if say it was the top 10 news stories or something.
 
0
•••
None of that is relevant or needed. The key of the array doesn't matter at all in this case.

It is sorted by the highest time to the lowest time, so the id's would be more like 105, 100, 5.

(Also, the time and id would be in the same order.. so that makes no difference.)
 
0
•••
Appraise.net
Unstoppable Domains
Domain Recover
DomainEasy โ€” Live Options
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back