PHP - How To Count Number Of Article Views In Php Mysql?
Hello,
I am trying to create article directory for learning PHP MySql and i want to write a script for recording number of views for an article and store it in Database. Can anyone please direct me to a nice simple tutorial or help me with the steps or algorithm? Suppose the link for an article is http://www.domain.com/article.php?id=123 Do i need to put extra variables and create a tracking PHP file for it? Similar TutorialsHi, I have a web site that sells both video and images. The full versions are located securely behind a paywall, however in a bid to get visitors to purchase them and to promote the members offering I have trailer videos of 30 seconds to one minute and four free images per imageset. I want to be able to restrict the number of images a visitor can view for free to 12 and the number of videos a visitor can play to 5. With a little graphic/overlay notifying the user when they have exceeded their free play/preview limit. I have been advised that a tracking cookie and php if statement would be the way to go to achieve this. I have got a cookie that is successfully logging views that is written to on a free preview link as an onclick function. I have tried writing some php if code that will have the normal thumbnail or video divs display as usual if the views recorded in the cookie are below 12 or five, respectively, and have a not allowed, please subscribe div that would show over the thumbnails or video divs should the number of views exceed the threshold. My PHP code does not seem to be working at all, I have ran it through an online syntax checker - corrected the mistakes and now it's supposed to be working but opts to display nothing instead of either if argument. Videos then become a whole other problem because at the moment I have no other way to track the view/visit other than to count the loading of the video summary page with the free player as the preview, whether or not the clip is played - ideally I would like the view to be counted when the video begins to play. But I have no idea how to write to the cookie from Flash (is that even possible?) Any help would be greatly appreciated. My current project requires me to save the number of views an image (banner ad) has appeared on users website. The problem here is that the image will be appearing on several users website and i want to be able to save the number of times it has appeared on my database. I know how to do this if the image was on the same server as the database, but not when the image and database are on completely different servers. This is somewhat like analytic where it tracks the visitors to a page. Does anyone how i can accomplish this? i figured i would need to create a JavaScript code to provide to the user so they can place it on the site but how do i get that code to connect to my database? Hi all - I'm setting up a custom PHP blog. It pulls the data from a MySQL database which includes HTML tags (<p><div><span> etc...). I would like to display only up to 50 words per post on the blog page, which users can then read and click a link to then see the entire post. I've developed some code which does this, however, it seems to be stripping my HTML tags... Very sad! Would be very very grateful if one (or more) of you kind lot would have a look at my code and let me know if there is a easier (I'm all for easy) and proper way of implementing this so that it works without stripping my HTML tags. Cheers!!! Code: [Select] // Counts number of blog words in the content $blog_content_words = str_word_count($blog["content"], 1); // Sets the blog_words variable to 0 $blog_words = 0; // Prints title on page as a permalink to a post echo '<h1><a href="blog.php?id='.$blog["id"].'">'.$blog["title"].'</a></h1><p>'; // Loops while blog_word is under 50 while($blog_words < 50) { // Prints a word from blog_content_words array of blog post and adds a space afterwards echo $blog_content_words["$blog_words"]." "; // Adds 1 to the blog_words counter ++$blog_words; } // Adds a read more link to the post which links to full blog post echo '... <a href="blog.php?id='.$blog["id"].'">[read more]</a></p>'; Hi all, I'm trying to create a PHP script for user's profile to display the amount of times they've been viewed. I'm looking to have this script increase on a unique view, and it should update the variable in the database. Profiles are accessed by the following link: userprofile.php?userid=X (where X is the ID, e.g. 1, 2, 1001, 345982, etc.). The database variable I'm looking to update is called ProfileViews. I began developing the script, which is as follows: Code: [Select] $_SESSION['Viewed'] = 0; if ($_SESSION['Viewed'] == 0) { $profileViewsQuery = mysql_query("SELECT ProfileViews FROM Users WHERE UserID='????'"); $getProfileViews = mysql_fetch_array($profileViewsQuery); $profileViews = $getProfileViews['ProfileViews']; $profileViews = $profileViews + 1; mysql_query("UPDATE Users SET ProfileViews='$profileViews' WHERE UserID='????'"); $_SESSION['Viewed'] = 1; } However, I'm stumped on a couple things. Could you possibly help me out? 1. How can I get the script to recognize the link accessed's ID? E.g. when a user goes to userprofile.php?userid=1001, how can I get the script to identify the ID to update should be 1001? This is where the "????" would be replaced in the code. 2. On page load, the variable is always going to be $_SESSION['Viewed'] = 0, which isn't going to produce unique hits. Do you have any recommendations how I could achieve unique hits using this method? Thanks very much for reading. Hello folks, good day! I'm having trouble here selecting my desired data. I hope somebody will help me. Im having two tables. tbl_a seId course year 1 IT 1999 2 HE 1992 3 RT 1990 tbl_b id seId status 1 1 NO 2 2 YES I want to select A.*,B.* from tbl_a AS A, tbl_b AS B having status = NO and status IS NULL my desired output is to see the following. course year status IT 1999 NO RT 1990 is it possible? thank you in advance. Hi guys, I need your help. I am trying to insert the rows in the mysql database as I input the values in the url bar which it would be like this: Code: [Select] www.mysite.com/testupdate.php?user=tester&pass=test&user1=tester&email=me@shitmail.com&ip=myisp However i have got a error which i don't know how to fix it. Error: Column count doesn't match value count at row 1 <?php session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'mydbusername'); define('DB_PASSWORD', 'mydbpassword'); define('DB_DATABASE', 'mydbname'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var){ return mysql_real_escape_string(strip_tags($var)); } $username = clean($_GET['user']); $password = clean($_GET['pass']); $adduser = clean($_GET['user1']); $email = clean($_GET['email']); $IP = clean($_GET['ip']); if($username == '') { $errmsg_arr[] = 'username is missing'; $errflag = true; } if($password == '') { $errmsg_arr[] = 'PASSWORD is missing'; $errflag = true; } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { $sql = "INSERT INTO `members` (`username`,`email`,`IP`) VALUES ('$adduser','$email','$IP')"; if (!mysql_query($sql,$link)) { die('Error: ' . mysql_error()); } echo "The information have been updated."; } ?> Here's the name of the columns i have got in my database: Code: [Select] username IP I have input the correct columns names, so I can't correct the problem I am getting. Please can you help? I got to thinking late last night (which generally leads to trouble), so please be gentle if some educating is in order, as I suppose I will touch a few related issues. As I develop my database, I will include two seperate columns. One for the time/date that an order was placed. A second for the time/date that an order is updated (unless there's a better way, please inform me). I pressume that if a record is updated several times, it will continually overwrite the time/date to the point that I will only see the latest update. Now I was wondering if I could create a third column to keep count of the number of times a record had been updated, so if it were altered 16 times, I would know when the last update occurred, and have the count number also. Will something like this coding work? SELECT updates FROM myTable WHERE id="$id" $updates =n if n<1, n=1 }else{ n++ I need help with a count query and displaying it. Right now have the following query $query = "SELECT COUNT(*) as num1 FROM spoiler WHERE id='$id"; $total_trophies = mysql_fetch_array(mysql_query($query)); $total_trophies = $total_trophies[num1]; So I have another query that pulls how many trophies there are. That way I can do the following ? echo $total_trophies; echo 'of'; echo $row[trophy_count]; ?> But the problem is sometimes in my database I could have mulitple entrys per trophy. For Trophy 1 I might have only one record so It counts it correctly for trophy 2 I could have 3 records so It counts each one of those records when I only one to count it once for that trophy hi, I want to count the total number of multiple select that user has selected instead of getting the value the user has selected How should I go about doing the counting part? Code: [Select] <form action="1.php" method="post"> <select name="test[]" multiple="multiple"> <option value="one">one</option> <option value="two">two</option> <option value="three">three</option> <option value="four">four</option> <option value="five">five</option> </select> <input type="submit" value="Send" /> </form> <?php $test=$_POST['test']; if ($test){ foreach ($test as $t){echo 'You selected ',$t,'<br />';} } ?> How to count the number of files/images from a folder? How do you count the number of folders in a directory? Hi all, I am trying to use the Mysql COUNT function to count the number of records in a database where the manufacturer is the same and then generate a list of all manufacturers along with a number of associated records next to their name. I have got the script below to show the manufacturer in a fetch array but I can not get the number next to the manufacturer to work. A push in the right direction would be appreciated thanks. <?php include("cxn.php"); $query = "SELECT manufacturer FROM sales"; $result = mysqli_query($cxn,$query) or die(mysqli_error()); ?> <select name="manufacturer" id="manufacturer"> <option value="" selected="selected">Select a Manufacturer..</option>"; <?php while($row = mysqli_fetch_array($result)){ $query1 = "SELECT manufacturer COUNT(*) FROM sales WHERE manufacturer='$row[manufacturer]'"; // TRYING TO COUNT THE ROWS HERE $result1 = mysqli_query($cxn,$query1) or die (mysqli_error()); while($row1 = mysqli_fetch_array($result1)){ echo "<option value=\"202\">".$row['manufacturer'] ."[". $row1['COUNT(manufacturer)']."]</option>"; // SHOW NUMBER HERE } } ?> Hi. I have the following code where I grab the names and average overall rating from my database and group by the name. Code: [Select] $sql="SELECT name, AVG(overall) AS overall FROM $tbl_name GROUP BY name ORDER BY AVG(overall) DESC"; $result=mysql_query($sql); while($rows=mysql_fetch_array($result)){ Does anybody know how I can count the number of names that have been grouped together? For instance: Name Overal ratingl John 2 Pete 5 John 8 It should group the john's, calculate the average rating (8+2)/2=5, and then show that there have been 2 johns making up the 5 rating? I've tried using the code below and messed around with it loads but cant get it to work: Code: [Select] $sql="SELECT name, AVG(overall) AS overall FROM $tbl_name COUNT name GROUP BY name ORDER BY AVG(overall) DESC"; $result=mysql_query($sql); while($rows=mysql_fetch_array($result)){ echo $rows['Count name']; Thanks in advance. I have a html table displaying data from mysql database. I can count rows properly with mysql_num_rows but is there a way to echo which row number it is. What I want to do is count the rows ordered by cities and echo row number. Thanks for any help. i HAVE FOLLOWING VALUES IN FEED TABLE
a1a a1b a1c a1d ......
7 8 7 7
8 9 8 8
9 7 7 8
7 8 10 9
6 4 6 7
7 8 8 5
9 7 7 6
This in an array ( select a1a,a1b,a1c,a1d from feed where .....)
1) I want to know how many times each values has occured in the array ( eg: 7 occured 10 times, 8 occured 8 times)
2) How many 7's, 8's, 9's are there in a1a, howmany .....in a1b
Number of times each values occured OVERALL in array as well as also under each head
Please help
Folks, Having trouble here. Forgot how you use the COUNT. Not interested in the num_rows due to it being slow compared to COUNT. I have users table like this: id|username|sponsor_username 0|barand|requinix 1|phpsane|alison 2|mickey_mouse|requinix Now, I want to check if a sponsor username exists or not. Such as does the entry "requinix" exists or not in one of the rows in the "sponsor_username" column. In our example it does twice and so the variable $sponsor_username_count should hold the value "2". Else hold "0". I get error: Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?' at line 1
My code:
$stmt_1 = mysqli_prepare($conn,"SELECT COUNT(sponsor_username) from users = ?"); mysqli_stmt_bind_param($stmt_1,'s',$sponsor_username_count); mysqli_stmt_execute($stmt_1); //Show error if 'users' tbl was not successfully queried". if (!$stmt_1) { echo "ERROR 1: Sorry! Our system is currently experiencing a problem loading this page!"; exit(); } else { mysqli_stmt_bind_result($stmt_1,$sponsor_username_count); mysqli_stmt_fetch($stmt_1); mysqli_stmt_close($stmt_1); //Check if Sponsor Username in Url ($_GET) is already registered or not. If not then show "Invalid Url" or Link message. if ($sponsor_username_count < 1) { echo "<b>Invalid Url or Link!<br> Invalid Sponsor: \"$sponsor_username\"!</b><?php "; exit(); } }
Hi guys I'm kinda stuck..so hopefully you guys can lend me a hand I've got an array containing date elements ("Y-m-d")... I'm trying to output some data into googlecharts..so i need to count how many elements that are in the array with todays date -1 day, todays date-2 days...todays date -3days etc... Up until a set number of days (for example 7 days, 14 days etc).. Any advice on how to go about to achieve this? I have a database lets say punch_log The data in the table looks like: --------------------------------------------------------------------------- |punch_log_id | user_id | punch_id | punch_time | --------------------------------------------------------------------------- | 10010 | 21 | 1 | 2010-11-10 15:04:59| | 10011 | 21 | 2 | 2010-11-10 15:50:05| | 10010 | 21 | 1 | 2010-11-11 15:04:59| | 10011 | 21 | 2 | 2010-11-11 15:50:05| | 10010 | 21 | 1 | 2010-11-12 15:04:59| | 10011 | 21 | 2 | 2010-11-12 15:50:05| | 10010 | 21 | 1 | 2010-11-13 15:04:59| | 10011 | 21 | 2 | 2010-11-13 15:50:05| | 10010 | 21 | 1 | 2010-11-14 15:04:59| | 10011 | 21 | 2 | 2010-11-14 15:50:05| | 10010 | 21 | 1 | 2010-11-14 15:50:59| <-- this is why i need this. | 10011 | 21 | 2 | 2010-11-14 15:55:05| <-- this is why i need this. ---------------------------------------------------------------------------- Im currently using : $kust = $_POST['AKuu']; $kuni3 = $_POST['LKuu']; $valitudtootaja = $_POST['TNimi']; mysql_select_db($database, $con); $query2 = "SELECT *, SUM(punch_id) FROM punch_log WHERE user_id = '".$valitudtootaja."' AND punch_time BETWEEN '$kust' AND '$kuni3' AND punch_id ='1' "; $result2 = mysql_query($query2) or die(mysql_error()); while($row = mysql_fetch_array($result2)){ $X = $row['SUM(punch_id)']; When using the current query im getting Workers days at work = 6 it should be 5 but thats why i need some solution to group dates and then sum them. Does anybody have any ideas? Hi everyone, Like the title says I need to count the amount of 1's in a column but I can't figure out how. Gr and thanx already Ryflex Hi everyone
I have a requirement to create some reports and outputting these to a PDF. I'm using FPDF which seems to be rather user-friendly. I was hoping someone could help with me a problem I have regarding MySQL COUNT and PHP arrays.
I have a table named 'faults' and within this a row called 'level'. There are three 'levels' = P1, P2, P3 that could populate this row. I wish to display the output into a PDF so it looks something like this:
Total number of faults;
P1 P2 P3
1 2 3
Where I'm falling short is how to perform the COUNT and then grab the data stick it into an array and output it to the PDF. The MySQL statement you see below, if put directly into phpMyAdmin, seems to do the trick, so I'm hoping I'm on the right track. So far I have this:
<----OUTPUT OMITTED----> $result=mysqli_query($con, "SELECT COUNT(IF(level='P1',1, NULL)) 'P1', COUNT(IF(level='P2',1, NULL)) 'P2', COUNT(IF(level='P3',1, NULL)) 'P3' FROM faults"); //initialize counter $i = 0; //Set maximum rows per page $max = 25; while($row = mysqli_fetch_array($result)) { //If the current row is the last one, create new page and print column title if ($i == $max) { $pdf->AddPage(); //print column titles for the current page $pdf->SetY($y_axis); $pdf->SetX(25); $pdf->Cell(50,6,'Total P1s',1,0,'L',1); $pdf->Cell(50,6,'Total P2s',1,0,'L',1); $pdf->Cell(50,6,'Total P3s',1,0,'L',1); //Go to next row $y_axis = $y_axis + $row_height; //Set $i variable to 0 (first row) $i = 0; } //This bit definitely ain't right $p1 = $row['level']; $p2 = $row['level']; $p3 = $row['level']; $pdf->SetY($y_axis); $pdf->SetX(25); $pdf->Cell(50,6,$p1,1,0,'L',1); $pdf->Cell(50,6,$p2,1,0,'L',1); $pdf->Cell(50,6,$p3,1,0,'R',1); //Go to next row $y_axis = $y_axis + $row_height; $i = $i + 1; } //Send file $pdf->Output(); ?>Appreciate it must look a mess, but would appreciate someone cleverer than me pointing me in the right direction. |