Jump to content

array output not what I expected - need a review of small code


xwishmasterx

Recommended Posts

I have a problem with the below code:

<?php
$sql_ranks = ("SELECT vtp_members.id, vtp_members.name, vtp_members.teamleader, teams.team_name, count(vtp_tracking.id) surfs
FROM vtp_members, vtp_tracking, teams
WHERE vtp_members.team_id=".$_GET['t']." AND vtp_tracking.credit_members_id=vtp_members.id AND vtp_tracking.action_date > '$last_sunday' AND vtp_tracking.action_date < '$next_sunday'
GROUP BY teams.team_name
ORDER BY surfs DESC");
$rsranks = mysql_query($sql_ranks);
echo "<br><table align='center' valign='top' border='0' width='300px'>
<tr><td colspan='2' align='center'><font size='2px'><b>Team Rankings (Current Week)</b></font></td></tr>
<tr><td><font size='2px'><b>Team</font></td><td align='right'><font size='2px'>Total Surfs</font></td></tr>";
while ($row = mysql_fetch_array($rsranks)) {
echo "<tr><td><font size='2px'><b>".$row[team_name]."</font></td><td align='right'><font size='2px'>".$row[surfs]."</font></td></tr>";}
echo "</table>";
?>

 

Problem is that the last output (".$row[surfs].") is the same for all teams. It seems it is not making a total of all id's and not per team_name.

 

anyone can see what I am doing wrong. I need to sort by team_name and the surfs should display the total of the members with team_id is ".$_GET['t']."

Link to comment
Share on other sites

Hi

 

Are team_name and surfs set up as constants? If not then you need quotes around them when using them as indexes to the $row array.

 

Also you are not joining the teams table to the others. It will just do a cross join, bringing back a silly number of records

 

You should also GROUP BY all non aggregate columns.

 

Something like this is needed (based on a guess of joining vtp_members and team on team_id and id).

 

<?php
$sql_ranks = ("SELECT vtp_members.id, vtp_members.name, vtp_members.teamleader, teams.team_name, count(vtp_tracking.id) surfs
FROM vtp_members
INNER JOIN vtp_tracking ON vtp_tracking.credit_members_id = vtp_members.id 
INNER JOIN teams ON vtp_members.team_id = teams.id 
WHERE vtp_members.team_id=".$_GET['t']." 
AND vtp_tracking.action_date > '$last_sunday' AND vtp_tracking.action_date < '$next_sunday'
GROUP BY vtp_members.id, vtp_members.name, vtp_members.teamleader, teams.team_name
ORDER BY surfs DESC");
$rsranks = mysql_query($sql_ranks);
echo "<br><table align='center' valign='top' border='0' width='300px'>
<tr><td colspan='2' align='center'><font size='2px'><b>Team Rankings (Current Week)</b></font></td></tr>
<tr><td><font size='2px'><b>Team</font></td><td align='right'><font size='2px'>Total Surfs</font></td></tr>";
while ($row = mysql_fetch_array($rsranks)) 
{
echo "<tr><td><font size='2px'><b>".$row['team_name']."</font></td><td align='right'><font size='2px'>".$row['surfs']."</font></td></tr>";
}
echo "</table>";
?> 

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.