Welcome Guest, Not a member yet? Register   Sign In
how to pass data to view??
#1

[eluser]yudahebat[/eluser]
what wrong with this controller??

I have a controller like this

Code:
<?php
class Test extends Controller
{
    function Test()
    {
        parent::Controller();
    }
    
    function index()
    {
            
    $data['tanggal']    = '';
        $data['email']         = '';
        $data['nama']         = '';
        $data['isi']         = '';
      
    
    $this->load->view('testpage',$data);    
        
    }
    
    
    function choose_data($id=0)
        {
        $this->db->where('id_testimonial',$id);
        $query = $this->db->get('testimonial');
        if ($query->num_rows() > 0)
        {
                $data= $query->row();
        }
        $this->index($data);
        }
    
}

and my view like this

Code:
<html>
<head>
<title>testpage</title>
</head>

<body>
<?php
echo $tanggal;
echo '<br />';
echo $email;
echo '<br />';
echo $nama;
echo '<br />';
echo $isi;
?&gt;
&lt;/body&gt;
&lt;/html&gt;

My problem is why if I call choose_data
by the URL http://localhost/ci2/test/choose_data/15
its didn't display anything??
#2

[eluser]Thorpe Obazee[/eluser]
it won't. index doesn't have anything to produce. your variables do not have anything in them.

try

function index($data) {
// your index method code
}
#3

[eluser]yudahebat[/eluser]
I try your command
it still display nothing

I think my problem is
index function sets a bunch of variables to ‘’
but if I didn't it
$tanggal
$email
$nama
$isi
will not define..

do you have any idea to fix it??
#4

[eluser]Thorpe Obazee[/eluser]
errr..

you really should read the user_guide.

that's what you need to put if you want to assign it to a variable.
Code:
$data['tanggal'] = $data->yourtablefield;
#5

[eluser]yudahebat[/eluser]
Do you mean like this
Code:
&lt;?php
class Test extends Controller
{
    function Test()
    {
        parent::Controller();
    }
    
    function index($data)
    {
        $data['tanggal']    = $data->tanggal;
        $data['email']         = $data->email;
        $data['nama']         = $data->nama;
        $data['isi']         = $data->isi;
      
        //Load the URL helper
        $this->load->view('testpage',$data);    
        
    }    
    
    function choose_data($id=0)
    {
        $this->db->where('id_testimonial',$id);
        $query = $this->db->get('testimonial');
        if ($query->num_rows() > 0)
        {
        $data ['tanggal']= $query->row();
        $data ['email']= $query->row();
        $data ['nama']= $query->row();
        $data ['isi']= $query->row();
        }
        $this->index($data);
    }
    
}

its still error

Code:
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/test.php

Line Number: 11
and 3 another error..

Im sorry Im newbie, I dont really understand about passing data
please your help..
#6

[eluser]Thorpe Obazee[/eluser]
Code:
&lt;?php
class Test extends Controller
{
    function Test()
    {
        parent::Controller();
    }
    
    function index($data)
    {
        $data['vars'] = $data;
        //Load the URL helper
        $this->load->view('testpage',$data);    
        
    }    
    
    function choose_data($id=0)
    {
        $this->db->where('id_testimonial',$id);
        $query = $this->db->get('testimonial');
        if ($query->num_rows() > 0)
        {
            $data = $query->row();
        }
        $this->index($data);
    }
}


Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;testpage&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;?php
echo $vars->tanggal;
echo '<br />';
echo $vars->email;
echo '<br />';
echo $vars->nama;
echo '<br />';
echo $vars->isi;
?&gt;
&lt;/body&gt;
&lt;/html&gt;
#7

[eluser]Colin Williams[/eluser]
I'm guessing this is a repost? yudahebat, you might want to get your footing in basic PHP scripting. As has been demonstrating, you don't seem to have a problem using the CI framework as much as you do understanding how to move variables around in a script.
#8

[eluser]yudahebat[/eluser]
I test with http://localhost/ci2/test/choose_data/15
and the result is

Fatal error: Cannot use object of type stdClass as array in C:\xampp\htdocs\CI2\system\application\controllers\test.php on line 11

what is wrong??
#9

[eluser]Thorpe Obazee[/eluser]
[quote author="Colin Williams" date="1238749673"]I'm guessing this is a repost? yudahebat, you might want to get your footing in basic PHP scripting. As has been demonstrating, you don't seem to have a problem using the CI framework as much as you do understanding how to move variables around in a script.[/quote]

yeah, I agree.

yudahebat, you probably don't have those fields in the db.
#10

[eluser]missionsix[/eluser]
you can't interexchange objects and arrays, as you are doing in this example:

Code:
&lt;?php
class Test extends Controller
{
    function Test()
    {
        parent::Controller();
    }
    
    function index($data)
    {
        $data['vars'] = $data;
        //Load the URL helper
        $this->load->view('testpage',$data);    
        
    }    
    
    function choose_data($id=0)
    {
        $this->db->where('id_testimonial',$id);
        $query = $this->db->get('testimonial');
        if ($query->num_rows() > 0)
        {
            $data = $query->row();
        }
        $this->index($data);
    }
}


You should either be returning a row as an array with row_array(), or choosing a different variable name.

Code:
&lt;?php
class Test extends Controller
{
    function Test()
    {
        parent::Controller();
    }
    
    function index($data)
    {
        $page['vars'] = $data;
        //Load the URL helper
        $this->load->view('testpage', $page);    
        
    }    
    
    function choose_data($id=0)
    {
        $this->db->where('id_testimonial',$id);
        $query = $this->db->get('testimonial');
        if ($query->num_rows() > 0)
        {

            // changed to row_array()
            $data = $query->row_array();

        }
        $this->index($data);
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB