Hello,
I am new to codeigniter and mvc. I have been working on a test project that has a page that will display images with data from the database. I have this data saved in an array of arrays as such:
This is populated by the images_model:
Then the controller sends the array of arrays to the view:
Controller code:
In the view is where the problem arises, I am not sure how to write the foreach loop to get the individual elements from the "a1, a2, ..." arrays.
View Code:
When I dump the $row array I get a list of strings with no index:
My question is how do I access each individual with no index to go on? I would like to be able to use them to display the id, name, date, and use the url to add an <img /> tag.
Any help will be greatly appreciated.
Thank you for your time.
I am new to codeigniter and mvc. I have been working on a test project that has a page that will display images with data from the database. I have this data saved in an array of arrays as such:
Array ( [a1] => Array ( [id] => 2 [name] => test.jpg [url] => http://localhost/images/test.jpg [date] => 2013-01-29 ) [a2] => Array ( [id] => 1 [name] => test.jpg [url] => http://localhost/images/test.jpg [date] => 2013-01-29 ) )
This is populated by the images_model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Images_model extends CI_Model { public function get_last_ten(){ $q = $this->db->query("SELECT * FROM images ORDER BY id DESC LIMIT 10"); $i=1; foreach ($q->result() as $row) { $img['a'.$i]['id'] = $row->id; $img['a'.$i]['name'] = $row->name; $img['a'.$i]['url'] = $row->url; $img['a'.$i]['date'] = $row->uploaded_date; $i++; } return $img; } }
Then the controller sends the array of arrays to the view:
Controller code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Welcome extends CI_Controller { public function index() { $userData = $this->session->userdata('email'); if(empty($userData)){ $this->load->view('head_view'); $this->load->view('welcome_message'); $this->load->view('foot_view'); } else{ $this->load->model('images_model'); $img = $this->images_model->get_last_ten(); //show contents of $img print_r($img); //load views $this->load->view('head_view'); //pass $img to logged_in_message view $this->load->view('logged_in_message', $img); $this->load->view('foot_view'); } } }
In the view is where the problem arises, I am not sure how to write the foreach loop to get the individual elements from the "a1, a2, ..." arrays.
View Code:
<h1>Welcome <?php echo $this->session->userdata('email'); ?> to Layton's Picture Site </h1> <div id="body" class="container"> <p style="text-align:center">Thank you for coming to see Layton's photos.</p> <p style="text-align:center"> <?php foreach($a1 as $row){ var_dump($row); } ?> </p> </div>
When I dump the $row array I get a list of strings with no index:
string(1) "2" string(8) "test.jpg" string(32) "http://localhost/images/test.jpg" string(10) "2013-01-29"
My question is how do I access each individual with no index to go on? I would like to be able to use them to display the id, name, date, and use the url to add an <img /> tag.
Any help will be greatly appreciated.
Thank you for your time.