View Full Version : How to count times faq link is viewed?

11-13-2009, 06:20 PM
Hi guys,

I am building a FAQ page and would like to include a way to know which questions are viewed most. Is there a way I could count the times a user clicks the question link.

At the moment I have a table with ID, question, answer, link and anchor. I plan to use do/while loops to display the questions and answers and include the links and anchors like this....

<?php do{ ?>
<a href="#<?php echo $link; ?>"><?php echo $question ?></a><br>
<?php while($questions = mysql_fetch_array($result)); ?>

<?php do{ ?>
<a name="<?php echo $anchor; ?>" id="<?php echo $anchor; ?>"></a><?php echo $answer; ?><br>
<?php while($answers = mysql_fetch_array($result)); ?>

Has anyone got any ideas how I can record the times a question is viewed.

Thanks, Paul

11-14-2009, 03:27 AM
using cookies perhaps?

11-14-2009, 10:52 AM
Thanks for the suggestion edbr but I am thinking of keeping track of the most popular question rather than who is reading them.

Also, I'm thinking that if users delete their cookies I will get false info.

I'm trying to think of a way to count the clicks on a question link and updating a field in my Database.

Any further input would be great.

On another note Edbr, things seem much slower on DWC these days. I've not been around for a while and things seem quiet. I notice Dj is not around any more either!!

Regards, Paul

11-15-2009, 12:15 AM
ah ok some kind of log table then.
re quiet there is often a feast and famine on the forum it will be busy when people get dreamweaver in their christmas stockings maybe :)
davidj is very involved in a new job and his tutoring site

11-15-2009, 02:01 AM
If you gaveeach link a valve you could collect as a $_GET and update a table based no that

11-15-2009, 10:08 PM
Thanks for the idea Edbr,

I did as you suggested and got the question ID (q) and value for the count (c) from the URL using $_GET. So my link was this.....

<a href="<?php $_SERVER['PHP_SELF']; ?>?q=<?php echo $questions['faqID']; ?>&c=1#<?php echo $questions['faqID']; ?>"><?php echo $questions['question']; ?></a>

My code to update the count and then get the questions and answers was this.....


$faqID = $_GET['q'];
$c = $_GET['c'];

if($faqID && $c){
$query5 = "SELECT * FROM faq WHERE faqID = $faqID ";
$result5 = @mysql_query($query5);
$getcount = @mysql_fetch_array($result5);

$count = $getcount['faqcount'];
$newcount = $count + $c;

$query = "UPDATE faq SET faqcount = '$newcount' WHERE faqID = '$faqID' ";
$result = @mysql_query($query);

$query6 = "SELECT * FROM faq ";
$result6 = @mysql_query($query6);
$questions = @mysql_fetch_array($result6);

$query7 = "SELECT * FROM faq ";
$result7 = @mysql_query($query7);
$answers = @mysql_fetch_array($result7);


I hope this helps someone else.

Regards Paul

11-18-2009, 01:27 PM
I created something like that years ago, called a view counter.
Create a page called "views.php" or any name you want, remember your link has to work prior to using this.
Replace your link with:views.php?id=(id_from_db)

//connect to database
//run your sql update
$sql="UPDATE tablname SET fieldname=fieldname+1 WHERE (id= '".$_GET['id']."')";
//check if any row was affected
if(mysql_affected_rows()==1){ header('Location: page_to_go_to.php?id='.$_GET['id']);
} else {
header('Location: error.php); }

11-18-2009, 02:24 PM
Thanks for that student101,

Sounds very similar to the way I have done it.