Forum Moderators: open
Story: I have this private formula result quessing page. Where we are quessing formula comptetition results.
There is tables: users, games, bets.
Structure is like this:
users: id, username, ...
bets: user_id, game_id, .....
games: id, name, ...
And as you can understand, thing goes like this. If user named Example (ID: 4) is quessing results for game Turkish GP (ID: 10). Then bet goes to table bets. Where user_id is then 4 and game_id 10 and then there is hes/shes bets.
<?php
//Lets get all games from DB and already made bets counting.
$query_games = @mysql_query("
SELECT Count(B.game_id) AS kpl, G.*
FROM bets B
LEFT JOIN games G
ON B.game_id = G.id
GROUP BY G.id
ORDER BY G.id DESC");
//This is used to check is there any games on DB.
$check_games = @mysql_num_rows($query_games);
//Lets get all usernames from DB who hasn't made any bets to game. There is only 12 users so this isn't so heavy query.
//I have problem with this query. It doesn't work.
$nobets = @mysql_query("
SELECT G.username, Count(B.game_id) AS count
FROM users U, games G
LEFT JOIN bets B
ON (U.id = B.user_id AND B.game_id = B.id)
GROUP BY B.id
HAVING count=0
ORDER BY B.id DESC");
//Lets get busy
echo '
<h1>Open games</h1>
<table width="100%">
<tr>
<td width="40%"><b>Competition name</b></td>
<td width="20%"><b>Competition added</b></td>
<td width="16%"><b>Competition ends</b></td>
<td width="14%"><b>Bets</b></td>
<td width="9%"><b>Status</b></td>
</tr>';
while($f = @mysql_fetch_array($query_games))
{
//Beauty script. If number is smaller than 10 then add zero to front of number.
if($f['count'] < 10) { $count_bets = '0'.$f['count']; } else { $count_bets = $f['count']; }
$array = array("n" => array('<span class="locked">Locked</span>', '<span class="open">Open</span>', '<span class="closed">Closed</span>', '<span class="open">Results</span>'));
echo '
<tr>
<td><a href="?p=bet&id='.$f['id'].'">'.$f['username'].'</a></td>
<td>'.$f['start_datetime'].'</td>
<td>'.$f['end_datetime'].'</td>
<td title="';
echo 'Remaining bets: ';
//When you put your mouse over this cell. You can see usernames that hasn't made hes quess yet.
while($nobets_fetch = @mysql_fetch_array($nobets))
{
echo $nobets_fetch["username"].", ";
}
echo '">('.$count_bets.'/'.$count_users.')</td> <td>'.$array["n"][$f['status']].'</td>';
}
echo '</table>';
?>