Problem with counter in table - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Problem with counter in table (/showthread.php?tid=61676) |
Problem with counter in table - StratoKyke - 05-05-2015 I need to insert an auto incrementing number in a table. The model is: PHP Code: public function players($limit, $offset) { The controller is: PHP Code: public function players($offset = 0) { The template is: PHP Code: {foreach $variable as $data} The template is in smarty template. How can I insert an auto incrementing number that continues to increase with changing page? And it can set within that foreach? Sorry for the inconvenience, but I just can not figure out how. Thanks for the answers. RE: Problem with counter in table - StratoKyke - 05-07-2015 Who Can help me? RE: Problem with counter in table - codinghamster - 05-07-2015 Hi. For me is a bit confusing what kind of auto incrementing field you want. You need something like the order number for the list of players that is persistent through all the pages? Like this? Page 1: 1. Player1 2. Player2 ... 25. Player25 Page 3: 51. Player51 52. Player52 and so on? If this is your case, then you can add the offset parameter to the template data in your controller: PHP Code: public function players($offset = 0) { Then simply do addition in your template: PHP Code: {foreach $variable as $index => $data} Not sure it'll work well with smarty syntax, because I'm not that familiar with it. But still you should get the idea. RE: Problem with counter in table - StratoKyke - 05-07-2015 Wow Thanks for the reply and yes, that is what I wanted to do. Can you explain to me what means $index? RE: Problem with counter in table - codinghamster - 05-07-2015 Sure. $index is the variable that will hold the numerical position of the element in array. Your PHP Code: array($player1, $player2, ... $playerN) is actually something like the PHP Code: array(0 => $player1, 1 => $player2, ... N - 1 => $playerN) So you can just grab that position into the $index variable in the foreach expression. Also because of the arrays start from 0 you need that +1 thing in the template. RE: Problem with counter in table - StratoKyke - 05-07-2015 Thanks |