从SQL动态生成的表中获取单个值,并在Wordpress中的另一个表中使用它


Getting a single value from a table dynamically generated by SQL and using it in another table in Wordpress

我目前有一个由sql查询生成的表。该页面的url是http://skunkboxstudios.com/dev/ofa-courses/

在表中,每行上都有一个按钮,显示一个模态窗口,显示该行的更多细节。我如何告诉模态窗口只选择有关行信息。

下面是页面上表格的代码:

global $wpdb;
$courses = $wpdb->get_results("SELECT * FROM section_view;");
$results = array();
echo "<table>";
echo "<tr>";
echo "<th>Section Name</th>";
echo "<th>Section Description</th>";
echo "<th>Start Date</th>";
echo "<th>End Date</th>";
echo "<th>Location</th>";
echo "<th>Details</th>";
echo "</tr>";
foreach($courses as $course){
    $sectionname = "$course->section_name";
    $sectiondescription = "$course->section_description";
    $sectionstartdate = "$course->term_startdate";
    $sectionenddate = "$course->term_enddate";
    $sectionlocation= "$course->location_name";
    $sectionid = "$course->section_id";
    echo "<tr>";
    echo "<td>$sectionname</td>";
    echo "<td>$sectiondescription</td>";
    echo "<td>$sectionstartdate</td>";
    echo "<td>$sectionenddate</td>";
    echo "<td>$sectionlocation</td>";
    echo "<td>$sectionid</td>";
    echo "<td>";
    echo "<button class='element' onclick='javascript:openDialog();'>Details</button>";
    echo "</td>";
    echo "</tr>";
}
echo "</table>";

模态窗口的代码在这里:

$details = $wpdb->get_results(
"SELECT * FROM section_view WHERE section_id = '$sectionid';");
foreach($details as $detail){
    echo "<h2>".$detail->section_name."</h2>";
    echo "<table>";
    echo "<tr>";
    echo "<td>".$detail->section_name."</td>";
    echo "<td>".$detail->section_description."</td>";
    echo "</tr>";
    echo "</table>";
}
echo "<button class='element' onclick='javascript:closeDialog();'>Close</button>";

所以我需要在模态窗口查询SELECT * FROM section_view WHERE section_id = ' section_id对应于表的行'

如果有人能帮我,请帮忙。如果你需要的话,我也会贴出更多的信息。谢谢。

不是我经常使用的方法,但我认为您需要在您的openDialog()调用的URL中包含section_id。例如

echo "<button class='element' onclick='javascript:openDialog('"myurl.php?sectionid=${sectionid}'");'>Details</button>";

可以包括:

$sectionid=$_GET["sectionid"];

在你的模态窗口脚本的顶部。

帮忙吗?

有一些快速修复以及正确过程的替代方法(但显然是冗长的)1. 在functions.php中创建一个函数,并将代码添加到模态窗口中。

  1. 点击"details"链接使用AJAX调用此函数(使用admin-ajax.php)

  2. 从方法中返回要作为字符串显示的HTML,并在模式窗口中显示。

如果需要进一步的细节或帮助,请告诉我。