PHP Help

Blasteh

Lord Farquaad
Apr 3, 2013
1,151
513
So basically, I'm trying to exclude ranks from being shown in a top stats chart. Yes, I'm talking Habbo, but this is PHP I'm trying to get assistance with.

So right now, it's selecting all users – but I want it to select "rank < 2" so it's only people less than rank 2 that are being shown.

As you can see, there is two tables being selected at once, which makes it a little harder due to the fact it selects the users ID from user_stats.

Code:
<?php
                        $numberRespect = 0;
                        $getRespect = mysql_query("SELECT `respect`,`id` FROM `user_stats` ORDER BY respect desc");
                        while($respectStats2 = mysql_fetch_array($getRespect)) {
                            $respectq = mysql_query("SELECT * FROM `users` WHERE `id` = '" . $respectStats2['id'] . "'");
                            $respectStats = mysql_fetch_assoc($respectq);
                           
                            if ($numberRespect > 4)
                                continue;
                           
                            if ($respectStats['rank'] > 100)
                                continue;
                           
                            $numberRespect++;
                           
                            echo '
                            <tr>
                            <table width="100%">
                                <td width="25%"><img src="https://avatar-retro.com/habbo-imaging/avatarimage?hb=img&figure=' . $respectStats['look'] . '&size=m&direction=2&head_direction=2&gesture=sml&headonly=1&size=m" align="left"></td>
                                <td width="75%"><a href="{url}/home/'.$respectStats['username'].'"><b>'.$respectStats['username'].'</b></a><br />'.$respectStats2['respect'].' Recevied</td></table><hr>
                            </tr>';
                        }
                        ?>
 

RastaLulz

fight teh power
Staff member
May 3, 2010
3,926
3,921
This is more of a MySQL question than a PHP one. Ideally you want to do this in one query, by joining tables.

Some along like this is what you're looking for:
Code:
SELECT `user_stats`.`respect`, `user_stats`.`id`
FROM `user_stats`
INNER JOIN `users` ON `users`.`id` = `user_stats`.`id`
WHERE `users`.`rank` < 2
ORDER BY `user_stats`.`respect` DESC
 

Blasteh

Lord Farquaad
Apr 3, 2013
1,151
513
This is more of a MySQL question than a PHP one. Ideally you want to do this in one query, by joining tables.

Some along like this is what you're looking for:
Code:
SELECT `user_stats`.`respect`, `user_stats`.`id`
FROM `user_stats`
INNER JOIN `users` ON `users`.`id` = `user_stats`.`id`
WHERE `users`.`rank` < 2
ORDER BY `user_stats`.`respect` DESC
When you say joining tables, would the following be acceptable?
Code:
<?php
                        $numberRespect = 0;
                        $getRespect = mysql_query("SELECT `user_stats`.`respect`, `user_stats`.`id`FROM `user_stats`INNER JOIN `users` ON `users`.`id` = `user_stats`.`id`WHERE `users`.`rank` < 2 ORDER BY `user_stats`.`respect` DESC");
                        while($respectStats2 = mysql_fetch_array($getRespect)) {
                            $respectStats = mysql_fetch_assoc($respectq);
                          
                            if ($numberRespect > 4)
                                continue;
                          
                            if ($respectStats['rank'] > 100)
                                continue;
                          
                            $numberRespect++;
                          
                            echo '
                            <tr>
                            <table width="100%">
                                <td width="25%"><img src="https://avatar-retro.com/habbo-imaging/avatarimage?hb=img&figure=' . $respectStats['look'] . '&size=m&direction=2&head_direction=2&gesture=sml&headonly=1&size=m" align="left"></td>
                                <td width="75%"><a href="{url}/home/'.$respectStats['username'].'"><b>'.$respectStats['username'].'</b></a><br />'.$respectStats2['respect'].' Recevied</td></table><hr>
                            </tr>';
                        }
                        ?>

Never really had to deal with a joining query.
 

Users who are viewing this thread

Top