Welcome Guest, Not a member yet? Register   Sign In
How to pass an array data type with objects as its value, then "get" that objects in the View.
#1

[eluser]raymondS[/eluser]
class Xxx extends Controller
{
public function __construct()
{
parent::Controller();
}

public function index()
{
$config['hostname'] = "localhost";
$config['username'] = "root";
$config['password'] = "";
$config['database'] = "xxx";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";

$this->load->database($config);

//this will get 2 rows of object
$rows_of_object = $this->db->query("SELECT * FROM content");

//this will get an array with those objects as it's value
$array_of_object = $rows_of_object->result();

$data;

for($i=0;$i < ( $rows_of_object->num_rows() );$i++)
{
$data[$i] = $array_of_object[$i];
}

//It works here
print_r ($data[1]->content_title);

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

}

}


// The View, Doesn't Work at all Big Grin

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;

&lt;title&gt;&lt;?php echo $content_title ?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

<div id="container">
&lt;?php echo $content_value; ?&gt;
</div>

<hr />

&lt;/body&gt;
&lt;/html&gt;












HELP!!!!
#2

[eluser]raymondS[/eluser]
The manual says

"Note: If you use an object, the class variables will be turned into array elements."

Anyone can give a simple example ?

And one example of passing to the View an array with objects as it's values.
#3

[eluser]raymondS[/eluser]
$data['title'] = "My Real Title";
$data['heading'] = "My Real Heading";

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

//The View

$title // "My Real Title".

It works, now what if:



------------------------------------------
//Pseudo-code
//One object has 3 variables, content_value, content_title, content_author_id
$data[] = array(object, object);

$data[0] = object;
$data[1] = object;

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

/* My question is, how to "get" one object, i can't use the name because it's not an associative array, it's a numbered array. I can't use $nameOfTheIndex ( $content_value ). */

/* HELP */
#4

[eluser]WanWizard[/eluser]
Most obvious answer: you're defining the data array manually, so why create a problem using numbers as key?

What the manual means is that if $data itself is an object, it will be passed to the view as an array. So $data->field will become $field in the view ($data converted to an array).
#5

[eluser]raymondS[/eluser]
Okay, after tinkering with CodeIgniter, and after reading my question, it is too long.

This is my question, simplified:

$dataX[0];
$dataX[1];

print_r($dataX[0]);
/*
stdClass Object
(
[content_id] => 1
[content_title] => Cara baca asp
[content_value] => Bla Bla Bla
*/

print_r($dataX[1]);
/*
stdClass Object
(
[content_id] => 2
[content_title] => Cara baca php
[content_value] => Bla Bla Bla
*/


$this->load->view('indonesia_suka_sibuya', $dataX);







The question is, how to "GET" that data inside $dataX in the View. Admin help me. Big Grin
#6

[eluser]raymondS[/eluser]
[quote author="WanWizard" date="1284432512"]Most obvious answer: you're defining the data array manually, so why create a problem using numbers as key?

What the manual means is that if $data itself is an object, it will be passed to the view as an array. So $data->field will become $field in the view ($data converted to an array).[/quote]

Hi Wan, nice to meet you,

passing associative array to View and passing one object to View, i can do it.

But passing a numbered array to View with objects as it's values, i can't do it.
#7

[eluser]raymondS[/eluser]
If i used

$dataX[0];
$dataX[1];

print_r($dataX[0]);
/*
stdClass Object
(
[content_id] => 1
[content_title] => Cara baca asp
[content_value] => Bla Bla Bla
*/

print_r($dataX[1]);
/*
stdClass Object
(
[content_id] => 2
[content_title] => Cara baca php
[content_value] => Bla Bla Bla
*/


$this->load->view(‘indonesia_suka_sibuya’, $dataX[0];

//The View

echo $content_id; // 1, it works.


//BUT if i pass an numbered array with objects as it's values, i can't reference that data, i don't know how.

$dataX[0];
$dataX[1];

print_r($dataX[0]);
/*
stdClass Object
(
[content_id] => 1
[content_title] => Cara baca asp
[content_value] => Bla Bla Bla
*/

print_r($dataX[1]);
/*
stdClass Object
(
[content_id] => 2
[content_title] => Cara baca php
[content_value] => Bla Bla Bla
*/


$this->load->view(‘indonesia_suka_sibuya’, $dataX);

//The View

echo $????????? // How To get the value of $dataX[0]->content_id or $dataX[1]->content_id
#8

[eluser]WanWizard[/eluser]
Don't pass it like that.

Usually there is no need to access individual array elements (for example as a result of a query), but you will use a foreach to loop through the array.
In this case, you use
Code:
// in your controller
$data['datax'] = $dataX;


Code:
&lt;?php
//in your view
foreach ($datax as $obj)
{
    // do something with the object
}
?&gt;

If you need the individual array element, you know that up front (because you have to code the numeric index in your view), so you can assign it to an array element in your controller
Code:
// need index 1 in my view
$data['my_datax'] = $dataX[1];

Point is that the array passed on to the view HAS to be an associative array, as the array keys will become the variable names in the view.
#9

[eluser]raymondS[/eluser]
[quote author="WanWizard" date="1284433965"]Don't pass it like that.

Usually there is no need to access individual array elements (for example as a result of a query), but you will use a foreach to loop through the array.
In this case, you use
Code:
// in your controller
$data['datax'] = $dataX;


Code:
&lt;?php
//in your view
foreach ($datax as $obj)
{
    // do something with the object
}
?&gt;

If you need the individual array element, you know that up front (because you have to code the numeric index in your view), so you can assign it to an array element in your controller
Code:
// need index 1 in my view
$data['my_datax'] = $dataX[1];

Point is that the array passed on to the view HAS to be an associative array, as the array keys will become the variable names in the view.[/quote]

Ah yes, put it inside an associative array, then passed it to the View. I should have tried that, Thanks Wan for your answers, it helped me a lot, i would have spend another 1 or 5 hours figuring this out. Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB