0

スライダーを作成して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 it'a i IDであり、データベースから取得できます'.$row['id'].'が、jsとリンクの結果を作成する方法がわかりません。

4

1 に答える 1

1

(別のXMLタグ(<bla>)を解決する関数を追加せずに)最も簡単な方法は、IDをの値に追加し、をimage xml tag使用して、URLからIDを取得することです。delimitersplit

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>';

動作するはずです。それ以外の場合は、コメントして修正します。

于 2012-08-04T15:31:16.493 に答える