图像点击计数器

时间:2011-02-06 05:17:44

标签: php mysql database

有没有办法计算图像被点击的次数。点击图像后会显示它吗?

我目前正在从数据库中提取随机图像,并希望显示哪个已被点击最多。

<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>

感谢。

3 个答案:

答案 0 :(得分:0)

您可以使用javascript:

执行此操作

头标记:

<script type="text/javascript">
    var count = 0;
    function changevar(){
            count = count + 1;
        if (count == 3) {
             alert('Done');
        }
    }
        </script>

身体标签:

<img src="image.png" onClick="changevar()"/>

答案 1 :(得分:0)

在图像表中创建一个触发器以计算选择

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$$

通过计数器字段执行查询排序。

要注册点击,请在第二个字段上实施点击跟踪器查询更新,例如“点击”

答案 2 :(得分:0)

每次显示图像时如何计算?当且仅当图像相册查看器的工作方式中没有其他图像时,这是一个选项。

使用php渲染图像,并在其中运行一个小脚本,该脚本会增加此图像被单击(或显示)的次数。或者,您可以在每次显示图像时创建一个条目,从而节省更多可能的有趣信息,例如image,last_view,ip,count和or referrer(如果它可以工作;未经测试)。通过记录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" />

如果遇到问题且图像显示正确,请在浏览器中打开路径以查看错误。