php - 如何根据 HTML 项目 ID 动态进行查询?
问题描述
所以,我有这段 HTML 代码,它只是一个导航栏。
<div class="views">
<b>Views</b>
<hr style="background-color:#f0efef;">
<table id="views_table">
<tr class="item">
<td class="view" id="my_open_cases">My Open Cases</td>
</tr>
<tr class="item">
<td class="view" id="my_pending_cases">My Pending & On Hold Cases</td>
</tr>
<tr class="item">
<td class="view" id="my_solved_cases">My Solved Cases</td>
</tr>
<tr class="item">
<td class="view" id="my_csat_cases">My CSat Cases</td>
</tr>
<tr class="item">
<td class="view" id="my_open_cases">My Open Cases</td>
</tr>
<tr class="item">
<td class="view" id="WW Support Tier 1"><a href="escalationreview.php"><div>Escalation Review</div></a></td>
</tr>
<tr class="item">
<td class="view" id="WW Support Tier 1"><a href="wwt1.php"><div>WW Support Tier 1</div></a></td>
</tr>
</table>
</div>
然后我有这个 php 文件,它在我的 MySQL 数据库中进行查询并在表中显示所有信息。
<?php
if ( !$conn = new mysqli("localhost", "root", "", "zendesk_data") ){
$output="Connection failed: " . $conn->connect_error;
} else {
$sql = "SELECT status, id, subject, requester, requested, requested_updated, service, next_sla_breach FROM escalationreview";
if ( $result = $conn->query($sql) ){
if ($result->num_rows > 0) {
$output="<table class='queue_table'>
<tr align='left'>
<th>Status</th>
<th>ID</th>
<th>Subject</th>
<th>Requester</th>
<th>Requested</th>
<th>Requested Updated</th>
<th>Service</th>
<th>Next SLA Breach</th></tr>";
while($row = $result->fetch_assoc()) {
$output.= "<tr><td>". $row["status"]. "</td><td><a href='../tickets/new.php?tid=" . $row["id"] . "'>" . $row["id"]. "</a></td><td>" . $row["subject"]. "</td><td>" . $row["requester"]. "</td><td>" . $row["requested"]. "</td><td>". $row["requested_updated"]. "</td><td>".
$row["service"]. "</td><td>". $row["next_sla_breach"]. "</td></tr>";
}
$output.="</table>";
} else {
$output= "0 results";
}
} else {
$output="Error en la consulta: ".$conn->error;
}
$conn->close();
}
echo $output;
}
?>
现在,我想要实现的是:我想在每次点击导航栏中的某个项目时调用这个 php 函数,然后将点击的项目的 ID 发送到 php 函数并根据该 ID 进行查询,如下所示:
HTML
<tr class="item">
<td class="view" id="WW Support Tier 1" onclick="fill(this.id)"><a href="escalationreview.php"><div>Escalation Review</div></a></td>
</tr>
PHP
<?php
function fill($id){
if ( !$conn = new mysqli("localhost", "root", "", "zendesk_data") ){
$output="Connection failed: " . $conn->connect_error;
} else {
$sql = "SELECT status, id, subject, requester, requested, requested_updated, service, next_sla_breach FROM $id";
if ( $result = $conn->query($sql) ){
if ($result->num_rows > 0) {
$output="<table class='queue_table'>
<tr align='left'>
<th>Status</th>
<th>ID</th>
<th>Subject</th>
<th>Requester</th>
<th>Requested</th>
<th>Requested Updated</th>
<th>Service</th>
<th>Next SLA Breach</th></tr>";
while($row = $result->fetch_assoc()) {
$output.= "<tr><td>". $row["status"]. "</td><td><a href='../tickets/new.php?tid=" . $row["id"] . "'>" . $row["id"]. "</a></td><td>" . $row["subject"]. "</td><td>" . $row["requester"]. "</td><td>" . $row["requested"]. "</td><td>". $row["requested_updated"]. "</td><td>".
$row["service"]. "</td><td>". $row["next_sla_breach"]. "</td></tr>";
}
$output.="</table>";
} else {
$output= "0 results";
}
} else {
$output="Error en la consulta: ".$conn->error;
}
$conn->close();
}
echo $output;
}
?>
我知道上面的代码不起作用,这只是我想象它如何工作的一个想法。
我相信这可以使用 AJAX 来实现,但我不完全确定。这里的任何指导将不胜感激!预先感谢。
解决方案
如果您的 PHP 文件呈现整个 html,您可能根本不需要 AJAX,只需使用 id 作为查询参数填充每个导航项的链接:
<td class="view" id="WW Support Tier 1">
<a href="escalationreview.php?id="<?= urlencode("WW Support Tier 1") ?>"><div>Escalation Review</div></a></td>
</tr>
然后在您的 PHP 文件中,只需获取 ID 作为参数
<?php
$id = $_REQUEST["id"] ?? null;
if($id) {
fill($id);
}
function fill($id) {
if ( !$conn = new mysqli("localhost", "root", "", "zendesk_data") ){
$output="Connection failed: " . $conn->connect_error;
} else {
$sql = "SELECT status, id, subject, requester, requested, requested_updated, service, next_sla_breach FROM $id";
if ( $result = $conn->query($sql) ){
if ($result->num_rows > 0) {
$output="<table class='queue_table'>
<tr align='left'>
<th>Status</th>
<th>ID</th>
<th>Subject</th>
<th>Requester</th>
<th>Requested</th>
<th>Requested Updated</th>
<th>Service</th>
<th>Next SLA Breach</th></tr>";
while($row = $result->fetch_assoc()) {
$output.= "<tr><td>". $row["status"]. "</td><td><a href='../tickets/new.php?tid=" . $row["id"] . "'>" . $row["id"]. "</a></td><td>" . $row["subject"]. "</td><td>" . $row["requester"]. "</td><td>" . $row["requested"]. "</td><td>". $row["requested_updated"]. "</td><td>".
$row["service"]. "</td><td>". $row["next_sla_breach"]. "</td></tr>";
}
$output.="</table>";
} else {
$output= "0 results";
}
} else {
$output="Error en la consulta: ".$conn->error;
}
$conn->close();
}
echo $output;
}
?>
推荐阅读
- java - 登录后 JSessionId 发生变化,Angular 5 正在发送带有请求的新 JSessionID 并丢弃旧的 jsessionid
- javascript - 如何从另一个函数返回“addEventListener()” - Javascript
- angular - 使用指令设置元素焦点
- python - fit_transform 运行算法前的数据
- docker - docker-compose up 不绑定端口
- r - 折叠一列中因子变量的水平,同时对另一列中的计数求和
- java - 目录中的 Gradle fatjar
- python - OpenGL中的太阳系,相机位置
- javascript - 来自 Google GeoCode API 的值不会正确返回
- android - 将匿名用户与谷歌登录关联后,同一帐户仍有两个 Firebase 用户