格式化/了解Json数据

时间:2016-09-17 20:03:36

标签: php json html5 css3 dreamweaver

我有这个项目,我必须通过他们可用的API从网站上提取数据,我对整个事情是新的,到目前为止我能够完成的几乎是完美的但我很难理解数据允许我要设计它或只拉动必要的部件。 以下是我使用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>';
    }
    }
    ?>

所以我想做三件事,如下所示,任何帮助将不胜感激:) 1-获取格式图像中显示的价格,仅显示每克或每八分之一,然后输入&#34; $&#34;签到号码(价格)。

2-对于某些类别,它获取了错误的数据,我只希望价格如下图所示&#34;拉错了数据&#34; pulling wrong data

3-添加下拉选择器以选择要显示的类别。并不重要,但会很棒,

它正在拉动价格

的单位字段

P.S我在Dreamweaver上使用Bootstrap v3.3.4

干杯!

1 个答案:

答案 0 :(得分:0)

我认为以下代码适用于1号。 至于2,我无法理解你在这里需要什么.. 对于3,我建议你使用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>';
    }
    }
    ?>