Jump to content

How to a make a PHP Counter


frank_solo

Recommended Posts

So I'm trying to make a counter that counts how many times a record on mysql database has been viewed. Not a hit counter for a webpage. In other words I have a page that displays the info of that record from the mysql database and within that page I would like to display the amount of times it has been viewed. Would anyone know where I can begin or send me to a good tutorial?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/249426-how-to-a-make-a-php-counter/
Share on other sites

  Quote

So I'm trying to make a counter that counts how many times a record on mysql database has been viewed. Not a hit counter for a webpage. In other words I have a page that displays the info of that record from the mysql database and within that page I would like to display the amount of times it has been viewed. Would anyone know where I can begin or send me to a good tutorial?

 

Thanks

Add another column (an int) to the table called 'views'

 

Whenever the web page runs the query, just have another query increasing the view count:

 

UPDATE `table` SET `views` = `views` + 1 WHERE `id` = ''

  • 2 weeks later...

Sorry for not responding quick enough but I'm just getting over a bad case of pneumonia. So this is the script I currently have where would I add it to the current script which is this:

<?php
include "dbaptsConfig.php";
include "searchaptsstyle.css";
    	
// test id, you need to replace this with whatever id you want the result from
$id = (int)$_GET['id'];
$id = substr($id, 0,5);

if($id < 1 || $id > 99999) exit;

// what you want to ask the db
$query = "SELECT * FROM `apartments` WHERE `id` = $id";

// actually asking the db
$res = mysql_query($query, $ms);

// recieving the answer from the db	(you can only use this line if there is always only one result, otherwise will give error)	
$result = mysql_fetch_assoc($res);

// if you uncomment the next line it prints out the whole result as an array (prints out the image as weird characters)
// print_r($result);		

// print out specific information (not the whole array)		
echo "<div id='back'><div align='center'><FORM><INPUT TYPE='button' VALUE='BACK TO LIST' onClick='history.go(-1);return true;'></FORM></div>";
echo "<div id='table'><div align='left'><tr>"; 	
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Provider's Phone Number: <span style='color:#00F'>".$result['phone']."</span><br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Contact: <span style='color:#00F'>".$result['contact']."</span><br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Office: <span style='color:#00F'>".$result['office']."</span><br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Provider's E-Mail: <a href='mailto:".$result['email']."?subject=".$result['title']."'>".$result['email']."</a><br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Provider: <span style='color:#00F'>".$result['service']."</span><br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Borough: <span style='color:#00F'>".$result['county']."</span><br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Town: <span style='color:#00F'>".$result['town']."</span><br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Bedrooms: <span style='color:#00F'>".$result['rooms']."</span></td>";
echo "<td>   </td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Bathrooms: <span style='color:#00F'>".$result['bath']."</span><br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Pets: <span style='color:#00F'>".$result['pets']."</span><br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Lease Type: <span style='color:#00F'>".$result['lease']."</span><br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Square Footage: <span style='color:#00F'>".$result['square']."</span>ft<sup>2</sup><br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Rent: $<span style='color:#00F'>".$result['rent']."</span><br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Fees: <span style='color:#00F'>".$result['fees']."</span><br /></td>";
echo "<td bgcolor='#FFFFFF' style='color: #000' align='center'> Listed On: <span style='color:#00F'>".$result['date_created']."</span><br /></td>";
echo "<br/>";
echo "</tr></div>";
?>
</

 

If the update script is this:

 

UPDATE `table` SET `views` = `views` + 1 WHERE `id` = ''

 

Thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.