Jump to content

Recommended Posts

I'm new to php, and just understand a few basic functions.  I have a table that records how many times a person's photo(s) has been viewed.  How do I go through the table and add up the number of times that all of the person's photos have been viewed?  The field names are "UserID" and "views".  Any help appreciated.

 

Stan

 

Link to comment
https://forums.phpfreaks.com/topic/43374-solved-how-do-i-get-the-sum-of-values/
Share on other sites

something like this:

$sql = "SELECT * FROM your_table WHERE user = '{$username}'";
$query = mysql_query($sql) OR die(mysql_error());

$views = 0;
while($row = mysql_fetch_array($query)){
        $views += $row['views'];
}

echo $views;

You can use the SUM() function. This will add every value in a field, not count them.

 

Table Name= mytable

Photo Views

  A        2

  B        2

  C        2

 

 

$sqlCount = "SELECT COUNT(Views) AS TotalViews FROM mytable"  =======>  TotalViews=3

 

$sqlSum = "SELECT SUM(Views) AS TotalViews FROM mytable"  =======>  TotalViews=6

I tried the first one:

 

<?

$sql = mysql_query("SELECT COUNT(*) FROM Photos WHERE UserID='$userID'");

$num = mysql_num_rows($sql);

echo $num;

?>

 

All it returns is "1".  I tried substituting a user number for $userID, but it still returned "1".

 

I tried the second one:

 

<?

$sql = "SELECT * FROM Photos WHERE UserID = '{$username}'";

$query = mysql_query($sql) OR die(mysql_error());

$views = 0;

while($row = mysql_fetch_array($query))

{

  $views += $row['views'];

}

echo $views;

?>

 

It returns "0".  I tried changing things around, but I still got "0".

 

The third one:

 

<?

$sqlCount = "SELECT COUNT(Views) AS TotalViews FROM Photos"  =======>  TotalViews=3

$sqlSum = "SELECT SUM(Views) AS TotalViews FROM Photos"  =======>  TotalViews=6

?>

 

Returns, Parse error: syntax error, unexpected T_IS_IDENTICAL.

 

Stan

 

I should add, that this table is dynamic, records are constantly being add to and deleted from it.  The table is therefore not in order.  I would think that some sort of SORT would be required, to get the "UsedID" in numerical order, then loop thru the "UsedID" until all had been counted, and then loop thru the next "UsedID", and on and on.  Also, as a note, the "UsedID" are not consecutive and have missing "UsedID" numbers.

 

Stan

 

You can use the SUM() function. This will add every value in a field, not count them.

 

Table Name= mytable

Photo Views

  A        2

  B        2

  C        2

 

 

$sqlCount = "SELECT COUNT(Views) AS TotalViews FROM mytable"  =======>  TotalViews=3

 

$sqlSum = "SELECT SUM(Views) AS TotalViews FROM mytable"  =======>  TotalViews=6

 

That is exactly correct. Try this:

 

<?php

$sql = "SELECT SUM(views) AS Views FROM Photos WHERE UserID = '{$username}'";
$query = mysql_query($sql) OR die(mysql_error());
$view = mysql_fetch_array($query);
$view = $view[0];

echo "There have been " . $view . " Photos viewed.";
?>

<?php

$sql = "SELECT UserID, SUM(Views) as totalviews FROM photos GROUP BY UserID";
$result = mysql_query($sql);

while ( $row = mysql_fetch_object($result) ) { $views[$row->UserID] = $row->totalviews; }

echo '<pre>';
print_r($views);
echo '</pre>';
?>

 

This will allow you to get total counts for all users, which I then put into an array with the userID as the key and view count as the value.

I got it to work.  This is what it took:

 

<?

$query = "SELECT UserID, SUM(Views) FROM Photos GROUP BY UserID";

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result))

{

echo "There are ". $row['SUM(Views)'] ." views for user ". $row['UserID'] ."";

echo "<br />";

}

?>

 

Thanks to everyone for their help!

 

Stan

 

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.