Home > other >  SQL query html table format
SQL query html table format

Time:01-12

How to echo the left column table to "Daily" "Monthly" and "Yearly"? It currently displays repeatedly because the results of SQL queries on the right side are 3 items.

$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>";
}

The current output of my code:

test1 dynamic_data_1
test1 dynamic_data_2
test1 dynamic_data_3

The output I need:

Daily dynamic_data_1
Monthly dynamic_data_2
Yearly dynamic_data_3

CodePudding user response:

add a counter:

$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>";
}
  • Related