开发者

Problem looping through usernames

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;
    开发者_StackOverflow                    $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)";


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 ;-)


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜