2012-03-27 116 views
-1

我使用名为Avactis的购物车。它返回给我很少的“信息标签”用于我的前端。他们看起来像这样:在JSON数据中使用PHP函数

<?php ProductSmImage();?> 
<?php ProductName();?> 

我想在我的一些产品中使用敏捷转盘脚本。我想使用小图片,产品名称,产品替代文字和产品链接。

的JSON数据文件是这样的:

[{ 
"content": "<div class='slide_inner'><a class='photo_link' href='#'><img class='photo' src='images/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='images/f2_thumb.jpg' alt='bike is nice'></div><p>Agile Carousel Place Holder</p>" 
}] 

而在前端的脚本是这样的:

<script> 

// Code used for "Flavor 2" example (above) 

$.getJSON("agile_carousel/agile_carousel_data.php", function(data) { 
    $(document).ready(function(){ 
     $("#flavor_2").agile_carousel({ 

      // required settings 

      carousel_data: data, 
      carousel_outer_height: 330, 
      carousel_height: 230, 
      slide_height: 230, 
      carousel_outer_width: 480, 
      slide_width: 480, 

      // end required settings 

      transition_type: "fade", 
      transition_time: 600, 
      timer: 3000, 
      continuous_scrolling: true, 
      control_set_1: "numbered_buttons,previous_button, 
      ... (continues on same line)... pause_button,next_button", 
      control_set_2: "content_buttons", 
      change_on_hover: "content_buttons" 
     }); 
    }); 
}); 

所以,我想,以取代第一HREF与图像的src和等等。

我认为这与JSON编码有关,但我不是程序员(显然),并且需要知道在哪里编写代码以将标记转换为JSON数据,如果您可以给我一小部分样本要遵循的代码,我会永远感激。

谢谢!

+0

我假设尾随的“]”实际上在JSON文件中? – Hubro 2012-03-27 15:03:27

+0

你可以发布创建这个JSON字符串的代码吗?即时猜测你想要修改?它不容易理解你想要做什么......用你想要它做什么语言...... – ManseUK 2012-03-27 15:03:27

+0

¿你如何得到这个json? ¿通过PHP或jQuery的? – Patroklo 2012-03-27 15:03:54

回答

0

一些快速肮脏的方式如何使用PHP,你可以做你所要求的。

<?php 

$json = "[{ 
\"content\": \"<div class='slide_inner'><a class='photo_link' href='#'><img class='photo' src='images/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='images/f2_thumb.jpg' alt='bike is nice'></div><p>Agile Carousel Place Holder</p>\" 
}]"; 

$data = json_decode($json); 

$content = $data[0]->content; 

$src = substr($content, strpos($content, 'src=') + 5); 
$src = substr($src, 1, strpos($src, "'")-1); 

$content = str_replace("href='#'", "href='$src'", $content); 

echo $content;