简体   繁体   English

PHP链接点击计数器

[英]PHP Link Click counter

I want to write simple counter for links/tags. 我想为链接/标签编写简单的计数器。 So I have tags and random displayed them to the website 所以我有标签并将它们随机显示在网站上

$zmienna = "SELECT name, link FROM tag_content ORDER BY RAND() LIMIT 4";
$result2 = mysql_query($zmienna);
echo $result2;
while($row=mysql_fetch_array($result2)){
    echo "<a href='http://www.simplelink.xx/tag/".$row['link']."'>".$row['name']."</a><br>";
}

And now I want to count how many users clicked tags. 现在,我要计算点击标签的用户数量。 I created another row named "wys" and tried to write SQL stuff 我创建了另一个名为“ wys”的行,并尝试编写SQL内容

$wtf = "UPDATE tag_content SET wys=wys+1 WHERE id=2";
$result3=mysql_query($wtf);

As u can see it only works for tag id = 2. And now the problem: how to make it work for all tags? 如您所见,它仅适用于标签id =2。现在的问题是:如何使其适用于所有标签?

For example: I have 4 tags with different id. 例如:我有4个具有不同ID的标签。 How to make counter "read" the actual clicked tag and make it add "1" to the "wys"? 如何使计数器“读取”实际单击的标记,并使其在“ wys”中添加“ 1”?

Thx for help and let me know if u need more information (like code etc.) 寻求帮助,如果您需要更多信息(例如代码等),请告诉我

As the link field is unique, you can simply use it as an identifier instead of id . 由于link字段是唯一的,因此您可以简单地将其用作标识符而不是id

$wtf = "UPDATE tag_content SET wys=wys+1 WHERE link='".$something."";

where $something is a last part of page URL (you should parse it). 其中$something是页面URL的最后一部分(您应该解析它)。 Of course you also need to check this variable before you use it, because you got it from client side and it could have code for SQL-injection. 当然,您还需要在使用此变量之前对其进行检查,因为您是从客户端获得该变量的,并且该变量可能包含用于SQL注入的代码。

You need a way to get the id of all links. 您需要一种获取所有链接的ID的方法。 Either provide it as a query parameter for the link, or use parse out the name and url from the clicked link and make a relevant SELECT to get the id to loop over. 可以将其提供为链接的查询参数,或者使用解析出的链接中的名称和URL并进行相关的SELECT来获取ID来进行循环。

使用jquery:eq()选择器找出确切的链接,然后发出ajax请求以更新计数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM