Aymen Yaseen Aymen Yaseen - 2 months ago 14
JSON Question

Formatting/ Understanding Json Data

I have this project where I have to pull data from a website through their available API, Im new to the whole thing, what I was able to accomplish so far is almost perfect but I am having hard time understanding the data to allow me to style it or pull only necessary parts.
Here is what I came up with using some snippets from GitHub
formatting


//current URL of the Page. cart_update.php redirects back to this URL

$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);



$headers = array(

'Content-Type: application/json',

);

$url = 'https://weedmaps.com/api/web/v1/listings/green-valley-medicinal/menu?show_unpublished=false&type=dispensary';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
$menu_data = json_decode($result, true);
foreach($menu_data['categories'] as $menu_item){
echo '<h2>'.$menu_item['title'].'</h2>';
foreach ($menu_item['items'] as $item) {
echo '<article class="col-md-12">';
echo '<div class="col-sm-3 col-lg-7" id="gvm-items">';
echo '<div class="col-md-3 col-lg-4">';
echo isset($item['image_url']) ? '<img id="gvm-ftrd-img" src="'.$item['image_url'].'">' : '<img src="images/Marijuana - Icon.png" id="gvm-ftrd-img">';
echo '</div>';
echo '<div class="col-md-7">';
echo '</div>';
echo '<div>';
echo '<h4>'.$item['name'].'</h4>';
echo '</div>';
echo '<div>'.$item['body'].'</div>';
echo '</div>';
echo '<div class="col-sm-3 col-lg-4 col-lg-offset-1" id="gvm-prices">';
foreach($item['prices'] as $key => $cost){
echo '<div class="menu-item-price">'
.$key." : ".$cost.'&emsp;<i class=" data-toggle="tooltip" data-placement="right"></i>
</div>';
}
echo '</div>';
echo '</article>';
echo '<br>';
}
}
?>


So I am trying to do three things as follows and any help would be greatly appreciated :)
1- Get the prices as shown in the formatting image to show only per gram or per eighth, and put a "$" sign in front of the number (price).

2- For some categories, its getting wrong data, i want the prices only as shown in the following image "pulling wrong data"
pulling wrong data

3- Add a drop-down selector to choose which category to make visible. is not important but would be great,

It is pulling the unit field insted of the prices

P.S I am using Bootstrap v3.3.4 on Dreamweaver

Cheers!

Answer

I think the following code would work for number 1. As for 2, I couldn't understand what you need here.. As for 3, I suggest you to use DataTables

$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);



$headers = array(

'Content-Type: application/json',

);

$url = 'https://weedmaps.com/api/web/v1/listings/green-valley-medicinal/menu?show_unpublished=false&type=dispensary';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch); 
curl_close($ch);
$menu_data = json_decode($result, true);
foreach($menu_data['categories'] as $menu_item){
echo '<h2>'.$menu_item['title'].'</h2>';
foreach ($menu_item['items'] as $item) {
   echo '<article class="col-md-12">';
      echo '<div class="col-sm-3 col-lg-7" id="gvm-items">';
      echo '<div class="col-md-3 col-lg-4">';
echo isset($item['image_url']) ? '<img id="gvm-ftrd-img" src="'.$item['image_url'].'">' : '<img src="images/Marijuana - Icon.png" id="gvm-ftrd-img">';
echo '</div>';
      echo '<div class="col-md-7">';
      echo '</div>';
      echo '<div>';
      echo '<h4>'.$item['name'].'</h4>';
      echo '</div>';
      echo '<div>'.$item['body'].'</div>';
      echo '</div>';
      echo '<div class="col-sm-3 col-lg-4 col-lg-offset-1" id="gvm-prices">';
     foreach($item['prices'] as $key => $cost){
if($key == "gram" || $key == "eighth"){
                                    echo '<div class="menu-item-price">'
                                    .$key." : $".number_format($cost,2).'&emsp;<i class=" data-toggle="tooltip" data-placement="right"></i>
                                    </div>';
}
                                }
      echo '</div>';
      echo '</article>';
      echo '<br>';
    }
    }
    ?>
Comments