Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Help for foreach

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
StratoKyke
Smarty Rookie


Joined: 30 Apr 2015
Posts: 9

PostPosted: Thu Apr 30, 2015 4:25 pm    Post subject: Help for foreach Reply with quote

I use codeigniter and I have an problem with use foreach in the template.

My model:
Code:

public function players() {
            $player = $this->load->database('player', TRUE);
            $player->start_cache();
            $player->select('name', 'job', 'level');
            $player->stop_cache();
            $query = $player->get('player');
            $query_ultimate = $query->result();
            return $query_ultimate;
      }


My controller:

Code:
public function players() {
      $this->ranks->players();
      $data['variable'] = $this->ranks->players();
      $this->smarty->view('rank_player.tpl', $data);
   }


And this is the part of the template:

Code:
{foreach $variable as $data}
               {$data.name}
               {$data.job}
               {$data.level}
            {/foreach}


The error that I check is the following.

Code:
Cannot use object of type stdClass as array


It is probably a stupid mistake. But I start use smarty recently.

Thanks for your answers.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri May 01, 2015 2:59 pm    Post subject: Reply with quote

Your database results of player() seem not to be returned as an array.

Have a look at a var_dump of the results.
Back to top
View user's profile Send private message
StratoKyke
Smarty Rookie


Joined: 30 Apr 2015
Posts: 9

PostPosted: Sun May 03, 2015 9:47 am    Post subject: Reply with quote

If I edit the controller in this way:

Code:
public function players() {
      $this->ranks->players();
      $data['variable'] = $this->ranks->players();
      $this->smarty->view('rank_player.tpl', var_dump($data));
   }


They only print the name in the array. But not the jobs and the level.


P.S. If the template is only the foreach with variable name, errors are the following:

Code:
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: libraries/Smarty.php

Line Number: 25

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined index: variable

Filename: sysplugins/smarty_internal_templatebase.php(171) : eval()'d code

Line Number: 40

Code:
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: sysplugins/smarty_internal_templatebase.php(171) : eval()'d code

Line Number: 40


But if i edit the controller and remove the var_dump returns the same error as before
thank for the support.
Back to top
View user's profile Send private message
StratoKyke
Smarty Rookie


Joined: 30 Apr 2015
Posts: 9

PostPosted: Sun May 03, 2015 10:41 am    Post subject: Reply with quote

Ok, I solved the problem partially.

I modified the models in this way
Code:
public function players() {
            $player = $this->load->database('player', TRUE);
            $player->start_cache();
            $player->select('name', 'job', 'level');
            $player->stop_cache();
            $query = $player->get('player');
            $query_ultimate = $query->result_array();
            return $query_ultimate;
      }


the view:
Code:
<tbody>
            {foreach $variable as $data}
               <tr>
                  <td>{$data.name}</td>
                  <td>{$data.level}</td>
                  <td>{$data.job}</td>
               </tr>
            {/foreach}
            </tbody>


and controller
Code:
public function players() {
      $this->ranks->players();
      $data['variable'] = $this->ranks->players();
      $this->smarty->view('rank_player.tpl', $data);
   }


In this way, I was able to print the names but It should not be the level and the job.
Code:
Undefined index: level
Undefined index: job
Back to top
View user's profile Send private message
StratoKyke
Smarty Rookie


Joined: 30 Apr 2015
Posts: 9

PostPosted: Sun May 03, 2015 1:25 pm    Post subject: Reply with quote

I fix the problem.

The query was poorly written.

wrong:
Code:
 $player->select('name', 'job', 'level');


Right:

Code:
 $player->select('name, job, level');


Thanks for the help Smile
Reply here if you can help.
http://www.smarty.net/forums/viewtopic.php?t=25513
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP