3 Replies - 6569 Views - Last Post: 29 January 2014 - 12:37 AM Rate Topic: -----

#1 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 440
  • View blog
  • Posts: 1,867
  • Joined: 30-April 10

Passing Multidimensional array to Codeigniter view

Posted 30 January 2013 - 12:39 AM

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:
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.

Is This A Good Question/Topic? 0
  • +

Replies To: Passing Multidimensional array to Codeigniter view

#2 Sho Ke  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 110
  • View blog
  • Posts: 250
  • Joined: 13-October 11

Re: Passing Multidimensional array to Codeigniter view

Posted 30 January 2013 - 07:35 AM

I think when I used CI, I always put any information I want sent to the view nested in a "master" array, so to speak. Something like this:
$parent_array = array('img' => $img); //$img would be the same as your multi dimensional array seen at the top of your post
$this->load->view('welcome_message', $parent_array);


Then in the view:
var_dump($img);
echo $img['a1']['name']; //test.jpg


Was This Post Helpful? 2
  • +
  • -

#3 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 440
  • View blog
  • Posts: 1,867
  • Joined: 30-April 10

Re: Passing Multidimensional array to Codeigniter view

Posted 30 January 2013 - 04:12 PM

Solution thanks to Sho Ke:

I added a key to the variable name to pass like so :

Controller:
			$this->load->model('images_model');
//create key for array to pass as array
			$img['base'] = $this->images_model->get_last_ten();
			//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');



Then used that key name as the new array:

View:
	<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>
		<div style="text-align:center">
		<?php 
			foreach($base as $img){ 
				
				echo "<p style='width:90%; margin-right:auto; margin-left:auto; margin-bottom:26px;'>";
				echo "<b>" . $img['name'] . "</b></br>";
				echo $img['date'] . "</br>";
				echo "<img src='" . $img['url'] . "' alt='" . $img['name'] . "'/>";
				echo "</p>";
			}
		?>
		</div>
	</div>



Works like a charm thank you for your help I was having trouble understanding the passing in codeigniter and now I get it.
Was This Post Helpful? 1
  • +
  • -

#4 piyushbajaj0704  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-January 14

Re: Passing Multidimensional array to Codeigniter view

Posted 29 January 2014 - 12:37 AM

Thanks mate.. this piece of code really helped me a lot. Thanks a ton,buddy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1