Jump to content

Recommended Posts

Hi

 

I need some help am trying to count how many times a tutorial has been viewed in total for all tutorials. I can count how many tutorial views they've been from the rows but as my tutorial views increment each time the tutorial is viewed its not accurate.

 

What I need is to be able to count the values from the table from each row and add them together.

 

here is my table scheme:

 

  `tutorialID` int(4) NOT NULL auto_increment,
  `tutorialTitle` varchar(255) NOT NULL default '',
  `tutorialDesc` text,
  `tutorialCont` text,
  `category` varchar(40) NOT NULL default '',
  `tutorialDate` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `tutorialviews` varchar(30) NOT NULL default '0',
  PRIMARY KEY  (`tutorialID`)

 

Here is the code am using to count

 

<?php
$count_sql = "SELECT * FROM tutorials WHERE tutorialviews";
$count_result = mysql_query($count_sql);
$count = mysql_num_rows($count_result);
?> 
    <p>Total Views <?php echo $count;?></p>

 

This is the code I use to update my views when veiwing a tutorial

 

//first update views
$update = "UPDATE tutorials ".
         "SET tutorialviews = tutorialviews + 1 ".
		 "WHERE tutorialID = '$tutorialID'";
mysql_query($update) or die('Error : ' . mysql_error());

 

at present I have 80 views on one tutorial 30 views on another just not sure how to add them together to display the total views.

 

Any advice would be great

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/63704-solved-count-values-rather-then-rows/
Share on other sites

okay am sure am doing this completly wrong...

 

    <p>Total Tutorials <?php echo $count;?></p>
<?php $result = mysql_query("SELECT SUM(tutorialviews + tutorialviews) FROM tutorials"); ?> 
    <p>Total Views <?php echo $result;?></p>

 

as I get "Total Views Resource id #6" instead of the value

when I tried the following code I thought it worked great but it some how comes to the number 186 but when i look in the db and add the value of each tutorialviews up its 93 so i don't know where its getting 186 from so i still need a way to add all tutorialviews up so i can get the total number.

 

 

$result = mysql_query("SELECT SUM(tutorialviews * tutorialviews) FROM tutorials");
$row = mysql_fetch_row($result);

echo "<p>Total Views $row[0]</p>";

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.