Welcome Guest, Not a member yet? Register   Sign In
HELP!! How I can pass an array data to index controller function?
#5

[eluser]Michael Wales[/eluser]
The variables that are passed to Controller methods are automatically pulled from the URI. For example:
http://www.mybookstore.com/books/view/32

Could be (in code):
Code:
<?php
class Books extends Controller {

  function view($id) {
    echo $id; // Echo's 32
  }

}
?>

My question would have to be - why are you wanting to pass an array of data to the controller function? In trying to understand your explanation you are retrieving data from a database? Why not just do that within the controller method itself (or even better, a model). For the sake of simplicity - I'll place it all in the controller for now.

controllers/books.php
Code:
<?php
class Books extends Controller {

  function Books() {
    parent::Controller();
  }

  // Index will retrieve a list of all of our books with some basic info
  function index() {
    $query = $this->db->get('books');
    foreach ($query->result() as $book) {
      $data['books'][$book->id]['title'] = $book->title;
      $data['books'][$book->id]['author'] = $book->author;
      $data['books'][$book->id]['price'] = $book->price;
    }
    $this->load->view('books/list', $data);
  }
}
?>

views/books/list.php
Code:
<?php foreach ($books as $id => $book) {
  echo '<p>Title: ' . $book['title'] . '(' . $book['price'] . ')<br />
        Author: ' . $book['author']</p>';
?&gt;


Messages In This Thread
HELP!! How I can pass an array data to index controller function? - by El Forum - 07-30-2007, 02:31 PM



Theme © iAndrew 2016 - Forum software by © MyBB