Reputation: 41
i'm trying to get this image to display from an api call in php, to an img tag in html.
So far this is what i tried,
<div class="col-md-3 col-lg-3 " align="center"><img alt="User Pic" <?php echo 'src="'. $data['response']['avatar'] . '"'; ?>
but it's only displaying user pic, is it because the api isn't inside the php? full code below.
<section class="section blog-detail">
<div class="container">
<div class="row">
<div class="col-md-5 toppad pull-right col-md-offset-3 ">
<br>
</div>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 col-xs-offset-0 col-sm-offset-0 col-md-offset-3 col-lg-offset-3 toppad" >
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Profile</h3>
</div>
<div class="panel-body">
<div class="row">
<div class="col-md-3 col-lg-3 " align="center"> <img alt="User Pic" <?php echo 'src="'. $data['response']['avatar'] . '"'; ?> class="img-circle img-responsive"> </div>
<div class=" col-md-9 col-lg-9 ">
<table class="table table-user-information">
<tbody>
<?php
/* Check if button was clicked */
if (isset($_POST['someName'])) {
/* If id is empty or not a number, then don't proceed fetching data */
if (empty($_POST['id']) || !is_numeric($_POST['id'])) {
echo 'Invalid id';
} else {
/* Fetch data */
$data = file_get_contents('https://api.truckersmp.com/v2/player/' . $id = trim($_POST["id"]));
/* Decode json */
$data = json_decode($data, true);
/* Display id and name */
if (isset($data['response'])) {
/* You should add some checks to check if id and name actually exists and are not empty */
echo '
<tr><td>ID:</td><td><strong /> ' . $data['response']['id'] . '</td></tr>
<tr><td>Name:</td><td><strong /> ' . $data['response']['name'] . '</td></tr>
<tr><td>Join Date:</td><td><strong /> ' . $data['response']['joinDate'] . '</td></tr>
<tr><td>Steam 64:</td><td><strong /> ' . $data['response']['steamID64'] . '</td></tr>
<img src="'. $data['response']['avatar'] . '"></td></tr>
<tr><td>Group:</td><td><strong />' . $data['response']['groupName'].'</td></tr>';
}
}
}
?>
</tbody>
</table>
<a href="#" class="btn btn-primary">My Sales Performance</a>
<a href="#" class="btn btn-primary">Team Sales Performance</a>
</div>
</div>
</div>
<div class="panel-footer">
<a data-original-title="Broadcast Message" data-toggle="tooltip" type="button" class="btn btn-sm btn-primary"><i class="glyphicon glyphicon-envelope"></i></a>
<span class="pull-right">
<a data-original-title="Remove this user" data-toggle="tooltip" type="button" class="btn btn-sm btn-danger"><i class="glyphicon glyphicon-remove"></i></a>
</span>
</div>
</div>
</div>
</div>
</div>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Upvotes: 0
Views: 58
Reputation: 1374
I'm going to assume you are new to PHP cause you are breaking one fundamental rule. You are mixing the PHP logic code with HTML code. If you are a beginner and you cannot avoid writing PHP logic within the same file as HTML code at least do this: Write your PHP code first, before any HTML and leave the HTML for the file's end.
So, naturally this $data['response']['avatar']
is empty above, because the $data array is being retrieved from a json_decode but "below".
So, add your PHP code above first thing, and let your HTML live below. And remember, you'll do this just because you are a beginner. Later you will learn a Framework like Laravel, Symfony, Yii, CodeIgniter, etc ... which is more like the real deal for PHP.
Upvotes: 1