簡體   English   中英

如何僅回顯jsonfile中的一篇文章

[英]How to echo only one post from a jsonfile

我已經制作了一個模塊,可以顯示instagram個人資料中的最新圖像,並且它在Joomla 3.4.8中具有一個小的彈出畫廊,非常適合我要添加個人資料圖像-profile_picture,但是當然它應該只出現一次。 我嘗試了各種php echo-但沒有運氣

有人可以幫忙嗎?

的PHP

<?php
    // Supply a user id and an access token
    $userid = $params->get('klintweb_insta_id');
    $accessToken = $params->get('klintweb_insta_access_token');
    $count = $params->get('klintweb_insta_count');

    // Gets post and image
    function fetchData($url){
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_TIMEOUT, 20);
         $result = curl_exec($ch);
         curl_close($ch); 
         return $result;
    }

    // Pulls and parses data.
    $result = fetchData("https://api.instagram.com/v1/users/{$userid}/media/recent/?count={$count}&access_token={$accessToken}");
    $result = json_decode($result);


?>

的foreach

<?php foreach ($result->data as $post): ?>
    {emailcloak=off}
    <!-- Renders images. @Options (thumbnail,low_resoulution, high_resolution) -->
    <span class="KlintWebInstaSpan">
        <a data-mediabox-group="insta" type="image/jpeg" class="jcepopup noicon" data-mediabox-caption="<?= rtrim(strip_tags(substr ($post->caption->text,0,140))).'...'; ?>" data-mediabox-title="rasmusjorgensen128" href="<?= $post->images->standard_resolution->url ?>">
            <img src="<?= $post->images->thumbnail->url ?>" alt="Billede fra instagram" />
            <span class="overlay"></span>
        </a>
    </span>
<?php endforeach ?>

json文件

{
"data": [{
    "comments": {
        "count": 0
    },
    "caption": {
        "created_time": "1296710352",
        "text": "Inside le truc #foodtruck",
        "from": {
            "username": "kevin",
            "full_name": "Kevin Systrom",
            "type": "user",
            "id": "3"
        },
        "id": "26621408"
    },
    "likes": {
        "count": 15
    },
    "link": "http://instagr.am/p/BWrVZ/",
    "user": {
        "username": "kevin",
        "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_3_75sq_1295574122.jpg",
        "id": "3"
    },
    "created_time": "1296710327",
    "images": {
        "low_resolution": {
            "url": "http://distillery.s3.amazonaws.com/media/2011/02/02/6ea7baea55774c5e81e7e3e1f6e791a7_6.jpg",
            "width": 306,
            "height": 306
        },
        "thumbnail": {
            "url": "http://distillery.s3.amazonaws.com/media/2011/02/02/6ea7baea55774c5e81e7e3e1f6e791a7_5.jpg",
            "width": 150,
            "height": 150
        },
        "standard_resolution": {
            "url": "http://distillery.s3.amazonaws.com/media/2011/02/02/6ea7baea55774c5e81e7e3e1f6e791a7_7.jpg",
            "width": 612,
            "height": 612
        }
    },
    "type": "image",
    "users_in_photo": [],
    "filter": "Earlybird",
    "tags": ["foodtruck"],
    "id": "22721881",
    "location": {
        "latitude": 37.778720183610183,
        "longitude": -122.3962783813477,
        "id": "520640",
        "street_address": "",
        "name": "Le Truc"
    }
},
{
    "videos": {
        "low_resolution": {
            "url": "http://distilleryvesper9-13.ak.instagram.com/090d06dad9cd11e2aa0912313817975d_102.mp4",
            "width": 480,
            "height": 480
        },
        "standard_resolution": {
            "url": "http://distilleryvesper9-13.ak.instagram.com/090d06dad9cd11e2aa0912313817975d_101.mp4",
            "width": 640,
            "height": 640
        },
    "comments": {
        "count": 2
    },
    "caption": null,
    "likes": {
        "count": 1
    },
    "link": "http://instagr.am/p/D/",
    "created_time": "1279340983",
    "images": {
        "low_resolution": {
            "url": "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_6.jpg",
            "width": 306,
            "height": 306
        },
        "thumbnail": {
            "url": "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_5.jpg",
            "width": 150,
            "height": 150
        },
        "standard_resolution": {
            "url": "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_7.jpg",
            "width": 612,
            "height": 612
        }
    },
    "type": "video",
    "users_in_photo": null,
    "filter": "Vesper",
    "tags": [],
    "id": "363839373298",
    "user": {
        "username": "kevin",
        "full_name": "Kevin S",
        "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_3_75sq_1295574122.jpg",
        "id": "3"
    },
    "location": null
},

]}

多虧了亞當·泰勒(Adam Taylor),您指出了可以做到的簡單

也感謝Danoweb的幫助

我是否正確理解您正在瀏覽一張照片列表,並且還想訪問個人資料圖片並將其打印一次?

您是否需要瀏覽所有照片? 如果沒有,您可以在打印照片后添加一個break ,以退出循環。

如果還需要遍歷照片,為什么不訪問JSON中的第一個元素,從那里獲取個人資料照片,然后遍歷其余照片?

編輯:關於您的解決方案,直接訪問第一張照片,打印相關的用戶縮略圖,然后繼續循環打印所有照片,可能會更容易。 就像是:

<div class="user">
  <?php $post = $result->data[0]; ?>
    <img src="<?= $post->user->profile_picture ?>" alt="<?= $post->user->username ?>" />
    <span class="username"><?= $post->user->username ?></span>
</div>

<!-- do the rest of your looping/displaying photos -->

在您的解決方案中,您將數組切成一個元素,然后在您真正想要做的就是訪問第一個元素時遍歷數組。

據我了解,它似乎在JSON響應中兩次提供了個人資料圖片。 我將使用一個變量來跟蹤我是否已經顯示個人資料圖片,如果是,則不顯示第二張圖片。 例如:

<?php
$shown_profile = false;
foreach ($result->data as $post)
{
    //WE HAVE NOT SHOWN PROFILE PIC, SHOW IT AND SET FLAG.
    if($shown_profile == false and (image is profile pic) )
    {
        $shown_profile = true;
    }

    //WE HAVE ALREADY SHOWN PROFILE PIC AND THIS IMAGE IS A DUPLICATE
    if($shown_profile == true and (image is profile pic) )
    {
        //JUMP TO THE NEXT LOOP INTERATION
        continue;
    }
    //[THE REST OF YOUR FOREACH CODE]
}

?>

您可以在此處找到有關“ continue”語句的更多信息:
http://php.net/manual/en/control-structures.continue.php

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM