Welcome Guest, Not a member yet? Register   Sign In
How to echo this array?
#1

Hi guys,

I send an array $data['content'] into view.

and here are the content of this array.

Code:
array(2) {
 [0]=>
 array(5) {
   ["subj_id"]=>
   string(1) "1"
   ["subject_name"]=>
   string(7) "english"
   ["photo_url"]=>
   string(0) ""
   ["author"]=>
   string(5) "jason"
   ["date_created"]=>
   string(10) "0000-00-00"
 }
 [1]=>
 array(5) {
   ["subj_id"]=>
   string(1) "2"
   ["subject_name"]=>
   string(4) "math"
   ["photo_url"]=>
   string(0) ""
   ["author"]=>
   string(4) "anna"
   ["date_created"]=>
   string(10) "0000-00-00"
 }
}



Now how to echo 'subject_name' and 'author' using foreach?
Reply
#2

Well your question is a bit unclear. $data['content'] is the array $data with an element 'content'. Is the array you have shown the element 'content' or the array $data itself?

In your controller something like:
PHP Code:
public function index()
{
 
   $data = array();

 
   $content = array(
 
       array(
 
           'subject_name' => 'Maths',
 
           'subject_author' => 'Peter',
 
       ),
 
       array(
 
           'subject_name' => 'English',
 
           'subject_author' => 'John',
 
       ),
 
   );
 
   $data['content'] = $content;

 
   $this->load->view('homepage'$data);


In your 'homepage.php' view file something like:
PHP Code:
<?php if ( ! empty($content)) { ?>

     <?php foreach ($content as $item) { ?>

          <?php echo $item['subject_name']; ?>
          <?php echo $item['subject_author']; ?>

     <?php ?>

<?php ?>

I hope that helps but perhaps you were asking something more complicated that I have missed.

Best wishes

Paul.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB