count & id [solved]
Moderator: General Moderators
count & id [solved]
Hi
You may remember that i have a form for users to submit articles and want the articles be publicly viewable.
after any article submition, the following are inserted to the database table. ( table fields):
id (index, auto increment),user, title, article text, ip etc.
Now I need 2 things:
1. I want any article to be accessed via a url like this:
http://domain.com?id
What should I do? ( similar to vbulletin forums)
2. I want to know how many times each article has been read. ( how many times the url was reloaded) like " view" times, in forums
You may remember that i have a form for users to submit articles and want the articles be publicly viewable.
after any article submition, the following are inserted to the database table. ( table fields):
id (index, auto increment),user, title, article text, ip etc.
Now I need 2 things:
1. I want any article to be accessed via a url like this:
http://domain.com?id
What should I do? ( similar to vbulletin forums)
2. I want to know how many times each article has been read. ( how many times the url was reloaded) like " view" times, in forums
Last edited by m2babaey on Tue May 22, 2007 6:43 am, edited 1 time in total.
1. add a unique primary key in the table where your articles are stored. Make it auto-increment.
> Using GET and a query "where articleid = '$id'" you can then select the article data.
Make sure to clean the GET string to prevent SQL injection.
2. add a field "views" in the articles table and make it increment when that particle row is queried.
You might want to add an IP check to prevent the same user adding pageviews by refreshing.
// also that immmediately ?id after a domain, maybe you mean something like this:
domain.com/articles/?1337
Use mod_rewrite for that
> Using GET and a query "where articleid = '$id'" you can then select the article data.
Make sure to clean the GET string to prevent SQL injection.
2. add a field "views" in the articles table and make it increment when that particle row is queried.
You might want to add an IP check to prevent the same user adding pageviews by refreshing.
// also that immmediately ?id after a domain, maybe you mean something like this:
domain.com/articles/?1337
Use mod_rewrite for that
ok1. add a unique primary key in the table where your articles are stored. Make it auto-increment.
This topic told me to use POST instead of GET to avoid long URLs. Please give me more explanation and example ( if possible) since it is only about 5 days I've started learning PHPh and MySQL!!!!Using GET and a query "where articleid = '$id'" you can then select the article data.
okadd a field "views" in the articles table
How?and make it increment when that particle row is queried.
Yes. It's very important for me. How do I do that? Also how can I have the ip from where the article was submitted recorded?You might want to add an IP check to prevent the same user adding pageviews by refreshing.
Thanks for your help
- You need to use POST to handle form input. that has nothing to do with the url set to an article id.
get the id from the url and use it as a variable to load the correct
get the id from the url and use it as a variable to load the correct
Code: Select all
if (isset($_GET['id']))
{
$article_id = preg_replace('/[^a-zA-Z0-9\_]/', '', $_GET['id']);
$result = mysql_query("SELECT * FROM articles WHERE article_id ='$article_id'") or die(mysql_error());
// do article display here
}
else {
// do default here
}get the current value in from the database and update it with +1and make it increment when that particle row is queried.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
You can use $_POST for this, but why? This is something made for the query string.
Code: Select all
<?php
$sql = 'SELECT * FROM `articles`';
if (!$result = mysql_query($sql))
{
die('Could not execute the query: ' . $sql . ' at ' . __LINE__ . ' in file ' . __FILE__);
}
// Set articles array
// And ids array
$articles = array();
$article_ids = array();
while ($row = mysql_fetch_array($result))
{
$articles[] = $row;
$article_ids[] = $row['id'];
}
// Snag the count of the articles
$articles_count = count($articles);
// See if we need to grab an article
$article = null;
if (isset($_GET['article']))
{
$id = intval($_GET['article']);
if (in_array($id, $article_ids))
{
$article = $id;
}
}
// Loop the values and show what is necessary
for ($i = 0; $i < $articles_count; $i++)
{
if (!is_null($article))
{
// We show the one value
if ($article == $articles[$i]['id'])
{
echo 'Single article data: ' . $articles[$i]['article_name'];
break;
}
}
else
{
// Run them all as links to view more detail
echo '<p><a href="' . basename(__FILE__) . '?article=' . $article[$i]['id'] . '">View more info on ' . $article[$i]['article_name'] . '</a></p>';
}
}- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
figured it out. thanksget the current value in from the database and update it with +1
Still having problem. This is the code I use to get the form input and insert it to the database:get the id from the url and use it as a variable to load the correctCode: Select all
if (isset($_GET['id'])) { $article_id = preg_replace('/[^a-zA-Z0-9\_]/', '', $_GET['id']); $result = mysql_query("SELECT * FROM articles WHERE article_id ='$article_id'") or die(mysql_error()); // do article display here } else { // do default here }
Code: Select all
<?php
$title = $_POST['title'];
$subject = $_POST['subject'];
$article =$_POST['article'];
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("articles") or die(mysql_error());
mysql_query("INSERT INTO articles (subject, title, text) VALUES('$subject','$title','$article')")
or die(mysql_error());
include("main.php");
mysql_close();
?>Now what does :
Code: Select all
if (isset($_GET['id']))
{ $article_id =$_GET['id']
$result = mysql_query("SELECT * FROM articles WHERE article_id ='$article_id'") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
echo $row['article'];
}
}
else {
// do default here
}Sorry that I have not understood yet, hope have it solved in this forum.
Thanks in advance.
Let's see this strange error:
I used this code:
that mainarticle.php has:
And the error is:
Parse error: parse error in g:\programs(2)\easyphp1-8\www\ha\callforread.php on line 7
You see there must be a problem with connectint to database. since line 7 is:
Why that happens?
thanks
I used this code:
Code: Select all
<?php
if (is_numeric($_GET['id'])
# Put your MySQL connection stuff here
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("articles") or die(mysql_error());
$article_id = $_GET['id'];
# Let's assume that the article id row is called 'id'.
$sql = "SELECT * FROM articles WHERE id = '$article_id'";
$result = mysql_query($sql) or die (mysql_error());
$row = mysql_fetch_assoc($result);
# Now, just echo $row['rowname'] for each thing you want displayed;
# $row['title'] is the article title, $row['text'] is the article text, etc.
include("mainarticle.php")
# Display the data in what way seems best to you.
}
else {
echo 'This article cannot be displayed';
}
mysql_close();
?>Code: Select all
echo $row['subject']."<br>".$row['title']."<br>".$row['text']."<br><br>";Parse error: parse error in g:\programs(2)\easyphp1-8\www\ha\callforread.php on line 7
You see there must be a problem with connectint to database. since line 7 is:
Code: Select all
mysql_connect("localhost", "root", "") or die(mysql_error());thanks
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA