Welcome Guest, Not a member yet? Register   Sign In
Problem with counter in table
#1

I need to insert an auto incrementing number in a table.

The model is:

PHP Code:
public function players($limit$offset) {
                  
$player $this->load->database('player'TRUE);
                  
$player->select('p.name, p.job, p.level, pindex.empire, p.exp');
                  
$player->from('player AS p');
                  
$player->join('player_index AS pindex''p.account_id = pindex.id''INNER');
                  
$player->not_like('p.name','[');
                  
$player->order_by('exp DESC, level DESC'); 
                  
$player->limit($limit$offset);
                  
$query $player->get();
                  
$query_player $query->result_array();
                  return 
$query_player;
        } 

The controller is:
PHP Code:
public function players($offset 0) {
        
$offset = ($this->uri->segment(3) != '' $this->uri->segment(3): 1);
        
$config = array();
        
$config["base_url"] = base_url() . "index.php/rank/players";
        
$total_row $this->ranks->record_count('player''player');
        
$config["total_rows"] = $total_row;
        
$config["per_page"] = 25;
        
$offset = (($offset-1) * $config["per_page"])+1;        
        
$config['use_page_numbers'] = TRUE;
        
$this->pagination->initialize($config);
        
$str_links $this->pagination->create_links();
        
$data["links"] = explode(' ',$str_links );
        
$data['variable'] = $this->ranks->players($config["per_page"], $offset);
        
$this->smarty->view('rank_player.tpl'$data);
    } 

The template is:

PHP Code:
{foreach $variable as $data}
                    <
tr>
                        <
td>{$data.name}</td>
<
td>{$data.job}</td>
                        <
td>{$data.level}</td>
<
td>{$data.empire}</td>
                        <
td>{$data.exp}</td>
                    </
tr>
                {/foreach} 

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.
Reply
#2

Who Can help me?
Reply
#3

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) {
 
       // ... your code ...
 
       $data['offset'] = $offset;
 
       $this->smarty->view('rank_player.tpl'$data);
 
   

Then simply do addition in your template:
PHP Code:
{foreach $variable as $index => $data}
 
                   <tr>
 
                       <td>{$offset+$index+1}</td>
 
                       <td>{$data.name}</td>
 
                       <!-- other fields -->
 
                   </tr>
 
               {/foreach} 

Not sure it'll work well with smarty syntax, because I'm not that familiar with it. But still you should get the idea.
Reply
#4

(This post was last modified: 05-07-2015, 07:20 AM by StratoKyke.)

Wow Smile Thanks for the reply and yes, that is what I wanted to do.

Can you explain to me what means $index?
Reply
#5

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(=> $player1=> $player2, ... => $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.
Reply
#6

Thanks Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB