首页 > 解决方案 > 如何根据 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 来实现,但我不完全确定。这里的任何指导将不胜感激!预先感谢。

标签: phphtmlajax

解决方案


如果您的 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;
    }
?>

推荐阅读