Home > database >  Only Outputs 1 Row from SQL Database (Using Foreach Loop in PHP)
Only Outputs 1 Row from SQL Database (Using Foreach Loop in PHP)

Time:06-23

I have 500 rows of cryto data including coin name and symbol in Wordpress database.

I'm trying to output all of rows using foreach loop and shortcode.

However, I'm only getting "BTC" row which is the first row of the table.

enter image description here

My code is this.

<?php 
add_shortcode('call_CMC_table', function() {
    global $wpdb;
    $all_coin_data = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}cmc_listing_data");

    foreach ( $all_coin_data as $print_data ) {
        $db_name = $print_data->name;
        $db_symbol = $print_data->symbol;

        $output =
            '<div >
                <figure >
                    <table>
                        <thead>
                        <tr>
                            <th>Coin Name</th>
                            <th>Coin Symbol</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td>'. $db_name .'</td>
                            <td>'. $db_symbol .'</td>
                        </tr>
                        </tbody>
                    </table>
                </figure>
            </div>';

        return $output; 
    }
}); 
?>

What did i do wrong in this code? I want to loop "< td >" tags in the table. By the way, I'm learning concept of loop these days and I'm following this thread.

CodePudding user response:

return $output; ends the function there and then, and returns the data to the caller. Because you've put that inside the loop, it will stop the function at the end of the first time the loop has run. No more records will be processed.

You need to

  1. Move that return statement outside the loop, after the end of it. This will allow the loop to process all the rows before the function execution is ended.

  2. Append to $output each time the loop runs, instead of overwriting it - by using .= (i.e. $output .=...).

  3. Move outside the loop all other content which shouldn't be repeated, such as the table header and the opening and closing tbody tags

Complete code:

<?php 
add_shortcode('call_CMC_table', function() {
    global $wpdb;
    $all_coin_data = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}cmc_listing_data");

      $output =
            '<div >
                <figure >
                    <table>
                        <thead>
                        <tr>
                            <th>Coin Name</th>
                            <th>Coin Symbol</th>
                        </tr>
                        </thead>
                        <tbody>';

    foreach ( $all_coin_data as $print_data ) {
        $db_name = $print_data->name;
        $db_symbol = $print_data->symbol;

        $output .= '
          <tr>
            <td>'. $db_name .'</td>
            <td>'. $db_symbol .'</td>
          </tr>';
    }

    $output .=  '
          </tbody>
        </table>
      </figure>
    </div>';
    
    return $output; 
}); 
?>
  • Related