tags:

views:

61

answers:

3

I am trying to loop through an array of usernames and for each username, execute a mysql_query on each.

<?php
                    for ($i = 0; $i < count($u); $i++) 
                    {
                        $j = $i + 1;
                        $s = $database->checkUserPlayedRecent($u);
                        if (mysql_num_rows($s) > 0)
                        {
                            ?>
                            <tr>
                                    <?php
                                        echo "<td>$j</td>";
                                    ?>

                                    <?php
                                        echo "<td>$u[$i]</td>";
                                    ?>
                                    <?php
                                        echo "<td>$p[$i]</td>";
                                    ?>
                            </tr>
                        <?
                        }
                    }
                    ?>

As you can see, $u is each username. I want each username to only appear in the echo statements if each one has num_rows > 0.

At the moment nothing is displaying. But there should be results being returned!

Can someone help please.

The sql: $q = "SELECT id FROM ".TBL_CONF_RESULTS." WHERE (home_user = '$u' OR away_user = '$u') AND date_submitted >= DATE_SUB(CURDATE(),INTERVAL 14 DAY)";

+3  A: 

This line :

for ($i = 0; $i < count($u); $i++) 

indicates that $u is an array -- and not a single name.
And $i is used to keep track of the current index in that array.


So, in your loop, you should be working with $u[$i] -- which will be the "current" line of the array :

$s = $database->checkUserPlayedRecent($u[$i]);


Note that you could probably rewrite your loop, using a foreach loop, like this :

foreach ($u as $currentPlayer) {
    $s = $database->checkUserPlayedRecent($currentPlayer);
    // ...
}

With foreach, no need to keep track of the current index -- makes code easier to write and understand, in my opinion ;-)

Pascal MARTIN
+1 for identifying the root problem
Mark Baker
Shouldn't the `$u` in the foreach loop body be `$currentPlayer`?
nikic
You're using the wrong variable in your foreach loop for `checkUserPlayedRecent` (it should be `$currentPlayer`). It's also good to make sure that $u isn't empty when you use a foreach, otherwise you get a warning when it is. `if (!empty($u)) { ... }`
JustJohn
Ergh... Copy-pasting is bad :-( ;; thanks for the comments, I've fixed that :-)
Pascal MARTIN
A: 

I'd be tempted to rewrite the query to accept the array of names and use an IN statement, so that you could execute the whole of your database activity in a single query rather than once for every entry in the array. It would almost certainly be faster. If you show us the query that you're using in your checkUserPlayedRecent() method, we may be able to help with this.

Mark Baker
Hey mate, i will edit the question with the checkUserPlayedRecent query!
Luke
A: 

You should get in the habit of keeping count() outside of your conditional. You're count()ing the same array every time which is a waste of cycles.

$total = count($u);

for ($i=o; $i < $total; $i++) ...

I would definitely query these users all at once, especially since your query is abusing mysql_num_rows when you should be using the following sql:

select username, count(username) from user where username IN (your,array,of,usernames) group by username;

then your foreach loop would iterate over the results and you could reference each row without having to call yet another mysql_* method.

David O.
Thankyou for the advice about count. I understand it and it makes perfect sense. Small things like this improve the speed no end when the website gets busier and I am always up for improving on that front.I dont really understand what the sql statement is doing though!
Luke
My guess is that checkUserPlayedRecent() accepts a username and then returns all rows that it finds from a table for that username. You then use mysql_num_rows() to check to see if results were found. With the SQL statement I provided above you can instead get a list of the usernames and their total # of rows in the table by using the combination of count/group by. This will return a result set that you can use mysql_fetch_assoc() on. I would suggest reading up on the basics of joins/counts/groups in mysql. It can save you lots of extra calls to the database :)
David O.
Now I think I understand, it selects each username, then does something and groups by the username to get a chunk of rows which its counts to give you how many. Is there anyway this can be combined into the sql i provided at the bottom of my question!
Luke
Because you have an OR statement here you need to use mysql WITH ROLLUP, which will create an extra row that sums up your other tallies. To include the count in this query it'd be the following: $q = "SELECT home_user, count(1) as total FROM ".TBL_CONF_RESULTS." WHERE (home_user = '$u' OR away_user = '$u') AND date_submitted >= DATE_SUB(CURDATE(),INTERVAL 14 DAY)" group by home_user WITH ROLLUP;
David O.