JQuery填充选择下拉列表

时间:2013-04-26 14:13:01

标签: php javascript jquery facebook facebook-opengraph

我使用Facebook Open Graph通过以下方式检索有关我管理的网页的信息:

$user_profile_accounts = $facebook->api('/me/accounts','GET');
foreach ($user_profile_accounts['data'] as $account) {
...
}

之后,我想使用jquery.dd使用收到的信息填充选择下拉列表。如果我在foreach中使用它通常会创建一个这样的选择:

<select name="accmenu" id="accmenu">
    <option data-description="<?php echo $account['category'] ?>" data-image="https://graph.facebook.com/<?php echo $account['id']; ?>/picture" value="<?php echo $account['id'] ?>"><?php echo $account['name'] ?></options>
</select>
每个帐户的

因为在循环中。这不是我想要的,而是所有帐户的下拉列表是期望的结果。如果我把选择放在foreach之外我如何迭代我收到的数据?数据:

{
  "data": [
    {
      "category": "Artist",
      "name": "Alexandra Dinu Official",
      "access_token": "BAAIGQYZBqjIABAPu8IEsuBqFVegB1ZCto61tvMIPVzUZC4JITosIHEr6dSHvYLsehcDYsCuBqlw13KepOsq1Eq1EDVqc0DFPm5sktlkjfZC8qoZAvQKyKxBdnVc2ihmBiu7nPYZC1oYznmcgm21iPCMrFTbO2OX6Vu5nSIZBvsaxTYWiyWISVixU0mTBmmi76DC8JV5FQRbZBzY7x2FRFsgQ5pZCrPStC06ZB3WhoSZBHZAZAMzs88norJYfH",
      "id": "116376655202954",
      "perms": [
        "ADMINISTER",
        "EDIT_PROFILE",
        "CREATE_CONTENT",
        "MODERATE_CONTENT",
        "CREATE_ADS",
        "BASIC_ADMIN"
      ]
    },
    {
      "category": "Fictional character",
      "name": "Potato Pete",
      "access_token": "BAAIGQYZBqjIABAKk4mM2jkVMk2miMjHkUWjZC75UDNnEPUZBNWz4ZBN3FqXbmKZAGSTyocAUzmehdIgUVSuRbr6rOg9Bq28rfsITrNT1kM7fcYFBfMA1yC9bG7c3JvaIZCgkQeXwVHx5pRJw1NFnCTOcFEmpAVlFOsAE2fy28k7zHpAky3wDQjsWD6KLEkqi0C44ZAhR3QQILCaFM9BwVn7Vpau2WUnbRHrZAjf4RofGUYEb9frEX3v1",
      "id": "497232410336701",
      "perms": [
        "ADMINISTER",
        "EDIT_PROFILE",
        "CREATE_CONTENT",
        "MODERATE_CONTENT",
        "CREATE_ADS",
        "BASIC_ADMIN"
      ]
    },
    {
      "category": "Event planning/event services",
      "name": "Mariage Fest",
      "access_token": "AAAIGQYZBqjIABAKqeomUfYZAkZAwINIZCiEBBUqw1DB6WM04090f5PrWNqzjX6SvF9Ld4l70ghwZAJYqAb2S587uQCm0cK1ZAaZABH1IDqUTlbOUZBD56EPxEnvYyEH1NM0ZD",
      "id": "311822035526927",
      "perms": [
        "ADMINISTER",
        "EDIT_PROFILE",
        "CREATE_CONTENT",
        "MODERATE_CONTENT",
        "CREATE_ADS",
        "BASIC_ADMIN"
      ]
    },
    {
      "category": "Restaurant/cafe",
      "name": "Pub Restaurant Wales Pitești",
      "access_token": "BAAIGQYZBqjIABAEYYZA4bvhnFnGEy2pfwUjDwuZA3ZBCDur4ItNJhcxh4uFeAatjO3zf4q6FioK1QyXWHTQTc4vQ5tPltCgrhXhN75TeGCY6ntONDwE0VHmBrTFXMZAsNmKZCx6QAfHWbsWADsNzOhtY7lhDznC9uH0dsfIzh2nQyZCQXZAAcoKGpnlArJjtud5iUxDqC3rlZA3N1xvXaeiwOH6CZCM8IySEZChZCRkP4C9NLeHzcuB4qlJV",
      "id": "252842318171796",
      "perms": [
        "ADMINISTER",
        "EDIT_PROFILE",
        "CREATE_CONTENT",
        "MODERATE_CONTENT",
        "CREATE_ADS",
        "BASIC_ADMIN"
      ]
    }
  ]
}

这是PHP或JavaScript的问题?我非常感谢任何指导。先感谢您。

1 个答案:

答案 0 :(得分:1)

应该是;

$user_profile_accounts = json_decode($facebook->api('/me/accounts','GET'),true);

而不是;

$user_profile_accounts = $facebook->api('/me/accounts','GET');

因此...

<select name="accmenu" id="accmenu">
    <?php
    $user_profile_accounts = json_decode($facebook->api('/me/accounts','GET'),true);
    foreach ($user_profile_accounts['data'] as $account) {?>
        <option data-description="<?php echo $account['category'] ?>" data-image="https://graph.facebook.com/<?php echo $account['id']; ?>/picture" value="<?php echo $account['id'] ?>"><?php echo $account['name'] ?></options>
    <?php }?>
</select>