CodeIgniter Forums
how to display data to the view? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: how to display data to the view? (/showthread.php?tid=19282)

Pages: 1 2


how to display data to the view? - El Forum - 06-03-2009

[eluser]yudahebat[/eluser]
I have a model like this
Code:
class Crud extends Model
{
    function Crud()
    {
        parent::Model();
    }
    
/* PILIH DATA */

    function cetak_data()
    {
        $this->db->query('SELECT tgl_pesan FROM reservasi WHERE tgl_pesan BETWEEN '. $this->input->post('dari_tgl').
        ' AND '.$this->input->post('sampai_tgl').'');
        $query = $this->db->get('reservasi');
        foreach ($query->result() as $row)
        {
             $data['id'] = $row->id;
            $data['id_user'] = $row->id_user;
            $data['tgl_pesan'] = $row->tgl_pesan;
            $data['biaya_total'] = $row->biaya_total;
        }
        return $data;
    }
}

and controller like this

Code:
function cetak_data()
    {
        $data['css1']        = $this->css;
        $data['base']         = $this->base;
        $data['menu_adm']     = $this->menu;
        $data['myrobots']     = '<meta name="robots" content="noindex,nofollow">';
        $data['mywebtitle'] = 'mrputucar | Cetak Data';
        $data['pesan'] = $this->Crud->cetak_data();

        $this->load->view('cetak_data',$data);  
    }

and my view like this
Code:
<h1 style="color:#E7541A;">Data User</h1>
        <table width="925" border="0" cellpadding="0" cellspacing="0" class="tabel" >
        <tr>
            <td width="60" class="tabel"><p>TANGGAL</p></td>
            <td width="90" class="tabel"><p>ID</p></td>
            <td width="80" class="tabel"><p>ID USER</p></td>
            <td width="150" class="tabel"><p>BIAYA TOTAL</p></td>
        </tr>
        <tr>
            <td  class="tabel2" valign="top"><p>&lt;?= $pesan;?&gt;</p></td>
            <td  class="tabel2" valign="top"><p>&lt;?= $pesan;?&gt;</p></td>
            <td  class="tabel2" valign="top"><p>&lt;?= $pesan;?&gt;</p></td>
            <td  class="tabel2" valign="top"><p>&lt;?= $pesan;?&gt;</p></td>
            </tr>
        </table>

but its didn't show the data
on $pesan just become array on the view not the data that ive choose...

what wrong? help please...


how to display data to the view? - El Forum - 06-03-2009

[eluser]Thorpe Obazee[/eluser]
[quote author="yudahebat" date="1244032293"]I have a model like this
Code:
class Crud extends Model
{
    function Crud()
    {
        parent::Model();
    }
    
/* PILIH DATA */

    function cetak_data()
    {
        $this->db->query('SELECT tgl_pesan FROM reservasi WHERE tgl_pesan BETWEEN '. $this->input->post('dari_tgl').
        ' AND '.$this->input->post('sampai_tgl').'');
        $query = $this->db->get('reservasi');
        foreach ($query->result() as $row)
        {
             $data['id'] = $row->id;
            $data['id_user'] = $row->id_user;
            $data['tgl_pesan'] = $row->tgl_pesan;
            $data['biaya_total'] = $row->biaya_total;
        }
        return $data;
    }
}

and controller like this

Code:
function cetak_data()
    {
        $data['css1']        = $this->css;
        $data['base']         = $this->base;
        $data['menu_adm']     = $this->menu;
        $data['myrobots']     = '&lt;meta name="robots" content="noindex,nofollow"&gt;';
        $data['mywebtitle'] = 'mrputucar | Cetak Data';
        $data['pesan'] = $this->Crud->cetak_data();

        $this->load->view('cetak_data',$data);  
    }

and my view like this
Code:
<h1 style="color:#E7541A;">Data User</h1>
        <table width="925" border="0" cellpadding="0" cellspacing="0" class="tabel" >
        <tr>
            <td width="60" class="tabel"><p>TANGGAL</p></td>
            <td width="90" class="tabel"><p>ID</p></td>
            <td width="80" class="tabel"><p>ID USER</p></td>
            <td width="150" class="tabel"><p>BIAYA TOTAL</p></td>
        </tr>
        <tr>
            <td  class="tabel2" valign="top"><p>&lt;?= $pesan;?&gt;</p></td>
            <td  class="tabel2" valign="top"><p>&lt;?= $pesan;?&gt;</p></td>
            <td  class="tabel2" valign="top"><p>&lt;?= $pesan;?&gt;</p></td>
            <td  class="tabel2" valign="top"><p>&lt;?= $pesan;?&gt;</p></td>
            </tr>
        </table>

but its didn't show the data
on $pesan just become array on the view not the data that ive choose...

what wrong? help please...[/quote]

You're passing an array. Try $pesan['id'], $pesan['id_user'].


how to display data to the view? - El Forum - 06-03-2009

[eluser]yudahebat[/eluser]
it works, but all data display. not data that I choose..
anything wrong?


how to display data to the view? - El Forum - 06-03-2009

[eluser]Thorpe Obazee[/eluser]
what is now the current code? this one or the other one in the other post?


how to display data to the view? - El Forum - 06-03-2009

[eluser]yudahebat[/eluser]
same like above.. just change $pesan into $pesan['id'],$pesan['id_user']etc.
like your command


how to display data to the view? - El Forum - 06-03-2009

[eluser]Thorpe Obazee[/eluser]
I suggest you just return the result from the model and pass it to the view

[quote author="yudahebat" date="1244032293"]I have a model like this
Code:
class Crud extends Model
{
    function Crud()
    {
        parent::Model();
    }
    
/* PILIH DATA */

    function cetak_data()
    {
        $this->db->query('SELECT tgl_pesan FROM reservasi WHERE tgl_pesan BETWEEN '. $this->input->post('dari_tgl').
        ' AND '.$this->input->post('sampai_tgl').'');
        $query = $this->db->get('reservasi');
        if ($query->num_rows() > 0)
        {
            return $query->result();
        }

        return FALSE;
    }
}

and controller like this

Code:
function cetak_data()
    {
        $data['css1']        = $this->css;
        $data['base']         = $this->base;
        $data['menu_adm']     = $this->menu;
        $data['myrobots']     = '&lt;meta name="robots" content="noindex,nofollow"&gt;';
        $data['mywebtitle'] = 'mrputucar | Cetak Data';
        $data['pesan'] = $this->Crud->cetak_data();

        $this->load->view('cetak_data',$data);  
    }

and my view like this
Code:
<h1 style="color:#E7541A;">Data User</h1>
        <table width="925" border="0" cellpadding="0" cellspacing="0" class="tabel" >
        <tr>
            <td width="60" class="tabel"><p>TANGGAL</p></td>
            <td width="90" class="tabel"><p>ID</p></td>
            <td width="80" class="tabel"><p>ID USER</p></td>
            <td width="150" class="tabel"><p>BIAYA TOTAL</p></td>
        </tr>
        &lt;?php foreach($pesan as $r): ?&gt;
        <tr>
            <td  class="tabel2" valign="top"><p>&lt;?= $r->id;?&gt;</p></td>
            <td  class="tabel2" valign="top"><p>&lt;?= $r->user_id;?&gt;</p></td>
            <td  class="tabel2" valign="top"><p>&lt;?= $r->user_id;?&gt;</p></td>
            <td  class="tabel2" valign="top"><p>&lt;?= $r->user_id;?&gt;</p></td>
            </tr>
        &lt;?php endforeach; ?&gt;
        </table>

but its didn't show the data
on $pesan just become array on the view not the data that ive choose...

what wrong? help please...[/quote]

look above. What you did is that you overwritten your $data variable over and over which gives you just one row.

Then you didn't loop through your array so you got 'Array' as an output.


how to display data to the view? - El Forum - 06-03-2009

[eluser]sl3dg3hamm3r[/eluser]
By the way, your sql-query is open for any random sql-injection...


how to display data to the view? - El Forum - 06-03-2009

[eluser]yudahebat[/eluser]
I suspend on this code
Code:
$this->db->query('SELECT * FROM reservasi WHERE tgl_pesan BETWEEN '. $this->input->post('dari_tgl').
        ' AND '.$this->input->post('sampai_tgl').'');

because the data that I choose didn't show
but all data show..

do you know whats wrong?


how to display data to the view? - El Forum - 06-03-2009

[eluser]qirk petrucci[/eluser]
so you already did like bargainph said ?
and now the view is ok except it shows all data in your table?


what do u have in the $_post ?

maybe u could debug it by :

Code:
echo 'SELECT * FROM reservasi WHERE tgl_pesan BETWEEN '. $this->input->post('dari_tgl'). ' AND '.$this->input->post('sampai_tgl').''


and see if there's a query error


how to display data to the view? - El Forum - 06-03-2009

[eluser]yudahebat[/eluser]
yes, I've already did like bargainph said..
and it show all data from my table

on my $_post I've an input form
which is get date from popup javascript calendar

I want to choose data from the table where the date is between $_post1 and $_post2

so it show data that I choose. from X date until X date

do understand what I mean?