开发者

Move to item in array but keep ordering

开发者 https://www.devze.com 2023-02-12 23:41 出处:网络
I have an array which I loop through (as part of a javascript banner rotator) <div id=\"featured\" >

I have an array which I loop through (as part of a javascript banner rotator)

<div id="featured" >
<ul> 
<?php
    foreach ($rows as $key => $item) {  
?>
    <li>
        <a href="<?php echo $item->link;?>"><img src="images/stories/thumb_<?php echo strtolower($item->开发者_开发百科title);?>.jpg" title="<?php echo $item->title?>" alt="<?php echo $item->title?>"/></a>
    </li>
<?php
    }
?>  
</ul> 

This always starts the rotator at the first item in the array (as returned by the Model). If I click on an item in the array, I want the rotator to start at the clicked item. How do I move to the right element in the array and keep the order the same?

Edit:

To clarify, the items are returned from a database. The items are ordered according to a specific ordering field. What I want to obtain is:

Default action 1,2,3,4,5

Selected item = 3

3,4,5,1,2

I need to do this in the PHP.


Using Yent's answer

$currentLink = htmlspecialchars(JRequest::getURI());
//Loop through and match URLs
foreach ($rows as $key => $item) {
    $arrayLink = $item->link;
    if ($arrayLink == $currentLink) {
        $index = $key;
    }
}
//Reorder the array
$rows = array_merge(array_slice($rows, $index, null, true), array_slice($rows, 0, $index, true));

My only problem with this is that due to the thumbnails and the main images of the rotator being driven by the same list, my gallery that this generates moves the items around. Might have to tinker with the js.

0

精彩评论

暂无评论...
验证码 换一张
取 消