2013-08-02 72 views
0

我有一个json的代码,但我不知道如何输入while循环。json从sql数据库输入数据while循环

我有代码编辑的内容,但我没有一个想法如何while 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> 

尝试这种代码,但赢得了” t输出任何东西

> `<?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); ?> 

`

+0

在这种情况下,您的PHP与JSON有什么关系? – PHPglue

+0

我不清楚你在问什么。 –

+0

我在我的数据库上有一个图像名称尝试查看链接:D我使用这个php json布局。所以我问的是如何把我的while循环命令替换我从这个网站获得的字段这里是原件,所以我要从我的数据库调用数据并显示它,就像我给出的链接。 [链接](http://demo1.krablekod.ru/agile_carousel/examples/agile_carousel_multiple_slides_visible_example.html) –

回答

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数组中的第2个值将是这样的:

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

嗯不完全得到你的例子对不起,我真的不知道json我还在学习 –

+0

你正在处理一个包含一堆JavaScript对象的数组,它们更像联合数组,如果它们的属性中没有空格,则可以使用点来访问它们。如果你的财产是“内容按钮”:“价值在这里”,那么你不能使用点。 – PHPglue

+0

是的,这是一个搞砸我的头,那件事和{}。谢谢,但我得到了流量,但代码使用for循环我没有得到它只是一个初学者抱歉 –