Jump to content

adding up a column by username


testing7

Recommended Posts

basically i have a referral column that saves the username of the referral now what i would like to do is add up that persons referral count. so say their name comes up 10 times in the column i would be able to output a 10 so they can see how many referrals they have gotten.

 

not even sure this is the right way to start but this is what i came up with (let me know if it looks right or completely wrong because i suck)

 

<?php
$total="SELECT SUM(user) AS user_total FROM referals WHERE user = user"; // user = user cant be right can it?
$result=mysql_query($total);
print "<tr>";
while($row = mysql_fetch_array($result))
{
	//Here is where im confused
               $sum = $row['user_total'];
	print "<td>Sum: $sum </td>";
}
?>

 

am i way off? or can it even be done?

Link to comment
Share on other sites

Hi testing7,

 

Do you want to display the count for an individual user (i.e. you are passing a value to the database for one user) or do you want to return the count for all users in the database?

 

For the first option, you can use something like this:

<?php
$user = 'John';
$total= mysql_query("SELECT user, COUNT(user) AS user_total FROM referrals WHERE user = '$user'");
echo '<tr>';
while($row = mysql_fetch_array($total))	{		               		
    echo '<td>User: '.$row['user'].', Count: '.$row['user_total'].'</td>';	
}
?>

 

For the second option you can use something like this:

<?php
$total= mysql_query("SELECT user, COUNT(user) AS user_total FROM referrals GROUP BY user");
echo '<tr>';
while($row = mysql_fetch_array($total))	{		               		
    echo '<td>User: '.$row['user'].', Count: '.$row['user_total'].' / </td>';	
}
?>

 

Hope this helps,

 

John

Link to comment
Share on other sites

i posted this code:

 

<?php
$total= mysql_query("SELECT user, COUNT(user) AS user_total FROM referrals GROUP BY user");
echo '<tr>';
while($row = mysql_fetch_array($total))	{		               		
    echo '<td>User: '.$row['user'].', Count: '.$row['user_total'].' / </td>';	
}
?>

 

getting the error on this line:

 

echo '<tr>';

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.