meylo Posted December 10, 2014 Share Posted December 10, 2014 (edited) Can anyone help by shedding some light with a little problem I encounter using JSON endpoint requests. I'm not very good with PHP. When the endpoint is called and finds no data, I want to replace it with a local image source. In this case, I am calling brewery->logo_url. Most entries have endpoints, but some don't. When an image link is found at the endpoint, it throws off my layout. You can see the ones that look off. They align to the left. I want to replace with a logo default image each time this happens, with hopes to set the layout adjust back to normal. Here's my working document http://graintoglass.com/fb/tap-menu-test.php and here's the JSON. http://mcallen.taphunter.com/widgets/locationWidget?location=Grain-To-Glass&format=jsonv2short Code Effort <?php $url = 'http://mcallen.taphunter.com/widgets/locationWidget?location=Grain-To-Glass&format=jsonv2short&servingtype=bottlecan'; $curl_session = curl_init($url); curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl_session, CURLOPT_CONNECTTIMEOUT, 4); curl_setopt($curl_session, CURLOPT_TIMEOUT, 10); $data = curl_exec($curl_session); curl_close($curl_session); $beer_list = json_decode($data); echo '<p class="last-updated">Last Updated: ' . date("m/d/Y") . '</p>'; echo '<h1 class="page-title">Bottle List</h1>'; echo '<p class="page-intro">Not only do we serve a hefty list of brews on tap, we also offer an exclusive list of beers in bottle. Our bottle list is constantly changing so the occasional return visits to browse this list is the best way to stay up to date. Take a look!</p>'; foreach ($beer_list as $beer) { echo '<article class="beer-entry"> <div class="beer-image"><img src="'.$beer->brewery->logo_url.'" /></div> <div class="beer-info"> <p class="brewery-name">'.$beer->brewery->name.' — '.$beer->brewery->origin.'</p> <h2 class="beer-name">'.$beer->beer->name.'</h2> <p class="beer-style">Beer Style: '.$beer->beer->style.'</p> <p class="beer-description">'.$beer->descriptions->short_description.'</p> <p class="beer-abv">ABV: '.$beer->beer->abv.' / IBU: '.$beer->beer->ibu.'</p> <p class="beer-ibu"></p> </div> <div class="clearfix"></div> </article> '; } echo '<p class="page-bottom">If you would like to see more of our food and beer options, <a href="https://www.graintoglass.com" target="_blank">visit our website</a>.</p>'; ?> Edited December 10, 2014 by meylo Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted December 10, 2014 Solution Share Posted December 10, 2014 You ned to check to see if $beer->brewery->logo_url is empty then display your default image. If its not then display the supplied brewery logo. Example code $logo_image = empty($beer->brewery->logo_url) ? 'default-brewer-logo.png' // use this default image if brewery->logo_url is empty : $beer->brewery->logo_url; // use the brewery logo from json if it is not empty ... echo '<article class="beer-entry"> <div class="beer-image"><img src="'.$logo_image.'" /></div> Quote Link to comment Share on other sites More sharing options...
meylo Posted December 11, 2014 Author Share Posted December 11, 2014 (edited) Thank you for the help. Here's what I am getting with the added code you provided. Test Page: http://graintoglass.com/fb/tap-menu-test.php It seems that I get the default logo all the way through. I just need the default logo to show when the image endpoint is empty. Most recent code effort <?php $url = 'http://mcallen.taphunter.com/widgets/locationWidget?location=Grain-To-Glass&format=jsonv2short'; $curl_session = curl_init($url); curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl_session, CURLOPT_CONNECTTIMEOUT, 4); curl_setopt($curl_session, CURLOPT_TIMEOUT, 10); $data = curl_exec($curl_session); curl_close($curl_session); $beer_list = json_decode($data); $logo_image = empty($beer->brewery->logo_url) ? 'GrainToGlassExclusive.gif' // use this default image if brewery->logo_url is empty : $beer->brewery->logo_url; // use the brewery logo from json if it is not empty echo '<p class="last-updated">Last Updated: ' . date("m/d/Y") . '</p>'; echo '<h1 class="page-title">Tap Menu</h1>'; echo '<p class="page-intro">You have stumbled upon the treasure chest of delicious drinking. From dark porters and stouts to light pilsners and hefeweizens, we have a variety of beer styles to match your beer palette. You might want to refresh your browser often, because we add new kegs daily. It is safe to say our tap list is always up to date.</p>'; foreach ($beer_list as $beer) { echo '<article class="beer-entry"> <div class="beer-image"><img src="'.$logo_image.'" /> </div> <div class="beer-info"> <p class="brewery-name">'.$beer->brewery->name.' — '.$beer->brewery->origin.'</p> <h2 class="beer-name">'.$beer->beer->name.'</h2> <p class="beer-style">Beer Style: '.$beer->beer->style.'</p> <p class="beer-description">'.$beer->descriptions->short_description.'</p> <p class="beer-abv">ABV: '.$beer->beer->abv.' / IBU: '.$beer->beer->ibu.'</p> <p class="beer-ibu"></p> </div> <div class="clearfix"></div> </article> '; } echo '<p class="page-bottom">If you would like to see more of our food and beer options, <a href="https://www.graintoglass.com" target="_blank">visit our website</a>.</p>'; ?> Edited December 11, 2014 by meylo Quote Link to comment Share on other sites More sharing options...
boompa Posted December 11, 2014 Share Posted December 11, 2014 I don't know how you're not getting an error, considering there is no $beer variable where you copied and pasted the provided code. *Think* about what you're doing here. You want to check the logo url *for each* beer in the list, right? Oh, and you probably want to fix your gif so that EXCLUSIVE is spelled properly. Quote Link to comment Share on other sites More sharing options...
meylo Posted December 11, 2014 Author Share Posted December 11, 2014 I'm trying to keep up with PHP as I am learning. I seems that Exclusive is spelled correctly and I so have $beer variable. So I'm a little confused. Quote Link to comment Share on other sites More sharing options...
meylo Posted December 11, 2014 Author Share Posted December 11, 2014 Ohhhhh "for each"! Got it. It works now. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.