[英]jCarousel dynamic content loading via Ajax
我嘗試制作一個滑塊並從php文件動態加載數據,我從數據庫中獲取圖像,但是現在我有問題鏈接此圖像...
這是我的php文件ajax_php.php
// Array indexes are 0-based, jCarousel positions are 1-based.
$first = max(0, intval($_GET['first']) - 1);
$last = max($first + 1, intval($_GET['last']) - 1);
$length = $last - $first + 1;
// ---
require_once('../../includes/config.php');
error_reporting(0);
mysql_connect($conf['host'], $conf['user'], $conf['pass']);
mysql_select_db($conf['name']);
$getUser = "SELECT * FROM sitex WHERE img_i!='images/img_i.jpg' ORDER BY `sitex`.`likes` DESC LIMIT 35";
$res = mysql_query($getUser) or die();
$images = array();
while(($row = mysql_fetch_assoc($res))) {
$images[] = '/images/'.$row['img_i'].'';
}
$total = count($images);
$selected = array_slice($images, $first, $length);
// ---
header('Content-Type: text/xml');
echo '<data>';
echo ' <total>' . $total . '</total>';
foreach ($selected as $img) {
echo '<image>' . $img . '</image>';
}
echo '</data>';
這是js
function mycarousel_itemLoadCallback(carousel, state)
{
// Check if the requested items already exist
if (carousel.has(carousel.first, carousel.last)) {
return;
}
jQuery.get(
'ajax_php.php',
{
first: carousel.first,
last: carousel.last
},
function(xml) {
mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, xml);
},
'xml'
);
};
function mycarousel_itemAddCallback(carousel, first, last, xml)
{
// Set the size of the carousel
carousel.size(parseInt(jQuery('total', xml).text()));
jQuery('image', xml).each(function(i) {
carousel.add(first + i, mycarousel_getItemHTML(jQuery(this).text()));
});
};
function mycarousel_getItemHTML(url)
{
return '<img src="' + url + '" width="75" height="75" alt="" />';
};
jQuery(document).ready(function() {
jQuery('#futuredbc').jcarousel({
easing: 'BounceEaseOut',
itemVisibleOutCallback: {onAfterAnimation: function(carousel, item, i, state, evt) { carousel.remove(i); }},
itemLoadCallback: mycarousel_itemLoadCallback
});
});
這種方法使我的結果像
<img width="75" height="75" alt="" src="img.jpg">
但是我需要制作一個像
<a href="http://site.com/548"><img width="75" height="75" alt="" src="img.jpg"></a>
所以site.com/ 548是我的ID,我可以從數據庫'.$row['id'].'
獲取它'.$row['id'].'
但我不知道如何制作js和鏈接結果
最簡單的方法(不添加用於解析另一個XML標記的功能( <bla>
))只是將ID添加到image xml tag
的值中,並使用delimiter
將ID從URL中split
出來。
將row ID
添加到數組:
while(($row = mysql_fetch_assoc($res))) {
$images[] = array('id' => $row['id'] , 'src' => '/images/'.$row['img_i'].'');
}
將其打印到XML:
foreach ($selected as $img) {
echo '<image>' . $img['src'] . '|||'.$img['id'].'</image>';
}
然后,重新整理:
return '<img src="' + url + '" width="75" height="75" alt="" />';
帶有:
var tmp = url.split('|||');
return '<a href="www.site.com/'+ tmp[1] +'"><img src="' + tmp[0] + '" width="75" height="75" alt="" /></a>';
應該管用。 否則,請發表評論,我會修復它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.