Page 1 of 1
count & id [solved]
Posted: Mon May 21, 2007 8:36 am
by m2babaey
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
Posted: Mon May 21, 2007 9:20 am
by thiscatis
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

Posted: Mon May 21, 2007 9:59 am
by m2babaey
1. add a unique primary key in the table where your articles are stored. Make it auto-increment.
ok
Using GET and a query "where articleid = '$id'" you can then select the article data.
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!!!!
add a field "views" in the articles table
ok
and make it increment when that particle row is queried.
How?
You might want to add an IP check to prevent the same user adding pageviews by refreshing.
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?
Thanks for your help

Posted: Mon May 21, 2007 10:46 am
by thiscatis
- 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
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
}
and make it increment when that particle row is queried.
get the current value in from the database and update it with +1
Posted: Mon May 21, 2007 11:24 am
by RobertGonzalez
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>';
}
}
Posted: Mon May 21, 2007 11:31 am
by thiscatis
feyd would ban you if he caught you giving full example coding

Posted: Mon May 21, 2007 11:46 am
by RobertGonzalez
I figured it was enough to keep the OP busy trying to figure it out for a while. Give and take my friend, give and take.
Besides, I was in a coding mood a few minutes ago. I couldn't help myself.
Posted: Mon May 21, 2007 2:31 pm
by m2babaey
get the current value in from the database and update it with +1
figured it out. thanks
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
}
Still having problem. This is the code I use to get the form input and insert it to the database:
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();
?>
that main.php has a <? echo ($article); ?> in it.
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
}
do? Should insert this code be in the formprocessor.php file? or somewhere else?
Sorry that I have not understood yet, hope have it solved in this forum.
Thanks in advance.
Posted: Tue May 22, 2007 2:46 am
by m2babaey
Let's see this strange error:
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();
?>
that mainarticle.php has:
Code: Select all
echo $row['subject']."<br>".$row['title']."<br>".$row['text']."<br><br>";
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:
Code: Select all
mysql_connect("localhost", "root", "") or die(mysql_error());
Why that happens?
thanks
Posted: Tue May 22, 2007 10:31 am
by RobertGonzalez
No, the parse error is above it. THere are no closed parenthesis on the if statement and there is no opening brace for the conditional block.