Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Query on foreach / while in view
  • Been trying to find the best technique for querying a table while inside a foreach or while loop. E.g Listing all the pages of a website - each page has an parent_id which is the id of another page (maybe 0) While in the foreach(i think?) I need to get the parent_id link it to the correct page with that id and display the title. How can i do this? Is there a really simple way with fuel?
  • You should not run queries in your views. Either code it in your controller, or, if it's specific to the view and doesn't belong in the controller, use a viewmodel instead of a view.
  • Any examples?
  • You can prepare an array for pages in you controller
    class Controller_Pages extends Controller {
    
      public function action_list()
      {
         // Extract pages from DB ( optionally joined with another relantionship table )
         $data['pages'] = DB::select()->from('pages)->execute()->as_array();
    
         return Response::forge($view, $data);
      }
    
    }
    

    and in your view run a foreach on this array :
    <ul>
      <?php
      
       foreach($pages as $page)
       &#123;
           .....
       }
    
       ?>
    </ul>
    

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion