473,386 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Add 1 to existing table row if condition...

155 100+
I want to track how many times an article gets read by a unique reader. In other words, if the same person opens the same article a hundred times, it still only counts as one when it comes to the number displayed in the row "accessed" in the "articles" table.

The row titled "accessed" starts out with "0" and grows by 1 each time a unique or different reader opens the article.

I need some ideas as to how I should setup the database table to do this.

I'll have tables:
  • submit_article_users - rows: s_user_id, s_user_name, etc.
  • articles - rows: article_id, s_user_id, accessed, title, content
  • read_article_users - rows: r_user_id, r_user_name
  • track - rows: article_id, r_user_id

[PHP]<?php
$article_id' = $_GET['article_id''];
$query = "SELECT * FROM articles WHERE article_id = '$article_id'";
$result = mysql_query ($query);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$title = stripslashes($row['title']);
$article_id = $row["article_id"];
$s_user_name = stripslashes($row['s_user_name']);
$s_user_id = $row["s_user_id"];
}
echo "<strong>$title</strong> &nbsp;by $s_user_name<br><br>$content";


// Check if article has been opened before by reader.

$query = "SELECT r_user_id FROM track
WHERE r_user_id = '".$_SESSION['r_user_id']."'
AND article_id = '$article_id'";

$result = mysql_query ($query);
if ($result) { // Reader has accessed this article before so do nothing.

Should I put this here or leave this area blank?
include ('includes/bottom.php'); // Include the footer.
exit();

}
else
{

// The reader has not accessed this article before so add '".$_SESSION['r_user_id']."' and $article_id to table track and increase $accessed by 1 in the articles table.

// I'll need help with how to do this

$query = "INSERT into tract VALUES ('$article_id', '".$_SESSION['r_user_id']."');
$result = mysql_query ($query);

$accessed = ($accessed + 1);
$query = "UPDATE articles SET accessed='$accessed' WHERE article_id = $article_id";
$result = mysql_query ($query);
}

include('includes/bottom.php'); // Include the footer.
?>[/PHP]

I have not tried this code yet because I know that it would fail. I would like to know if what I have here would come anywhere close to doing what I need it to do? Any help woud be appreciated.

Thanks.
David
Jan 27 '08 #1
2 1609
dlite922
1,584 Expert 1GB
I want to track how many times an article gets read by a unique reader. In other words, if the same person opens the same article a hundred times, it still only counts as one when it comes to the number displayed in the row "accessed" in the "articles" table.

The row titled "accessed" starts out with "0" and grows by 1 each time a unique or different reader opens the article.

I need some ideas as to how I should setup the database table to do this.

I'll have tables:
  • submit_article_users - rows: s_user_id, s_user_name, etc.
  • articles - rows: article_id, s_user_id, accessed, title, content
  • read_article_users - rows: r_user_id, r_user_name
  • track - rows: article_id, r_user_id
[PHP]<?php
$article_id' = $_GET['article_id''];
$query = "SELECT * FROM articles WHERE article_id = '$article_id'";
$result = mysql_query ($query);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$title = stripslashes($row['title']);
$article_id = $row["article_id"];
$s_user_name = stripslashes($row['s_user_name']);
$s_user_id = $row["s_user_id"];
}
echo "<strong>$title</strong> &nbsp;by $s_user_name<br><br>$content";


// Check if article has been opened before by reader.

$query = "SELECT r_user_id FROM track
WHERE r_user_id = '".$_SESSION['r_user_id']."'
AND article_id = '$article_id'";

$result = mysql_query ($query);
if ($result) { // Reader has accessed this article before so do nothing.

Should I put this here or leave this area blank?
include ('includes/bottom.php'); // Include the footer.
exit();

}
else
{

// The reader has not accessed this article before so add '".$_SESSION['r_user_id']."' and $article_id to table track and increase $accessed by 1 in the articles table.

// I'll need help with how to do this

$query = "INSERT into tract VALUES ('$article_id', '".$_SESSION['r_user_id']."');
$result = mysql_query ($query);

$accessed = ($accessed + 1);
$query = "UPDATE articles SET accessed='$accessed' WHERE article_id = $article_id";
$result = mysql_query ($query);
}

include('includes/bottom.php'); // Include the footer.
?>[/PHP]

I have not tried this code yet because I know that it would fail. I would like to know if what I have here would come anywhere close to doing what I need it to do? Any help woud be appreciated.

Thanks.
David
first two things: validate $_SESSION['r_user_id'] (secure it)
ie make sure its a valid one.

i assume you don't care if non-registered users see this article they don't count or can't access it anyway.

Other than that you're on the right track.

Alternatively, you can have one table and you just INSERT the user_id and article_id into it if no such row exists.

example if JANE has never accessed article 54, then add this row into the table.

To check if JANE has accessed it, use your method above, if $result is not empty, then she's accessed it.

Which is what you have,

Then to see how many people have accessed a particular article just do SELECT count(*) FROM article WHERE article_id = '54';

What advantage this has over yours is that it tells you EXACTLY which users have access the article.

So you can create something like a most recently accessed article for the user homepage, etc ... many possibilities.

If this information is beyond your outlook of this project, i'd just do what you arlready have.

good luck.
Jan 28 '08 #2
DavidPr
155 100+
Thanks, the SELECT count(*) is a good idea. May also be a good idea to add a date row to see how recent the article was accessed. You're right - many possibilities here.

Yes, only certain registered users will be able to access the articles.

Thanks for the help.
Feb 2 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: CMAR | last post by:
I have the following markup. The problem is that the browser, e.g., IE6, inserts several lines of blank space between the <div> and the following table. Is there a way to minimize that vertical...
11
by: Randell D. | last post by:
Folks, I have a table of addresses and a seperate table with contact names - All addresses tie to one or more names - I would like to keep track of the number of names 'belonging' to an address...
5
by: garydevstore | last post by:
Hi, I have a table defined as CREATE TABLE ( IDENTITY (1, 1) NOT NULL , NULL , NULL , NOT NULL , NULL , (255) COLLATE SQL_Latin1_General_CP1_CS_AS
4
by: lorirobn | last post by:
Hello, I'd be curious to hear other thoughts on my database and table design. My database is for 'space use' in a lodging facility - will hold all spaces, like rooms (lodging rooms, dining...
33
by: Geoff Jones | last post by:
Hiya I have a DataTable containing thousands of records. Each record has a primary key field called "ID" and another field called "PRODUCT" I want to retrieve the rows that satisy the following...
2
by: Timothy Perrigo | last post by:
I'm working on a plpgsql function that creates and populates a temporary table. I would like the function to first drop the temp table, if it already exists. I'm not sure how to accomplish this,...
5
by: das | last post by:
hello all, this might be simple: I populate a temp table based on a condition from another table: select @condition = condition from table1 where id=1 in my stored procedure I want to do...
9
by: shanevanle | last post by:
I have two tables that are pretty big. I need about 10 rows in the left table and the right table is filtered to 5 rows as well. It seems when I join the tables in the FROM clause, I have to...
5
by: DaveLock | last post by:
Hi, I have 3 tables of data created from different sources, each with the same 2 fields & I wanted to fill the 2nd field of another table with the data on condition. The condition is because...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.