2014-05-12 19 views
0

我想表明訪問者使用他們的id從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

+0

該數據不是臨時存儲,因此您必須將其保存到數據庫中。創建一個列可能是'hit_counter',然後在這個頁面上更新一個'jobid'爲$ _GET ['id']'的地方。你也應該通過IP地址過濾它,否則刷新會增加計數器(即使是同一個用戶)。 –

+2

注意SQL注入,不要直接從'$ _GET'或'$ _POST'使用params而不進行清理。 – Sal00m

+0

你有什麼嘗試?你有什麼問題?你在爭鬥什麼?如果您對如何使用PHP/MySQL缺乏一般理解,那麼閱讀教程或在YouTube上觀看教程可能會更好。 –

回答

0

至於說通過@I可以了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地址,他/她正在訪問的JOB ID,然後在表格中插入一個條目,如果他/她正在訪問表格條目中不存在的IP的JOB ID。現在

,你可以得到「的次數作業ID:$ _GET [」身份證「]被視爲」使用簡單的查詢:

$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 ['REMOTE_ADDR']提供的值。

您可以自由優化任何查詢,清理輸入內容,以不同方式創建表格。這只是實現你基本尋找的簡單方法之一。

+0

謝謝@learner,我想創建上面的數據庫內的數據庫,我已經有了工作的名稱。 – Rameen

+0

你的意思是你現有的數據庫裏面的'table'? – learner

+0

謝謝@learner,我想創建上面的數據庫內的數據庫,我已經有了工作的名稱。 這是我的數據庫結構: [鏈接](http://s2.postimg.org/67hzzqvqh/Capture.jpg) – Rameen