跟踪横幅视图

时间:2013-02-02 18:02:34

标签: php javascript

我想通过PHP和JS跟踪横幅视图的方式提供一些帮助。我知道如何跟踪横幅广告点击,但我找不到跟踪视图的方法。

2 个答案:

答案 0 :(得分:3)

如果您的横幅是图片,您可以使用记录视图并返回图像的PHP脚本替换它的URL。

例如:<img src="http://img.com/img.png"> - &gt;重写PHP脚本 - &gt;记录 - &gt;返回图片

答案 1 :(得分:1)

如果要永久存储标题/图像的查看次数,请修改数据库中包含图像标识的表格。

表横幅

id | path | clicks | views

<强> www.example.org/products.php?products=type_a

<?php
// other code
$type = $_GET["products"];
// sanitize $type
$views;
$products_selected = get_product_ids_from_type($type);
$query = "..."; // use $products_selected to create a query that will help to get image records from table
while($row = mysql_fetch_row(...)) { 
  echo "<img src='".$row[1]."' />";
  $views[$row[0]] = $row[3]; // get the old number of views
}
foreach($products_selected as $id) {
$query = "insert into `table-banner` (`views`) values ('".($views[$id] + 1)."') where id=`".$id."`;";
// perform the query
}

这意味着当有人点击链接 www.example.org/products.php?products=type_a 时,请求会转到服务器,因为选择了type_a个产品,你的.php文件将使用该类型来获取id并显示相应的图像,并在获取这些图像以供显示时,同时更新每个图像记录的视图列

如果您还想使用JavaScript(这不是必需的),您可以

<script>
var selected_ids = [
<?php
  foreach($products_selected as $v) {
    echo $v.",";
  }
?>
];</script>
<script>
  $(function() {
   for(x in selected_ids) {
    $.ajax({
      type: "POST",
      url: "ajax.php",
      data: { id: selected_ids[x] }
    }).done(function( msg ) {
      alert( selected_ids[x] + " now has views : " + msg );
    });
   }
  });
</script>

<强> www.example.org/ajax.php

$id = $_POST["id"];
// create connection
$view = 0;
$query = ...; // get old views from id and store in $view
// perform the query to insert new view
$query = "insert into `table-banner` (`views`) values ('".($view + 1)."') where id = `".$id."`;";
echo $view;