Welcome Guest, Not a member yet? Register   Sign In
How to use loops in smarty template engine ?
#1

[eluser]Mr.SoOoMa[/eluser]
Hello there ,

When i was programming with the normal PHP without any MVC system, i was using while loops to show the results in smarty templates (with sections).

but now i'm using CI and i don't know how to print the loop in the templates, i'm using library which helps me to use smarty with CI, so how can i use the loops in smarty to show the result?

i tried :

Code:
$query=$this->db->get('settings');
        $this->load->view("admincp/loop",$query);

and the template is :

Code:
<ul>
{foreach from=$query->result_array() item=$settings}
    <li>{$settings}</li>
{/foreach}
</ul>

but it didn't work Sad

but when i'm put

Code:
foreach ($query->result_array() as $row)
        {
         echo $row['value'];
        }

in the model, it prints good .. but i want it to print in the template file not the model, cause i want to help users to change templates content so i can't put <div> for example in the model file Sad i need to use templates to print the loop ..
#2

[eluser]Mr.SoOoMa[/eluser]
+ how can i print the result directly without loops if i'm sure that the query gives me 1 result only ..

#3

[eluser]Aken[/eluser]
http://www.smarty.net/docs/en/language.f...oreach.tpl
#4

[eluser]Mr.SoOoMa[/eluser]
I tried this in the model :

Code:
$query=$this->db->get('settings');
        $this->load->view('admincp/loop',$query);

and this is the template:

Code:
<ul>
{foreach $query->result_array() as $settings}
    <li>{$settings}</li>
{/foreach}
</ul>

but still have error: Undefined index: query
#5

[eluser]Aken[/eluser]
You're passing data to the view incorrectly. http://ellislab.com/codeigniter/user-gui...views.html

You really need to take time to read through these manuals, because they answer all of the questions you have... I'm not going to help you every time you send me a private message.
#6

[eluser]Mr.SoOoMa[/eluser]
I'm sorry Sad

it's working now, i have founded the solution ..

sorry for making you angry Smile




Theme © iAndrew 2016 - Forum software by © MyBB