您好,我的 index.php 页面中有一个搜索功能。当用户在字段中输入“搜索”时,它会转到 jobsearch.php 并从 mysqli 数据库中打印出工作摘要。
我已经创建了结果末尾的链接。 因此,当用户看到职位摘要时,用户会点击链接(点击此处),然后将用户定向到一个弹出 View ,其中显示有关该职位的更多信息(职位_描述)。是否有这样做的方式?我不想为每个职位创建 html 页面,因为数据库中可能有 100 个职位。
我希望点击此处链接获取所需职位的 ID 并在弹出页面上打印出职位描述。
看起来像这样:
这是打印作业列表的 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();
?>
改变
//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 的详细信息。
我是一名优秀的程序员,十分优秀!