Cannot print data into "view"
I开发者_C百科 have a page that I would like to show the data from the database.
I can print_r($sale)
and it shows the data that I am after - $sale is set in the controller but I cannot seem to do <?php $sale['name'] ?>
it shows nothing.
Print_r:
Array ( [0] => stdClass Object ( [id] => 48 [name] => Jess McKenzie [location] => Auckland [bedrooms] => 5 [bathrooms] => 1 [condition] => Fair [description] =>
hii
[price] => 30.00000 [imagename] => purple.jpg [thumbname] => purple_thumb.jpg ) [1] => stdClass Object ( [id] => 49 [name] => jzmwebdevelopment [location] => Auckland [bedrooms] => 15 [bathrooms] => 4 [condition] => OK [description] =>
zebra
[price] => 25.00000 [imagename] => Zebra.jpg [thumbname] => Zebra_thumb.jpg ) )
Model:
function getSalesContent($id = NULL) {
$this->db->where('id', $id);
$query = $this->db->get('sales', 1);
if($query->num_rows() > 0) {
$row = $query->result_array();
return $row;
}else{
return FALSE;
} # End IF
} # End getSalesContent
It's returning an array of objects.
To show the first element returned you would use
$sale[0]->name;
To cycle through all of the values you could use a foreach loop
foreach($sale as $s){
print $s->name;
}
$query->result_array()
returns an array of arrays, you would use $sale['name']
in a foreach loop.
$query->result()
returns an array of stdclass objects, you would use $sale->name
in a foreach loop.
I cannot seem to do $sale->name it shows nothing.
Open your index.php
file and add error_reporting(E_ALL)
to the top. If you were error reporting you'd be able to see your mistakes with helpful error messages telling you exactly what went wrong. Just set it to 0
for when you go live.
I have tried
<?php $sale['name'] ?>
and get nothing
You need an echo
statement: <?php echo $sale['name'] ?>
If $sale
is the output you posted, <?php echo $sale[0]->name ?>
should print Jess McKenzie
精彩评论