json在循环时从sql数据库输入数据

时间:2013-08-02 21:43:09

标签: php sql database json while-loop

我有一个json代码,但我不知道如何输入while循环。

我有代码编辑了内容,但我不知道如何循环json,因为我被分心,下一行它意味着下一张图片右边试图添加一个它的工作但不知道如何在它上面插入这里是示例的链接。

link

> agile_carousel_data.php
>     <?php
>     include_once('includes/dbConnect.php');
>     
>     
>     ?> <?php
>     
>     $query = mysql_query("select * from persons order by rand()");
>     
>     while ($test = mysql_fetch_array($query)) {
>         
>     }
>     
>     
>     
>     ?> [ {
>                "content": "<div class='slide_inner'><a class='photo_link' href='#'><img class='photo' src='banner_bike.jpg'
> alt='Bike'></a><a
>          class='caption' href='#'>Sample Carousel Pic Goes Here And The Best
>          Part is that...</a></div>",
>                "content_button": "<div class='thumb'><img src='f2_thumb.jpg' alt='bike is nice'></div><p>Agile Carousel Place
> Holder</p>" }, {
>                "content": "<div class='slide_inner'><a class='photo_link' href='#'><img class='photo' src='banner_paint.jpg'
> alt='Paint'></a><a
>          class='caption' href='#'>Sample Carousel Pic Goes Here And The Best
>          Part is that...</a></div>",
>                "content_button": "<div class='thumb'><img src='f2_thumb.jpg' alt='bike is nice'></div><p>Agile Carousel Place
> Holder</p>" }, {
>                "content": "<div class='slide_inner'><a class='photo_link' href='#'><img class='photo' src='banner_bike.jpg'
> alt='Bike'></a><a
>          class='caption' href='#'>Sample Carousel Pic Goes Here And The Best
>          Part is that...</a></div>",
>                "content_button": "<div class='thumb'><img src='f2_thumb.jpg' alt='bike is nice'></div><p>Agile Carousel Place
> Holder</p>" }, {
>                "content": "<div class='slide_inner'><a class='photo_link' href='#'><img class='photo' src='banner_paint.jpg'
> alt='Paint'></a><a
>          class='caption' href='#'>Sample Carousel Pic Goes Here And The Best
>          Part is that...</a></div>",
>                "content_button": "<div class='thumb'><img src='f2_thumb.jpg' alt='bike is nice'></div><p>Agile Carousel Place
> Holder</p>" }, {
>                "content": "<div class='slide_inner'><a class='photo_link' href='#'><img class='photo' src='banner_bike.jpg'
> alt='Bike'></a><a
>          class='caption' href='#'>Sample Carousel Pic Goes Here And The Best
>          Part is that...</a></div>",
>                "content_button": "<div class='thumb'><img src='f2_thumb.jpg' alt='bike is nice'></div><p>Agile Carousel Place
> Holder</p>" }, {
>                "content": "<div class='slide_inner'><a class='photo_link' href='#'><img class='photo' src='banner_paint.jpg'
> alt='Paint'></a><a
>          class='caption' href='#'>Sample Carousel Pic Goes Here And The Best
>          Part is that...</a></div>",
>                "content_button": "<div class='thumb'><img src='f2_thumb.jpg' alt='bike is nice'></div><p>Agile Carousel Place
> Holder</p>" }, {
>                "content": "<div class='slide_inner'><a class='photo_link' href='#'><img class='photo' src='Penguins.jpg'
> alt='Tunnel'></a><a
>          class='caption' href='#'>Sample Carousel Pic Goes Here And The Best
>          Part is that...</a></div>",
>                "content_button": "<div class='thumb'><img src='f2_thumb.jpg' alt='bike is nice'></div><p>Agile Carousel Place
> Holder</p>" }, {
>                "content": "<div class='slide_inner'><a class='photo_link' href='#'><img class='photo' src='Penguins.jpg'
> alt='Tunnel'></a><a
>          class='caption' href='#'>Sample Carousel Pic Goes Here And The Best
>          Part is that...</a></div>",
>                "content_button": "<div class='thumb'><img src='f2_thumb.jpg' alt='bike is nice'></div><p>Agile Carousel Place
> Holder</p>" }
>          
>          ]

这是我的while循环和查询代码

$query=mysql_query("select * from persons  order by date desc LIMIT 0,10 ");

> <img height=200 width=200 src=' upload/". $test['Image'] ."'/> <a href
> ='profile.php?PersonID=$id'> ".$test['LastName'].", ". $test['FirstName']." ". $test['MiddleName']."</a>

这是我调用代码的脚本文件

> agile_carousel_multiple_slides_visible_example.html
> 
> <script>
> 
>     $.getJSON("agile_carousel_data.php", function (data) {
> 
>         $("#multiple_slides_visible").agile_carousel({
>             carousel_data: data,
>             carousel_outer_height: 230,
>             carousel_height      : 200,
>             slide_height         : 200,
>             carousel_outer_width : 480,
>             slide_width          : 260,
>             number_slides_visible: 3,
>             transition_time      : 330,
>             control_set_1: "previous_button,next_button",
>             control_set_2: "group_numbered_buttons",
>             persistent_content: "<p class='persistent_content'>Agile Carousel Example: Multiple Slides Visible</p>"
>         });
> 
>     })
> 
>  </script>

尝试了这段代码,但不会输出任何内容

> `<?php include_once('includes/dbConnect.php'); $data = array(); while
> ($test = mysql_fetch_array($query)) { $data[]['content'] = "<div
> class='slide_inner'><a class='photo_link' href='#'><img class='photo'
> src='{$test['image']}' alt='Bike'></a><a class='caption'
> href='#'>Sample Carousel Pic Goes Here And The Best Part is
> that...</a></div>" $data[]['content_button'] = "<div
> class='thumb'><img src='{$test['thumb']}' alt='bike is
> nice'></div><p>Agile Carousel Place Holder</p>" } echo
> json_encode($data); ?>

`

1 个答案:

答案 0 :(得分:0)

客户端,您的循环应如下所示:

//first assign the entire Array to var whatever
for(var i in whatever){
  var eachObject = whatever[i];
  for(var n in eachObject){
    var eachObjectValue = eachOject[n];
  }
}

如果您只想访问,请说明whatever数组中第3个对象的第2个值,它将如下所示:

whatever[2].content_button or whatever[2]['content_button'];