I write some simple article scripts. Now I want add some article counter.
If there have 3 possibility it should make an article counter.
the article has been opened for read.(only one whole article per page, it will count 1 time)
the article has been searched in a content search list(with title and short content description 5 items per page, it will count 1 time. then if open to read whole article content, it will count another 1 time.)
the article has been showed in the home page by Random (with title and short description, it will count 1 time)
Is there any good suggestion how to do these better?
Which database design is better? put article and count number in one table? or make two tables?
Can anyone recommend me some php article counter script, if the main rules write into a file like class.php then include into my every page.
Thanks.
If you are counting the number of hits on an article, create a column and add one to it every time somebody accesses the page.
Something like:
$sql = "UPDATE table SET count=count+1 WHERE id='$id'";
mysql_query($sql);
Would increment the column count in the table table by 1. Then you could just retrieve that value.
What you could do is use the database that auto increments and just increment that value when the page is viewed, and display that value if you want.
Here is how you can setup the auto increment.
Then update the "value" (you don't have to actually update anything it will AI)
mysql_query("UPDATE COUNTER SET HITS = ''");
Then just display the view
$result = mysql_query("SELECT * FROM PAGEVIEWS");
while($row = mysql_fetch_array($result))
{
echo $row['COUNTER'];
}
This is off the top of my head - it should work.
Related
I am new to php, mostly learning from examples, and having a little bit of trouble.
I already searched for this, but couldnt find exactly what i was searching for, or at least couldnt understand it.
I am trying to create somekind of a "news" system. I have a database with 3 columns:
tid - auto-incremented, the id # for the news i post
created_by - who created it...obviously
text - the news themselves
So, is there a way that every time i post some new article it gets a new unique page, based on its id?
In example - mysite.com/news.php?id=3 will show the article with "tid"=3 (from the sql table).
Lets say news.php is my main file and i want its content to change everytime when the ?id in the url changes.
Thanks and wish you a successful week ahead.
<?php
$currentID = $_GET['current_id']; // get current id out of the link
// add some logic to validate it is a valid id to prevent sql injection
build the query which is get next post that is above
current id and limit it to 1 post only
"SELECT * FROM news where id > $currentID limit 1";
back link would turn the greater than to a less than. you would need to have checks to ensure you cant go below 0 and cant go max id number. A quick count of records would do it. or catch not found records to go back to the start
I am working on a project where only title of posts are shown on main page and on clicking the title, full post is loaded on another page posts.php code for this is:
<?php echo $row['title']; ?>
Is there any method to track how many times post with specific id was loaded on posts.php. In simple words I want to track how many times full post has been viewed on posts.php page, someone suggested count HTTP requests with this URL. So please guide me on this just few lines on how the thing works can help me a lot.
What you have to do is create a new column in your table, suppose it's name is 'hits'. Set the default value of it to 0 while creating a new row everytime.
$id = $_GET['postId'];
$sql = "UPDATE posts SET hits = hits + 1 WHERE post_id = $id"; //Suppose your table name is posts
You can add a new column in your database table like view_count. When you get data from database you can update view_count like
update table_name set view_count = view_count + 1 where post_id = $_GET['postId']
it will increment view_count every time when page will load.
You insert a code on top of your script post.php in order to count visitors.
Something like: http://hibbard.eu/how-to-make-a-simple-visitor-counter-using-php
If you prefer Database, you just have to change your code.
I am trying to sort the information given to me by the API of an engineering journal. I have extracted the following information into a table:
ID (integer),
Journal Entry Name (Text),
Description (Text),
Page Length (integer),
Has Media (boolean)
Each "Journal Entry" has only one ID associated with it. Each ID also has other characteristics that are not returned by the API but that I want to use to sort. They are:
Category (Things like Econ, Math, Biology. Each ID can have more than one category)
Boolean values (Things like requiring special subscriptions)
I have created a second table in the following format:
ID (integer),
Category (text),
Boolean1 (bool),
Boolean2 (bool),
Boolean3 (bool)
Since each ID can have more than one category, when this occurs another row is added to this table. The idea being that any given row only has one ID and one category in it.
What I want to do is this:
Be able to find the top ten categories when it comes to
Highest Journal Entry (ID) count
Highest Total Page Length
Highest Journal Entry count where the "Has Media" boolean is true
Create a means of navigating like "pagination" where each page shows the nth results of the aforementioned top ten.
So if I chose to Highest Journal Entry count method, the first page would be show IDs, Names, and Descriptions of all the Journal entries in the category with the highest count.
My plan has been to create a new table where the numbers one through ten are in the first column, and then populate the second column with the top ten categories. Then I can use a process similar to pagination in which the nth page only shows the values with the corresponding category from the original value. However I can't seem to be able to make this top ten list/matrix, nor do I know if it there is a better way.
Unfortunately I am not a MySQL or PHP coder by trade, and have only gotten this far by lots and lots of googling. I have been completely unable to find any guides for a navigation method like the one I want. And since I don't know the proper terminology, I am just trying random google searches at this point.
Is this the best way to go about it? Or would it be better to create a third table of some sort? Is there perhaps an easier way to do this with something that can use the PHP and MySQL code I already wrote?
Not sure I really understand what you're going for here, but my best guess is that you probably want to combine your two initial tables and have category be a set rather than an individual term so you can have a single entry per unique ID.
Then you'd just need to write calls for each of your top ten finds as needed. Since each id can have an unknown number of categories I would start with a limit of 10 and then process the returns starting with the top match, grab its categories, if there are more than 10 grab the first 10, if there are less than 10 grab what there are, update the amount you're looking for (if there were 4 then you're now looking for 6), and move on to the next best match.
Maybe something like this:
categories = null;
$delimeter = ',';
$count = 1;
$top10 = array();
$result = mysql_query("
SELECT *
FROM table_name
ORDER BY page_length DESC
LIMIT 10");
while($row = mysql_fetch_array($result) && $count <=10)
{
$categories = $row['categories'];
$id = $row['id'];
$splitcontents = explode($delimeter, $catagories);
foreach($splitcontents as $category){
if (!in_array($catagory,$top10)){
$top10[$count] = array(
'category'=>$category,
'journal_id'=>$id
);
$count++;
}
}
}
1) How do I add a simple page counter to a PHP page and insert the values in a MySQL table? Also I would need the MySQL table value to be updated with each new visit.
2) The trick is that the PHP page is a template for a variety of user generated landing pages. For example, I would like each of these pages to have their own separate counters:
examplesite.com/template.php?getvalue=bob
examplesite.com/template.php?getvalue=sam
examplesite.com/template.php?getvalue=samantha
My impression is that if I put the counter on the "template.php" file then it will add up all the visits from each user to a grand total. The output that I would like is to have each user only get counts for the individual landing page.
So, if there are a total of 12 visits, dispersed as follows:
examplesite.com/template.php?getvalue=bob had 4 visits
examplesite.com/template.php?getvalue=sam had 2 visits
examplesite.com/template.php?getvalue=samantha had 6 visits
then I would want bob's page counter to read as '4', sam's as '2' and samantha's as '6.' Am I correct in assuming that if I just put the counter on template.php that each user's landing page would read as '12?' Do you have a solution for an easy way to fix this?
That's pretty simple:
pdo::prepare( 'UPDATE counter SET hits = hits+1 WHERE value = ?');
pdo::execute($_GET['getvalue']);
if ( pdo::rowCount() == 0 ) {
pdo::prepare('INSERT INTO counter (?,0)');
pdo::execute($_GET['getvalue']);
}
Well, just as you are able to separate the requests for bob and sam when generating the page, you can do the same for the counter?
You'll probably do something with $_GET['getvalue']. Just use that value (escaped, paramterized etc) in a query to update your counter....
UPDATE yourtable SET count = count + 1 WHERE pagename = ?
and then bind the getvalue...
You would want to have a row in your MySQL table for each user. When you go to update the table, update the appropriate row for that user by reading the getvalue. Then do the same for displaying the user's page counter.
What is the best way to count page views for dynamic pages like the url example below? I'm using PHP and MySQL. A brief explanation would help. Thanks!
http://www.example.com/posts/post.php?id=3
Usually the table structure looks like this:
table pages:
id | name | ...
==========================
1 Some Page
2 Some Other Page
table pages_views:
page_id | views
================
1 1234
2 80
where pages_views has a unique index on page_id
The MySQL statement to increment the views then looks as follows:
INSERT INTO `pages_views` SET views=1 WHERE page_id=?
ON DUPLICATE KEY UPDATE views=views+1 ;
Since pages_views.page_id is unique, the row for the page will get created if it doesn't exist; if it exists (that's the "duplicate key" clause), the counter will be incremented.
I chose two separate tables here, as CMS pages usually aren't updated too often (and therefore, their load is mostly reads), whereas page views are read and updated, well, with each page view.
This is my code and it's working properly when I open the page or When I refresh the page, page views is incrementing by 1. If the page_id doesn't exist it will insert a record with views = 1, if page_id exists it will increment the views
`INSERT INTO pages_views ( pages_id, views) VALUES ( $page_id, 1) ON DUPLICATE KEY UPDATE views=views+1`
With PDO you will have something like this
$sql = "INSERT INTO pages_views ( pages_id, views) VALUES ( :pageId, 1) ON DUPLICATE KEY UPDATE views=views+1";
$q = $conn->prepare($sql);
$q->execute(array(':pageId'=>$pageId));
Well, you can simply add a field pageviews to your pages table and do UPDATE pageviews = pageviews +1 WHERE id = 1 query on each page load
For the sake of viewing statistics by day/week/month/year, I have made two tables. The first archives all visits to the site with my page and id saved on the same row. The second table records tallys, such as Piskvor describes.
The benefit is that I can view stats for any page and ID I want over time (but that'll be a lot of rows over time...) or I can simply view total pageviews. For the visitors of my site, I serve information from this second table, but my admin panel makes full use of the first table.
statsEach
- statID
- page (example: page 100 is index.php, or 210 is news.php)
- id (example: 1 is news story 1, 2 is news story 2,...)
- date
- time
- user
and
statsTotal
- statTotalID
- page
- id
- total
I don't know what you need/want to do, or even if my table structure is best, but this works for me.
Just increase an integer on the post you currently serve.
A simple example can be found at http://www.configure-all.com/page_view_counter.php