开发者

php: add array placement number to each array set e.g. 1 2 3

I have a data set from mysql in a multidimensional array.

I am using a pagination script to show 10 per page of that array. I want to know how to append a number to each one like a scoring system. The array is sorted by the way that it will aways be so i want to to add a 1 to the first item, a 2 to the second item and so on.

I dont want to do this in the foreach that i output since this will not translate over to the second page where it would start back at 1.

Any ideas on how to attach a number in des开发者_如何学JAVAc order to each item in the array so i can access it in the foreach and display it?

Thanks in advance


Use the same math used to find the offset in your pagination query and start counting from there.


fist you need to save the indexes in a $_SESSION variable:

$_SESSION['indexes'] = array();

and for multidimentional:

foreach( $array as $index=>$arrValue) {
            echo $index;
    foreach($arrValue as $index2=>$value){
            echo $index2;
            $_SESSION['indexes'][$index][$index2] = $value;
            echo $value;
     }
}

than you can go through all of the session indexes; where $index is the page or $index2 can be the row


I figured it out by doing some calculations based on which page i was on and the page count that was set and finding the size of the array from the db:

$all_members = get_users_ordered_by_meta('metavalue', 'desc'); 
$array_count = count($all_members);
$posts_per_page = get_option('posts_per_page');

if ($page > 0) {
    $total_posts_avalible = ($posts_per_page * $page);
    $usernum = $total_posts_avalible - $posts_per_page;
}

Then i echo the $usernum in the foreach next to the name of the user.

foreach() {
    $usernum++; 
    echo $usernum; echo $user_name;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜