0

I realize there is likely something very easy I'm missing here. Also, I'm very new to php so please keep that in mind when responding :)

I am trying to display the results from an API call in an html table. Here is my code:

// construct the query with our apikey and the query we want to make
$endpoint = 'http://www.coinmine.pw/api.php?method=coinprofit';
// setup curl to make a call to the endpoint
$session = curl_init($endpoint);
// indicates that we want the response back
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// exec curl and get the data back
$data = curl_exec($session);
// remember to close the curl session once we are finished retrieveing the data
curl_close($session);
// decode the json data to make it easier to parse the php
$search_results = json_decode($data, true);
if ($search_results === NULL) die('Error parsing json');
//print_r($search_results);

echo '<table>';
foreach ($search_results as $coin) {

$name = $coin["name"];
$profit = $coin["profit"];

echo '<tr><td>' . $name . '</tr></td>';
echo '<tr><td>' . $profit . '</tr></td>';

}
echo '</table>';

All the above outputs is name profit name profit name profit but with no separate table rows.

Ideally I'd like the whole api call to be in a table, but I'm just trying to get this to display properly first so I can figure out making the whole table.

Bonus points if someone can point me in the right direction to make the table sortable by each column.

2
  • 1
    echo '<tr><td>' . $name . '</tr></td>'; & echo '<tr><td>' . $profit . '</tr></td>'; should be echo '<tr><td>' . $name . '</td></tr>'; & echo '<tr><td>' . $profit . '</td></tr>'; Commented Mar 21, 2014 at 16:21
  • 1
    For bonus points: DataTables is a neat tool to make sortable tables by column. It uses some javascript, but very easy to setup. Commented Mar 21, 2014 at 16:22

1 Answer 1

1

You're closing your <tr> before closing the child <td>. Should be:

echo '<tr><td>' . $name . '</td></tr>';
echo '<tr><td>' . $profit . '</td></tr>';

But it looks like you want each $coin to be its own row, in which case it should be:

echo '<tr><td>'.$name.'</td><td>'.$profit.'</td></tr>';
Sign up to request clarification or add additional context in comments.

10 Comments

I can't believe I did that lol. New to php, not to html :) It still didn't fix the issue though. It is displaying the same way.
Do you want a row for each $coin? You shouldn't wrap both $name and $profit in their own <tr> then, you should have two <td>s wrapped in one <tr>. Updated my answer.
That is indeed what I'm trying to do. However, since I couldn't get it to display right I thought I would just break it down as much as possible and see if i could get ANYTHING to show up in different rows. Here's exactly what the output is looking like with your code above altcoinfo.com/mining-info
It seems like maybe my code is right but there's something in the data that isn't allowing me to do what I want?
I don't even see a table in there - do you have something that strips out HTML tags in your output?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.