0

すべてのデータベースを取得するために、単純な php/mysql リクエストを作成し、それを json にエンコードしました。

私は今これを持っています:

[
   {
      "id":"1",
      "title":"Paris - New York",
      "desc":awesome food,
      "price":"5-10",
      "adress":"63 Rue du faubourg Saint-Denis",
      "map":"https:\/\/maps.google.com\/maps?f=q&source=s_q&hl=fr&geocode=&q=80+Rue+du+Faubourg+Saint-Denis,+Paris,+France&aq=0&oq=80+rue+du+faubour&sll=37.0625,-95.677068&sspn=36.778911,79.013672&t=m&ie=UTF8&hq=&hnear=80+Rue+du+Faubourg+Saint-Denis,+75010+Paris,+%C3%8Ele-de-France,+France&ll=48.873562,2.35491&spn=0.001866,0.004823&z=14&output=embed",
      "background":"pny.jpg"
   },
   {
      "id":"2",
      "title":"Urfam Durum",
      "desc":"des sandwichs Kurdes vraiment bons",
      "price":"5-10",
      "adress":"66 rue du faubourg Saint-Denis",
      "map":"google blabla",
      "background":"ud.jpg"
   }
]

javascript/jquery ($('').html() のようなもの) で挿入することにより、差分 DIV に単一の投稿をランダムに表示したい

私はこのような結果を期待しています:

<div class="span6 offset3 place">
      <h2>**"title":"Paris - New York"**</h2>

      <ul class="infos">
        <li class="prix">**"price":"5-10"**</li>
        <li class="adresse"><a href="#map" data-toggle="modal">**"adress":"63 Rue du faubourg Saint-Denis"**</a></li>
      </ul>
</div>

(**) の間のテキストは、データを表示する場所を意味します

私はあなたがアイデアを持っていると思います!どんな助けでも本当に感謝します

PS : 英語は私の母国語ではないので、間違いをお許しください

4

5 に答える 5

0

jquery テンプレートgithub リンクを使用することをお勧めします

また、使用できます

jQuery.each(json, function(i, v) {
    $('#containerDiv').append('<div class="span6 offset3 place"><h2>'+v.title+'</h2> <ul class="infos"><li class="prix">'+v.price+'</li> <li class="adresse"><a href="#map" data-toggle="modal">'+v.adress+'</a></li></ul></div>');
});

各ループのjsonはあなたのデータです

于 2013-07-24T21:14:17.157 に答える
0

テンプレート エンジン (_template) でアンダースコア ライブラリを使用できます。したがって、オブジェクト全体を渡して、テンプレート内のプロパティを使用できます。

于 2013-07-24T21:12:15.600 に答える
-1

jquery などを使用して、データを Web ページに取得することをお勧めします。

$.ajax({
    url: "get_json_data.php",
    type: "GET",
    dataType: "json",
    success: function(data){
        // *** Do things here with the data ***
        console.log(data);
    },
    error: function(error){
         alert("Something is wrong :" + error);
    }
});

PHP が JSON データを送信していることを確認する必要があります。

header('Content-type: application/json');
echo $my_json_results;

この投稿を見て、返された結果でテンプレートを使用する方法を確認してください: How do I use jquery templates with JSON data?

于 2013-07-24T21:26:37.233 に答える
-1
  use JSON.parse() for parsing your json encoded data.


   $.ajax({ 
    url:"something.php",
    type:"POST",
    data:{data_You_send},
    success:function(data){

//this is your response that you retrieve

    var obj=JSON.parse(data)  //since the data that we have retrieved is in json_decoded format,we need to parse it

for(var i in obj){//to see all your data,try this for all fields
    console.log(obj[i].id+","+obj[i].title);//just for 2 fields for understanding purpose

$("#id").html(obj[0].id) or $("#id").text(obj[0].id)//assign innerHTML or content to respective elements by these jquery methods.
    }

ご不明な点がございましたら、お気軽にお問い合わせください!!!

于 2013-07-24T21:05:09.523 に答える