有没有办法计算一张图片被点击的次数。单击图像后,是否显示该图像?
我目前正在从一个数据库中随机拉取图像,并想要显示哪个是被点击最多的。
<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"]."\"></a>"; } ?> </div>
<div style="float:left; margin-left:100px"> <?php include("image2.php"); ?>
</div> </body> </html>谢谢。
发布于 2011-02-06 13:37:16
在映像表中创建触发器以计数选择
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$$按计数器字段执行查询排序。
为了注册点击,实现了一个点击跟踪器来查询第二个字段的更新,比如' clicks‘
发布于 2011-02-06 13:27:24
您可以使用javascript来完成此操作:
在head标签中:
<script type="text/javascript">
var count = 0;
function changevar(){
count = count + 1;
if (count == 3) {
alert('Done');
}
}
</script>在body标签中:
<img src="image.png" onClick="changevar()"/>发布于 2011-02-06 22:14:48
如何计算每次显示图像的次数?这是一个选项,当且仅当没有其他图像可以以图像相册查看器工作的方式提供给它时。
使用php渲染图像,并在其中运行一个小脚本,该脚本会增加该图像被点击(或显示)的次数。或者,您可以在每次显示图像时创建一个条目,以保存更多可能更有趣的信息,如图像、last_view、ip、计数和/或引用(如果它工作正常;未经测试)。记录IP将允许您跟踪独特的视图,计数显示他们查看图像的次数;这取决于您如何实现它。
$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;如下所示显示图像
<img src="/image.php?img=example.png" />如果遇到问题且图像显示正确,请在浏览器中打开路径以查看错误。
https://stackoverflow.com/questions/4911740
复制相似问题