image click counter
Is there a way to count how many times an image how been clicked. And once the image is clicked have it displayed?
I am currently pulling random images from a database and want to show which is has been clicked the most.
<html> <body>
<div style="float:left"> <?php // Connect to the database mysql_connect ('localhost', 'root') ; mysql_select_db ('links');
// Number of images $num_displayed = 1 ;
// Select random images from the database $result = mysql_query ("SELECT * FROM links ORDER BY RAND() LIMIT $num_displayed");
// For all the rows that are selected while ($row = mysql_fetch_array($result))
// Display images { echo "<a href=\"".$row["link"]."\"><img src=\"".$row["image"]."\" border=0 alt=\"".$row["text"]."\">&l开发者_JAVA技巧t;/a>"; } ?> </div>
<div style="float:left; margin-left:100px"> <?php include("image2.php"); ?>
</div> </body> </html>
thanks.
Create a trigger in image table to count selects
CREATE TRIGGER `database_name`.`trigger_name` BEFORE SELECT INSERT ON
`database_name`.`images_table_name` FOR EACH ROW
BEGIN
UPDATE
`database_name`.`images_table_name`
SET
`database_name`.`images_table_name`.`counter` = `database_name`.`images_table_name`.`counter` + 1
WHERE
`database_name`.`images_table_name`.`id` = NEW.`id`;
END$$
Perform your querys ordering by counter field.
To register clicks, implements a click tracker quering an update on second field, like 'clicks'
You can do this using javascript:
In head tag:
<script type="text/javascript">
var count = 0;
function changevar(){
count = count + 1;
if (count == 3) {
alert('Done');
}
}
</script>
In body tag:
<img src="image.png" onClick="changevar()"/>
How about counting each time the image is displayed? This is an option if and only if there are no other images which can be to it in the way an image album viewer works.
Use php to render the image, and run a small script in there which increments the number of times this image has been clicked (or displayed). Alternatively you could create an entry for each time the image is displayed saving lots more possibly interesting information such as image, last_view, ip, count and or referrer (if it works;untested). Recording the IP would allow you to keep track of unique views with count showing you how many times they reviewed the image; this depends on how you implement it.
$name = trim($_GET['img']);
if (!isset($_GET['img'] || empty($name)) {
// Check url var wasn't omitted or typed incorrectly.
die("Image not specified.");
}
// This is just an example path. It would be a good idea to specify a path
// like this to ensure that people don't try and use it to display files
// that you wouldn't want them too.
//eg. images you don't want to keep records of.
$image = "/images/$name";
$date = time();
$ip = $_SERVER['REMOTE_ADDR'];
$ref = $_SERVER['HTTP_REFERER'];
if (!file_exists($image)) {
// Ensure that something exists at $image
die("Invalid image.");
}
$f = fopen($image, 'r');
if (!$f) {
// Make sure that the contents of the file can be opened.
die("Unable to open image.");
}
$info = @imagegetsize($image);
if (!$info) {
// This is to make sure that the $image contains a path
// to an image not just a regular file.
die("Invalid image type.");
}
Header("Content-type: {$info['mime']}");
echo fread($f, filesize($image));
fclose($f);
/****************
* Script for saving image 'click' information.
***************/
exit;
Display the image like this
<img src="/image.php?img=example.png" />
If you are having troubles and images are displaying correctly, open the path in your browser to see the errors.
精彩评论