使用作业ID命中计数器

时间:2014-05-12 06:55:57

标签: php mysql

我想表明访问者使用他们的id从MySQL数据库中调用了多少次作业。

以下是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>

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

1 个答案:

答案 0 :(得分:0)

正如@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();
    }
} 

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

现在,您可以获得“被查看次数的次数:$ _GET [&#39; id&#39;]被查看&#39;使用简单的查询:

$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可以位于代理服务器后面,或者IP可以是来自代理服务器后面的LAN的内部IP。所以不能真正相信$ _SERVER提供的价值[&#39; REMOTE_ADDR&#39;]。

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

相关问题