Return html table in php function

✔ Recommended Answer

After investigating I found the way out. But honestly I can't understand why this code works. Thank you all for putting me on the right path.

Code:

function foobar_func($json_output){    $jsonurl = "http://xxxx/club/api/xxx/category/";    $json = file_get_contents($jsonurl,0,null,null);    $json_output = json_decode($json);    echo "<table>";    foreach ( $json_output->result as $result )    {        echo "<tr><td>".$result->id."</td><td>".$result->categoryKind."</td><td>".$result->ranking."</td>";    }    echo "</table>" ;    return $html;}add_shortcode( 'foobar', 'foobar_func' );

Source: stackoverflow.com

Answered By: sundsx

Method #2

Sure, here's an example PHP function that returns an HTML table:

php
function generate_table($data) { $html = '<table>'; $html .= '<tr><th>Name</th><th>Email</th><th>Phone</th></tr>'; foreach ($data as $row) { $html .= '<tr>'; $html .= '<td>' . $row['name'] . '</td>'; $html .= '<td>' . $row['email'] . '</td>'; $html .= '<td>' . $row['phone'] . '</td>'; $html .= '</tr>'; } $html .= '</table>'; return $html; }

This function takes an array of data as input, where each element of the array represents a row in the table with keys "name", "email", and "phone". The function generates an HTML table with headers and data rows, and returns the HTML code as a string.

You can use this function like this:

php
$data = array( array('name' => 'John', 'email' => 'john@example.com', 'phone' => '555-1234'), array('name' => 'Mary', 'email' => 'mary@example.com', 'phone' => '555-5678'), array('name' => 'Bob', 'email' => 'bob@example.com', 'phone' => '555-9012'), ); echo generate_table($data);

This will output an HTML table with the data in the $data array.

Comments

Most Popular

PhpStorm, return value is expected to be 'A', 'object' returned

Remove Unicode Zero Width Space PHP

Laravel file upload returns forbidden 403, file permission is 700 not 755