2015-03-19 75 views
2

嗨我有我的index.php頁面的搜索功能。當用戶鍵入一個字段並輸入'search'時,它會轉到jobsearch.php並從mysqli數據庫打印出作業摘要。鏈接到彈出的查看通過PHP查詢

我創建了結果結尾的鏈接。 因此,當用戶看到工作總結時,用戶點擊鏈接(點擊此處),然後將用戶引導至彈出視圖,該視圖顯示有關工作的更多信息(作業_描述)。有沒有辦法做到這一點?我不想爲每個作業創建一個html頁面,因爲數據庫中可能有100個作業。

我希望點擊此處鏈接獲取所需作業的ID並在彈出頁面上打印出作業說明。

它看起來像這樣:

enter image description here

這是jobs.php打印出的作業列表..

<?php 
$servername = "*****"; 
$username = "root"; 
$password = "*****"; 
$dbname = "jobslist"; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 
$sql = "SELECT id, job_title, job_description, job_location, job_category FROM jobs_list"; 
$result = $conn->query($sql); 

//display table 
echo "<table border='1'> 
<!--<tr> 
<th>ID</th> 
<th>Title</th> 
<th>Description</th> 
<th>Location</th> 
<th>Category</th> 
</tr>-->"; 

if ($result->num_rows > 0) { 
    // output data of each row 

    while($row = $result->fetch_assoc()) { 

echo "<tr>"; 
     echo "<td min-height:'200' ><h2>". $row["id"] ."</h2></td>"; 
     echo "<td>". $row["job_title"] . "</td>"; 
     echo "<td min-width:'700' >". $row["job_description"] . "</td> " ; 
     echo "<td>". $row["job_location"] . "</td>"; 
     echo "<td>". $row["job_category"] . "</td> " ; 

//this prints out a clickable link for every job 

echo "<td><a href='" . $row['id'] . "'>Click Here</a></td>"; 



     echo "</tr>"; 
    } 

} 
else { 
    echo "0 results"; 
} 

echo "</table>"; 
$conn->close(); 
?> 
+2

爲此,您可以使用2個選項。首先是在php中創建一個採用一個參數並在其中顯示所有作業數據的頁面,然後調用該頁面。第二個選擇是使用jQuery來發出ajax請求並獲取所需的所有細節,然後在彈出窗口中顯示它們 – 2015-03-20 06:58:38

+0

'$ row ['course_url']'是什麼?你能用它來鑑別一項獨特的工作嗎? – 2015-03-23 14:55:16

+0

道歉...將其更改爲「ID」 – Superunknown 2015-03-23 15:00:05

回答

3

變化

//this prints out a clickable link for every job 

echo "<td><a href='" . $row['id'] . "'>Click Here</a></td>"; 

echo "<td><a target='jobdescriptionwindow' href='jobdescription.php?id=" . $row['id'] . "'>Click Here</a></td>"; 

當你路過作業的ID的鏈接點擊進入jobdescription.php點擊這將打開一個新窗口(稱爲 'jobdescriptionwindow')。您還必須確保您的瀏覽器不會停止打開此窗口。

創建一個名爲jobdescription.php這些內容的新的PHP文件:

<?php 

if (isset($_GET['id'])) { // Check ID is is present in parameters 

    $servername = "*****"; // <-- Enter your details 
    $username = "root"; 
    $password = "*****"; // <-- Enter your details 
    $dbname = "jobslist"; 

    // Create connection 
    $conn = new mysqli($servername, $username, $password, $dbname); 

    // Check connection 
    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 

    // I do: (int)$_GET['id'] to type cast the value of $_GET['id'] to protect against injection. http://php.net/manual/en/language.types.type-juggling.php 

    $sql = "SELECT id, job_title, job_description, job_location, job_category FROM jobs_list WHERE id = ".(int)$_GET['id']; 

    $result = $conn->query($sql); // <-- Added new line 
    $row = $result->fetch_assoc(); 


    echo $row['job_title'] . '<br />'; 
    echo $row['job_description']; //<-- Does now show your description? 

    var_dump($result); // Lay out this data as required 
    // Outputs object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(5) ["lengths"]=> array(5) { [0]=> int(1) [1]=> int(23) [2]=> int(102) [3]=> int(4) [4]=> int(2) } ["num_rows"]=> int(1) ["type"]=> int(0) } 



} else { 

    echo "No ID provided"; 

} 

這將讓細節在GET參數發送作業ID。

+0

謝謝,我恐怕它似乎不工作。我不斷收到內部服務器錯誤。 – Superunknown 2015-03-27 09:46:21

+0

我有一個錯誤,指出: 注意:未定義的變量:導致/var/www/html/jobdescription.php上線24 致命錯誤:調用一個成員函數FETCH_ASSOC()一個非對象在/var/www/html/jobdescription.php在線24 – Superunknown 2015-03-27 10:18:23

+1

糟糕,我的錯誤:添加了一個新行:$ result = $ conn-> query($ sql); // < - 添加新行 – beingalex 2015-03-27 10:32:23

3

你並不需要生成每一個頁面工作。您可以生成一個獲取作爲GET參數(getJob.php?id = 5)的作業id的php,從該數據庫中獲取數據庫中的數據並將數據作爲HTML頁返回。

0

jobs.php的href - >

echo "<td><a href='something.php?id=".$row['id']. "'>Click Here</a></td>"; 

Something.php:

if(isset($GET_['id'])){ 
    $getid = $_GET['id']; 

    $sql = "SELECT job_title, job_description, job_location, job_category FROM jobs_list WHERE id = '".$getid."'"; 

    $result = $conn->query(sql); 

    while($row = $result->fetch_assoc()){ 

    echo $row['job_title'].'<br />'; 
    echo $row['job_description'].'<br />; 
    echo $row['job_location'].'<br />; 
    echo $row['job_category']; 
    } 
} 

但是這個代碼可攻擊與SQL注入!您需要轉義$ _GET參數,或者您需要學習/使用PDO。

1

除了beingalex的代碼,您還可以使用fancybox通過ajax而不是新窗口在彈出窗口中加載內容。 看到這個頁面: http://fancyapps.com/fancybox/

看看具體的例子ajax。 在此頁面上搜索'ajax'。這正是您需要的。

0

包含jQuery,Bootstrap和顯示模式彈出窗口的信息。模式窗口打開工作標題鏈接點擊。

<?php 
    $servername = "*****"; 
    $username = "root"; 
    $password = "*****"; 
    $dbname = "jobslist"; 

    // Create connection 
    $conn = new mysqli($servername, $username, $password, $dbname); 
    // Check connection 
    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 
    $sql = "SELECT id, job_title, job_description, job_location, job_category FROM jobs_list"; 
    $result = $conn->query($sql); 
    $tbdata = ""; 
    $modals = ""; 
    if ($result->num_rows > 0) { 
    // display table if results exist 
    echo "<table class=\"table table-striped\"><tr><th>Job Title</th><th>Location</th><th>Category</th></tr>"; 

    while($row = $result->fetch_assoc()) { 
    $tbdata.="<tr><td><a href=\"#\" data-toggle=\"modal\" data-target=\"#jbinfo_". $row["id"] . "\">". $row["job_title"] . "</a></td><td>". $row["job_location"] . "</td><td>". $row["job_category"] . "</td></tr>"; 
    $modals.="<div class=\"modal fade\" id=\"#jbinfo_". $row["id"] . "\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"jbLabel". $row["id"] . "\" aria-hidden=\"true\"><div class=\"modal-dialog\"><div class=\"modal-content\"><div class=\"modal-header\"><h4 class=\"modal-title\" id=\"jbLabel". $row["id"] . "\">". $row["job_title"] . "</h4></div><div class=\"modal-body\">". $row["job_description"] . "</div><div class=\"modal-footer\"><button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">Close</button></div></div></div></div>"; 
    } 
    echo $tbdata; 
    echo "</table>"; 
    echo $modals; 
    } else { 
     echo "No results"; 
    } 

    $conn->close(); 
    ?> 

您的結束標記的地方之前執行以下操作:

<!-- Latest compiled and minified CSS --> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"> 
<!-- Optional theme --> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
<!-- Latest compiled and minified JavaScript --> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>