Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
simple select query to pass data array to view
  • broken code
    <?php
    class Controller_Add extends Controller {
     
     public function action_index() {
      
      $data['records'] = DB::select('idea', 'timestamp')->from('idea')->as_object()->execute();
      
      $this->response->body = View::factory('howdy-view', $data);
     }
    }
    ?>
    

    view:
    <h1>Howdy</h1>
    
    <table>
    <tr>
      <th>Idea</th>
      <th>Date</th>
      <th>Delete</th>
    </tr>
    <?php foreach($records as $r): ?>
    <tr>
      <td><?=$r->idea?></td>
      <td><?=$r->timestamp?></td>
      <td>del</td>
    </tr>
    <?php endforeach; ?>
    </table>
    

    Error receiving, in screenshot: http://cl.ly/90bF Any help appreciated. Fixed code (works) Fixed, just edited query to be:
    $data['records'] = DB::select('idea', 'timestamp')->from('idea')->execute()->as_array();
    

    and view to be:
    <?php foreach($records as $r): ?>
     <p><?=$r['idea']?></p>
     <p><?=$r['timestamp']?>
    <?php endforeach; ?>
    
  • We need a little bit more info...you give 2 views there, Just give this: The code from the controller, and the view code that is causing the issue. Lets not confuse it with 2 different versions of the same code.
  • Can you try changing this:
    <?=$r->idea?>
    

    To this:
    <?php echo $r->idea; ?>
    

    See if that works. Just want to see something.
  • No it does not work, error msg: http://cl.ly/90La Code you asked me to change: http://cl.ly/90oP
    <?php foreach($records as $r): ?>
     <p><?php echo $r->idea; ?></p>
     <p><?php echo $r->timestamp; ?>
    <?php endforeach; ?>
    

    Running on mamp, php 5.3.2 http://cl.ly/90xq | http://cl.ly/90MM
  • Ya, I know what is going on. Can you try taking out the ->as_object() and see if that works?
  • Controller
    <?php
    class Controller_Add extends Controller {
     
     public function action_index() {
      
      $data['records'] = DB::select('idea', 'timestamp')->from('idea')->execute();
     
     $this->response->body = View::factory('howdy-view', $data);
    
     }
    }
    ?>
    

    view
    <?php foreach($records as $r): ?>
     <p><?php echo $r->idea; ?></p>
     <p><?php echo $r->timestamp; ?>
    <?php endforeach; ?>
    

    Error message I am now receiving: http://cl.ly/908R
  • Ok, if you can just keep it as the array version it will work. It is because of the Auto-Encoding that the view does. We need to come up with a solution for this tho. I will have a look a little later. You COULD probably add the Fuel\Core\Database_Result class to the security.whitelisted_classes config, but that wouldn't be very secure. We will come up with something. Just use the array for now. (they are easier to work with anyway). Dan
  • Try View::factory('howdy-view', $data, FALSE);
  • Only disable the auto encoding if you 100% trust the data you are outputting (which is what setting false as the 3rd param does). If it is data from a user then you should encode it. Dan
  • You are trying to pass an object to the view controller and 'auto_encode_view_data' => true, will not allow it. You have two choices: 1: Turn off 'auto_encode_view_data' => false, // Not recommended
    2: return data as_array()
  • Thanks Phil, look at my first post, I posted the working code, is that what you envisioned for choice 2?
  • Hi ya, This should work... $data = DB::select('idea', 'timestamp')->from('idea')->execute()->as_array(); Then access them by using <?php echo $r; ?>
  • Sorry :( Just skim read it whilst waiting for code to compile. Yes you are correct. That is basically how I have done it.
  • if you pass an object the the view using set_global .. the data is encoded right? $this->template->set_global('user', $user);

Howdy, Stranger!

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

In this Discussion