mysql SQL查询html表格格式

iyfjxgzm  于 2023-01-12  发布在  Mysql
关注(0)|答案(1)|浏览(156)

左列表如何回显到“日”、“月”、“年”,因为右侧SQL查询结果为3项,所以现在是重复显示。

$ID = get_current_user_id();
global $wpdb;
if(isset($_GET["view_data"])){
   $SubmissionID = $_GET["view_data"];
   $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}myTable WHERE user_id = {$ID} AND `key` IN ('name1', 'name2', 'name3')", OBJECT );
    if(isset($_GET["form_name"])) echo "<h4>YOUR REVIEW ON {$_GET["form_name"]}</h4>";

    echo "<table>";
    foreach ($results as $result) {

    echo "<tr>";
    echo "<td>";
    echo    "<p>test1</p>";
    echo "</td>";
    echo "<td>";
    echo    $result->value;
    echo "</td>";
    echo "</tr>";   
    
    }
    echo "</table>";
    echo "<h4><a href='?view_all'>Back to List</a></h4>";
}

我的代码的当前输出:
| 测试1|动态数据1|
| - ------|- ------|
| 测试1|动态数据2|
| 测试1|动态数据3|
我需要的输出:
| 每日|动态数据1|
| - ------|- ------|
| 每月|动态数据2|
| 每年|动态数据3|

woobm2wo

woobm2wo1#

添加计数器:

$ID = get_current_user_id();
global $wpdb;
if(isset($_GET["view_data"])){
   $SubmissionID = $_GET["view_data"];
   $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}myTable WHERE user_id = {$ID} AND `key` IN ('name1', 'name2', 'name3')", OBJECT );
    if(isset($_GET["form_name"])) echo "<h4>YOUR REVIEW ON {$_GET["form_name"]}</h4>";
    $count = 0;

    echo "<table>";
    foreach ($results as $result) {
    $count++;
    $title = 'DEFAULT Title';
        if( $count == 1 ){
            $title = 'Daily';
        }
        elseif( $count == 2 ){
            $title = 'Monthly';
        }
        elseif( $count == 3 ){
            $title = 'Yearly';
        }
    echo "<tr>";
    echo "<td>";
    echo    "<p>" . $title ."</p>";
    echo "</td>";
    echo "<td>";
    echo    $result->value;
    echo "</td>";
    echo "</tr>";   
    
    }
    echo "</table>";
    echo "<h4><a href='?view_all'>Back to List</a></h4>";
}

相关问题