• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question: CI & Twig. Show DB results

#1
[eluser]josepichu[/eluser]
Hi!

I have a problem using CI with Twig Template engine.

I want to show my DB results into my views.

i have do it (i simplify it):

Code:
My Controller

function index() {

   $data['data'] = $this->my_model->show_results();

   $this->twig->display('layout.php', $data);

}

Code:
My Model

function show_results() {

   $sql="select * from my_table";
   $result = mysql_query($sql);
   while ($row = mysql_fetch_Array($result)) {

      $data = array( array('param1' => 'value1',
                           'param2' => 'value2'));

   }

   return $data;

}

Code:
My view - layout.php

<ul>

{% for dat in data %}
  
    <li> {% dat.param1 %} </li>

{% enfor %}

</ul>

I want to show all the query results, with my code i only show the last row of my query.

Help please!

#2
[eluser]Davide Bellini[/eluser]
mmmm ... check code into your model

Code:
while ($row = mysql_fetch_Array($result)) {

      $data = array( array('param1' => 'value1',
                           'param2' => 'value2'));

   }

with this you overwrite $data value on every loop ... use something like :

Code:
while ($row = mysql_fetch_Array($result)) {

      $data[] = array( array('param1' => 'value1',
                           'param2' => 'value2'));

   }

#3
[eluser]josepichu[/eluser]
yes, i've do it and it works¡¡

thanks.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.