php - 使用作业 ID 的点击计数器

标签 php mysql

我想显示访问者使用从 MySQL 数据库调用的 ID 查看某个职位的次数。

下面是job.details.php页面中的代码。

<div style="width:900px;">
    <div style="float:left; width:200px;"class="ara-form"><header style="font-size:12px; color:#666666; font:Arial, sans-serif; padding:7px;"><?php

   $result = mysqli_query($conn,"SELECT * FROM job WHERE jobid = '".$_GET['id']."' ORDER BY `CreatedTime` DESC");

   $jobdetails = mysqli_fetch_assoc($result);

    echo '<strong>Job Title</strong></br> '.$jobdetails['positiontitle'].'<hr class="job">';
    echo '<strong>Company Name</strong></br> '.$jobdetails['companyname'].'<hr class="job">';
    echo '<strong>Location</strong></br> '.$jobdetails['location'].'<hr class="job">';
    echo '<strong>Closing Date</strong></br> '.$jobdetails['closingdate'].'<hr class="job">';
    echo '<strong>Number of Vacancy</strong></br> '.$jobdetails['numberofvacancy'].'<hr class="job">';
    echo '<strong>Job Category</strong></br> '.$jobdetails['jobcategory'].'<hr class="job">';
    echo '<strong>Duration</strong></br> '.$jobdetails['duration'].'<hr class="job">';
    echo '<strong>Employment Type</strong></br> '.$jobdetails['employmenttype'].'<hr class="job">';
    echo '<strong>Salary</strong></br> '.$jobdetails['salary'].'<hr class="job">';
    echo '<strong>Timing</strong></br> '.$jobdetails['timing'].'<hr class="job">';
    echo '<strong>Nationality</strong></br> '.$jobdetails['nationality'].'<hr class="job">';
    echo '<strong>Gender</strong></br> '.$jobdetails['gender'].'<hr class="job">';
    echo '<strong>Experience</strong></br> '.$jobdetails['experience'].'<hr class="job">';
    echo '<strong>Education</strong></br> '.$jobdetails['education'].'<hr class="job">';
    echo '<strong>Gender</strong></br> '.$jobdetails['gender'].'<hr class="job">';
    echo '<strong>Gender</strong></br> '.$jobdetails['gender'].'<hr class="job">';


?></header></div>
    <div style="float:left; width:600px;" class="ara-form">
    <fieldset style="font-size:12px; color:#666666; font:Arial, sans-serif;">
    <?php

    echo '<p><strong id="ara-form">Company Background</strong></br> '.$jobdetails['background'].'</p></br>';
    echo '<p><strong id="ara-form">Job Summary</strong></br> '.$jobdetails['summary'].'</p></br>';
    echo '<p><strong id="ara-form">Job Duties and Responsibilities</strong></br> '.$jobdetails['duty'].'</p></br>';
    echo '<p><strong id="ara-form">Qualification</strong></br> '.$jobdetails['qualification'].'</p></br>';
    echo '<p><strong id="ara-form">Skills</strong></br> '.$jobdetails['skill'].'<hr class="job"></p></br>';
    echo '<p><strong id="ara-form">Submission Guideline</strong></br> '.$jobdetails['submission'].'</p></br>';
    echo '<p><strong id="ara-form">Words to search this job</strong></br> '.$jobdetails['search'].'</p></br>';
    ?>
    </fieldset></div>
    <div style="float:left; width:33.4%; background:#ccc;">three</div>
    <div style="clear:both;"></div>
</div>

</div>

这是我从 MySQL 通过 ID 调用的作业示例: http://example.com/job/job.details.php?id=171

最佳答案

正如@I can Has Kittenz所说,它不是临时存储,需要持久化。 我可以建议创建一个类似的表:

mysql> create table `pageviews`(
->  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
->  `job_id` int,
-> `remote_host` varchar(45)
-> );

PHP 部分:

$ipaddress  =   $_SERVER['REMOTE_ADDR'];
$job_id     =   $_GET['id'];
/*
 * Checking if an entry exists for this JOB ID with this REMOTE HOST
 */
$query  =   "SELECT id FROM pageviews WHERE ".
                      "job_id=".$job_id." AND remote_host='".$ipaddress."'";
$result =   mysqli_query($dbc, $query);
if(mysqli_num_rows($result) !== 1){
/*
 * There is no entry for this job_id.
 * So create an entry
 */
    $query = "INSERT INTO pageviews (job_id,remote_host) VALUES ".
        "(".$job_id.",'".$ipaddress."')";
    $result = mysqli_query($dbc, $query);
    if($result){}
    else{
    echo 'There was a problem inserting the data';
    exit();
    }
} 

如果将上述 PHP 部分插入到您的 job.details.php 中,它将查看客户端的 IP 地址、他/她正在访问的作业 ID,然后如果他/她正在访问,则将一个条目插入到您的表中来自表条目中不存在的 IP 的作业 ID。

现在,您可以使用简单的查询获取“查看 jobid: $_GET['id'] 的次数”:

$query  = "select id from pageviews where job_id=".$job_id."";
$result = mysqli_query($dbc, $query);
echo "Number of times jobID: ".$job_id." was viewed = ".mysqli_num_rows($result);

注意:客户端 IP 可以位于代理服务器后面,也可以是来自代理后面的 LAN 的内部 IP。因此不能真正信任 $_SERVER['REMOTE_ADDR'] 提供的值。

您可以自由地优化任何查询、清理输入、以不同的方式创建表。这只是实现您基本目标的简单方法之一。

关于php - 使用作业 ID 的点击计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23602784/

相关文章:

php - 在php中使用静态类变量作为类名调用静态方法

php - localStorage 不维护 jquery 单击函数对页面刷新的影响

php - 通过 Web 界面向 PHP 脚本公开 MySQL?

mysql - 将混合编码的xml文件数据保存到mysql数据库中的utf-8

php - 在 CI 中使用 select2 在 php 中具有依赖性的多项选择

php - 如何计算每年每个月的行数,即使一个月不存在

php - 运行查询后如何重定向到另一个页面

php - 如何从 MySQL 日期时间中减去日期,其中表被指定为 MySQL 函数。

php - 如何更新多行在 MySQL 的 int 列中添加值?

php - pdf 缩略图 imagemagick php 不工作