Welcome Guest, Not a member yet? Register   Sign In
Data in View
#1

[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>
&lt;? foreach ($query->result() as $row):?&gt;
<li>&lt;?=$row->id?&gt; | &lt;?=$row->name?&gt; | <a >id?&gt;">Edit</a> | <a >id?&gt;">Delete</a></li>
&lt;? endforeach;?&gt;
</ul>

How I may get $query in controller and put in view as param?
#2

[eluser]indapublic[/eluser]
In example, I read

Code:
&lt;?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);
    }
}
?&gt;

and

Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;?php echo $title;?&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
<h1>&lt;?php echo $heading;?&gt;</h1>
    
<h3>My Todo List</h3>    

<ul>
&lt;?php foreach($todo_list as $item):?&gt;

<li>&lt;?php echo $item;?&gt;</li>

&lt;?php endforeach;?&gt;
</ul>
    
&lt;/body&gt;
&lt;/html&gt;

but how I can use it?
#3

[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
#4

[eluser]Mantero[/eluser]
Your model:

Code:
&lt;?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:
&lt;?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);
    }
}
?&gt;

your view:
Code:
echo $title; // Show title
echo $heading; // Show heading
echo $places; // Show places
#5

[eluser]indapublic[/eluser]
Tnanks all




Theme © iAndrew 2016 - Forum software by © MyBB