-1

I'm trying to use the <?php echo command in welcome.blade.php but do not know the right syntax

Tried all the workarounds but nothing

<div class="col">
@php
$decoded_json =json_decode(file_get_contents("https://api.coinmarketcap.com/v1/ticker/"),     TRUE);
function price($curr) {
    global $decoded_json;
    $js = array_column($decoded_json, 'price_usd', 'id');
    return $js[$curr];
}
@endphp

<body>
BTC $<?php echo price("bitcoin");?>
<br />
 LTC $<?php echo price("litecoin"); ?>.
<br />
XMR $<?php echo price("monero"); ?>.
</body>

The code in between @php & @endphp works fine but when i add <?php echo string throw a 500 error

Edit: I now have

<center><h2>BTC $<?php= price("bitcoin");?></h2></center>
<center><h2>LTC $<?php= price("litecoin");?></h2></center>
<center><h2>XMR $<?php= price("monero");?></h2></center>

Page loads, but no values load. What am I doing wrong?

waterloomatt
  • 3,662
  • 1
  • 19
  • 25
  • 2
    Hope you’re aware that half of this doesn’t belong into the template in the first place. – misorude Jul 30 '19 at 10:02
  • I spent 3 hours trying to figure out how to use a route/controller with no luck Tried to follow this and few other things on github and got lost https://ourcodeworld.com/articles/read/588/how-to-retrieve-information-and-the-value-of-any-cryptocurrency-bitcoin-ethereum-in-laravel This is as far as i come and atleast got this working just need to get the echo commands to work – CliveJones Jul 30 '19 at 10:07
  • 1
  • When i remove php out of – CliveJones Jul 30 '19 at 10:18
  • 1
    Only vaguely mentioning that you “get errors”, without telling us what those actually are, is quite pointless. – misorude Jul 30 '19 at 10:19
  • "array_column() expects parameter 1 to be array, null given (View: /var/www/website/resources/views/welcome.blade.php)" -------------------------------------------

    BTC $= price("bitcoin");?> | LTC $= price("litecoin");?> | XMR $= price("monero");?>

    – CliveJones Jul 30 '19 at 10:23
  • 1
    That is an error coming from your `price` function - apparently `$decoded_json` is not what you expected it to be, but simply NULL. You can check `json_last_error`/`json_last_error_msg` to find out what went wrong when attempting to decode the JSON. (Most likely perhaps though that trying to fetch the data using file_get_contents returned either an error message instead of the expected JSON, or just an empty string because something failed on the network level.) – misorude Jul 30 '19 at 10:30

1 Answers1

0

You really are fighting against the framework by doing everything in the view. Might as well just use plain-old-php if you're going to do it that way.

Nevertheless, if you really don't want to use a controller, you can keep the logic somewhat separated by performing the API lookup in your routes file web.php and then pass the data to the view.

Route::get('/', function () {

    $response = json_decode(file_get_contents("https://api.coinmarketcap.com/v1/ticker/"),true);
    $currencyValues = array_column($response, 'price_usd', 'id');

    // Now call your view and pass in the data so the view has access to it. 
    // Notice that the view will have access to a variable named `$currencies` and not `$currencyValues` 
    return view('welcome', [
        'currencies' => $currencyValues
    ]);
});

And then, in your your view, welcome.blade.php, you can output the currencies that were retrieved.

Edit - Forgot we were working in Blade. Added {{ ... }}.

{{ $currencies['bitcoin'] }}

<br />

{{ $currencies['litecoin'] }}
waterloomatt
  • 3,662
  • 1
  • 19
  • 25
  • So just want to be sure im doing this right the first part i should put at the end of my web.php in routes folder and the second part i put in welcome.blade.php where i want it shown? – CliveJones Jul 30 '19 at 12:58
  • Undefined variable: currencies (View: /var/www/website/resources/views/welcome.blade.php) – CliveJones Jul 30 '19 at 13:08
  • ErrorException (E_ERROR) – CliveJones Jul 30 '19 at 13:09
  • Right. But you need to replace your `Route::get('/', function () { ... }` with the one I wrote. Don't just paste it at the end of your file. You should only have 1 definition for `Route::get('/', ...)`. These routes are fundamental to Laravel (and most web frameworks) so I suggest reading about them. – waterloomatt Jul 30 '19 at 13:55
  • Route::name('auth.')->group(function () { include 'auth.php'; }); Route::prefix('admin') -> group(function (){ Route::middleware(['admin_panel_access'])->group(function () { include 'admin.php'; }); }); // Profile routes Route::middleware(['auth'])->group(function () { Route::get('banned', 'ProfileController@banned')->name('profile.banned'); Route::middleware(['is_banned']) -> group(function(){ include 'profile.php'; }); }) Route::get('/', 'IndexController@home')->name('home'); – CliveJones Jul 30 '19 at 15:20
  • Seems i have a Route::get('/', 'IndexController@home')->name('home'); in my web.php that works with my script is there a work around. Or could you tell me how to set up a controller and connect it? – CliveJones Jul 30 '19 at 15:21