Data in View - El Forum - 01-29-2011
[eluser]indapublic[/eluser]
Good day.
I have a model
Code: <?php
# Модель Места
class Places_model extends Model
{
function get_places_by_user($user_id)
{
$sql = "select * from places where user = ? order by created";
return $this->db->query($sql, $user_id);
}
}
?>
and view
Code: <?
$my_id = $this->session->userdata["user_id"];
$query = $this->places_model->get_places_by_user($my_id);
?>
<div><strong>My Places</strong></div>
<ul>
<? foreach ($query->result() as $row):?>
<li><?=$row->id?> | <?=$row->name?> | <a >id?>">Edit</a> | <a >id?>">Delete</a></li>
<? endforeach;?>
</ul>
How I may get $query in controller and put in view as param?
Data in View - El Forum - 01-29-2011
[eluser]indapublic[/eluser]
In example, I read
Code: <?php
class Blog extends Controller {
function index()
{
$data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');
$data['title'] = "My Real Title";
$data['heading'] = "My Real Heading";
$this->load->view('blogview', $data);
}
}
?>
and
Code: <html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<h1><?php echo $heading;?></h1>
<h3>My Todo List</h3>
<ul>
<?php foreach($todo_list as $item):?>
<li><?php echo $item;?></li>
<?php endforeach;?>
</ul>
</body>
</html>
but how I can use it?
Data in View - El Forum - 01-29-2011
[eluser]mi6crazyheart[/eluser]
@indapublic
For u'r 1st post, controller should be like this...
Code: $this->load->model('Places_model');
$data = $this->Places_model->get_places_by_user($user_id);
$this->load->view('view_page',$data);
For u'r 2nd post, in VIEW file...
Code: echo $title; // Show title
echo $heading; // Show heading
Data in View - El Forum - 01-29-2011
[eluser]Mantero[/eluser]
Your model:
Code: <?php
class Places_model extends Model
{
function get_places_by_user($user_id)
{
$data = array();
$this-> db-> where('user', $user_id);
$this-> db-> orderby('created','asc');
$Q = $this-> db-> get('places');
if ($Q-> num_rows() > 0)
{
foreach ($Q- > result_array() as $row)
{
$data[] = $row;
}
}
$Q- > free_result();
return $data;
}
your controller:
Code: <?php
class Blog extends Controller {
function index()
{
$this->load->model('Places_model');
$data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');
$data['title'] = "My Real Title";
$data['heading'] = "My Real Heading";
$data['places'] = $this->Places_model->get_places_by_user($user_id);
$this->load->view('blogview', $data);
}
}
?>
your view:
Code: echo $title; // Show title
echo $heading; // Show heading
echo $places; // Show places
Data in View - El Forum - 01-30-2011
[eluser]indapublic[/eluser]
Tnanks all
|