Reputation: 776
I have the following problem:
I have a controller in which I have:
$this->load->model('girl_model');
$ragazze = $this->girl_model->get_girls();
$data['content'] = 'view_news';
$data['girls'] = $girls;
in my main view I have:
<html>
<head>
<title> </title>
<link rel="stylesheet" type="text/css" href="/assets/css/style.css">
</head>
<body>
<div id="menu">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</div>
<div id="main-content">
<?php $this->load->view($content, array('girls' => $girls) ); ?>
</div>
<div id="footer">
Copy Right 2015
</div>
</body>
In my subview I have:
<?php echo $girls ?>
<?php foreach ($girls as $girl):?>
<tr>
<td><?php echo $girl; ?></td><br/>
</tr>
<?php endforeach;?>
The problem is that I don't see the results of my query in the subview. I only see the Array word printed. It's like something is not passed correctly. I'm new in CodeIgniter and I don't understand why it doesn't work. Can you properly explain to me the solution?
Upvotes: 0
Views: 3090
Reputation: 1138
HI your code should be :
controller :
$this->load->model('girl_model');
$ragazze = $this->girl_model->get_girls();
$data['content'] = 'view_news';
$data['girls'] = $ragazze ;
//echo '<pre>';print_r($data) //debug here if you want
this->load->view('YourMainView',$data);
Main View :
<html>
<head>
<title> </title>
<link rel="stylesheet" type="text/css" href="/assets/css/style.css">
</head>
<body>
<div id="menu">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</div>
<div id="main-content">
<?php $this->load->view($content,$girls); ?>
</div>
<div id="footer">
Copy Right 2015
</div>
</body>
In Sub View :
<?php print_r($girls); ?>// $girls is an array you can not echo it you have to use print_r().
<?php foreach ($girls as $girl) ?>
<tr>
<td><?php echo $girl['field_name']; ?></td>//field name in your table.
</tr>
<?php endforeach;?>
Upvotes: 1
Reputation: 648
just load that $data in first view and it will that variable is automatically available in included view. in your case. please change according to written bellow.
$this->load->model('girl_model');
$ragazze = $this->girl_model->get_girls();
$data['content'] = 'view_news';
$data['girls'] = $ragazze;
load this data in this controller.
$this->load->view('viewfilename',$data);
Now $girls is available for viewfilename.php and any view included in this viewfilename.php .
<html>
<head>
<title> </title>
<link rel="stylesheet" type="text/css" href="/assets/css/style.css">
</head>
<body>
<div id="menu">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</div>
<div id="main-content">
<?php $this->load->view($content); ?>
</div>
<div id="footer">
Copy Right 2015
</div>
now change it according to it and $girls will be available to $content views as well.
Upvotes: 1