I'm having trouble passing data to my view when using $query->row() I can only seem to get the value when using it in the controller.
Here's the model:
public function get_post($post_id) {
$this->db->select('id, date, title');
$this->db->from('posts');
$this->db->where('id', $post_id);
$this->db->limit(1);
$query = $this->db->get();
$row = $query->row();
$row->title = html_purify($row->title);
$row->date_posted_iso = date('c', $row->date);
return $row;
}
The controller:
public function post($post_id) {
$row = $this->post_model->get_post($post_id);
$this->data['post'] = $row;
$this->data['meta_title'] = $row->title;
$this->template->build('post/show_post', $this->data);
}
The view:
<?php
foreach ($post as $row):
echo $row->date;
echo $row->title;
endforeach;
?>
The line $this->data['meta_title'] = $row->title; in the controller works correctly, but I don't want to explicitly define all of those variables in the controller.
I've tried with the foreach and without and get Object of class stdClass could not be converted to string
How can I correctly echo the value in my view using $query->row()?
As you are returning a row, you don't need a foreach loop, try changing your view code to:
<?php
echo $post->date;
echo $post->title;
?>
The variable should be $post and not $row as you define post element in the data array in your controller.
Try to do like this.............
public function post($post_id)
{
$row = $this->post_model->get_post($post_id);
$model = array();
$model['post'] = $row;
$this->template->build('post/show_post', $model);
}
In view fie do like this
foreach($post as $data)
{
echo $data['title'];
}
Related
i am trying to display data using 2 tables in codeigniter, i have a products table with some products and another table with the id of the products, i did the following in controller:
public function index(){
$selectfeatured = $this->product->selectfeatured();
foreach($selectfeatured as $val){
$id=$val->pid;
$data['featured'] = $this->product->featured($id);
}
$this->load->view('home', $data);
}
in my model,
function selectfeatured()
{
$this->db->select('*');
$this->db->from('featured');
$this->db->order_by("id", "desc");
$this->db->limit(4);
$query = $this->db->get();
$result = $query->result();
return $result;
}
function featured($pid)
{
$this->db->select('*');
$this->db->where("id", $pid);
$this->db->from('product');
$query = $this->db->get();
$result = $query->result();
return $result;
}
this code only displays one product even though i have multiple products in both the tables, can anyone please tell me what is wrong in here
Make your $data['feature'] as an array:
public function index()
{
$selectfeatured = $this->product->selectfeatured();
$data['featured'] = array();
foreach ($selectfeatured as $val) {
$id = $val->pid;
$data['featured'][] = $this->product->featured($id); // add new item
}
$this->load->view('home', $data);
}
And make sure you display the featured array right way in your view, something like this:
<?php foreach ($featured as $feature) : ?>
<?php foreach ($feature as $product) : ?>
<div class="item"><img style="height:250px" src="<?php echo base_url(); ?>admin/uploads/products/<?php echo $product->pimage; ?>" alt="" class="img-responsive"></div>
</div>
<?php endforeach; ?>
<?php endforeach; ?>
The variable $data['featured'] is inside a loop and is getting new value on each iteration, and is overwritten on each iteration, that's why you are getting only one product, you need to use array_push or '+=' etc to append the other products.
I have a class names Product.php in my model directory. in my view directory, I have an index.php currently I fetching data out of my table using this function
the code works fine but I want to field $row['name'] to be called as an object. because I am not sure the way I'm doing is a semantically correct for oop programming.
public function display(){
$pizza = $this->connect()->query("SELECT * FROM pizza");
while ($row = $pizza->fetch(PDO::FETCH_ASSOC)) {
$data[]= $row;
} return $data;
}//end of method
and in my index page i write :
$blog = new Product();
$name = $blog->display();
foreach ($name as $row ) {
echo $row['name']. '<br>';
}
All you need to do is change PDO::FETCH_ASSOC to PDO::FETCH_OBJ.
Then for your index.php:
$blog = new Product();
$name = $blog->display();
foreach ($name as $row ) {
echo $row->name . '<br>';
}
Here is a really good article to read for PDO.
PDO Tutotrial
Good I'm starting with php and codeigniter, I have the following problem I'm running an array from my model, which is the result of a query, how can I read one by one my records in the controller?
function getCustomers() {
$sql = "SELECT * FROM customers";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$i = 0;
foreach($query->result() as $row) {
$img[$i]['id'] = $row->id;
$img[$i]['name'] = $row->name;
$img[$i]['Location'] = $row->Location;
$img[$i]['telephone'] = $row->telephone;
$i++;
}
return $img;
}
}
You can return array to the controller by loading the model into the controller like this.
$this->load->model('yourmodel');
$array = $this->yourmodel->yourmodelsmethod();
First you have to return this array to your controller like
$this->load->model('yourmodel');
$array['test'] = $this->yourmodel->yourmodelsmethod();
foreach($test as $key=>$value)
{
echo $value;
}
By doing this you can easily print your data from model in controller
I tried to call up data from the model , but when running the results in view the word " array" . is there who can help me ? i am using codeigniter
Controller
$data=array('pengunjung' => $this->mcrud->jumlah_visitor(),
'isi' =>'user/monitoring');
$this->load->view('layout/wrapper', $data);
Model
function jumlah_visitor() {
$date = date("Ymd");
$this->db->where('date',$date);
$this->db->group_by(array('ip'));
$ambil= $this->db->get('tbcounter');
if ($ambil->num_rows() > 0) {
foreach ($ambil->result_array() as $data) {
$hasil[] = $data;
}
return $hasil;
}
}
View
<div class="pull-left">Hari Ini : </div>
<div class="pull-right number"> <?php echo $pengunjung; ?></div>
Result
Hari ini : array
First you check the return value of $this->mcrud->jumlah_visitor() after that you print the value. If it's an array you have to use loop for that.
<?php echo $pengunjung; ?>
to
<?php print_r($pengunjung); ?>
$pengunjung variable is array not string variable
Yes of-course...
in your Model you are returning an array
foreach ($ambil->result_array() as $data) {
$hasil[] = $data;
}
return $hasil;
The same you are trying to catch in your controller $this->mcrud->jumlah_visitor(),
return your index-key just like $data->index_key
Try printing array as pengunjung is holding an array, not a simple variable.
echo "<pre>";
print_r($this->mcrud->jumlah_visitor);
echo "</pre>";
You will get complete array info.
You can't echo an array. you should use a loop to display result of $pengunjung.
In View just use
foreach( $pengunjung as $key => $value)
{
//echo $value;
}
Rewrite your model function like this
function jumlah_visitor() {
$date = date("Ymd");
$this->db->where('date',$date);
$this->db->group_by(array('ip'));
$ambil= $this->db->get('tbcounter');
$data = $ambil->result_array();
if (data->num_rows() > 0) {
foreach ($data as $data1) {
$hasil[] = $data1;
}
return $hasil;
}
}
Here is my model - I want to assign the rows and echo out on the view page e.g <?php echo $product?>
class Model_products extends CI_Model {
function printer()
{
$id = $this->uri->segment(3, 0);
$q = $this->db->get_where('printer', array('id' => $id));
if ($q->num_rows == 1)
{
foreach ($q->result() as $row)
{
echo $row->name; # THIS WORKS
$data['product'] = $row->name; # THIS DOES NOT WORK
$data['desc'] = $row->description;
$data['size'] = $row->size;
$data['options'] = $row->options;
$data['more'] = $row->more_info;
}
}
}
}
Here's my controller
public function products()
{
$this->load->model('Model_products');
$this->Model_products->printer();
$this->load->view('products', $data);
$this->load->view('pricing');
}
My view is
<h1>
<?php echo $product; ?>
</h1>
<p>
<?php echo $desc; ?>
</p>
<h3>
Size
</h3>
<p>
<?php echo $size; ?>
</p>
How do I properly pass my model array to my controller so that I can echo out on my view?
Simply return $data to controller
class Model_products extends CI_Model {
function printer()
{
$data = array();
$id = $this->uri->segment(3, 0);
$q = $this->db->get_where('printer', array('id' => $id));
if ($q->num_rows == 1)
{
foreach ($q->result() as $row)
{
// echo $row->name; # THIS WORKS
$data['product'] = $row->name; # THIS DOES NOT WORK
$data['desc'] = $row->description;
$data['size'] = $row->size;
$data['options'] = $row->options;
$data['more'] = $row->more_info;
}
}
return $data;
}
}
No win Controller
public function products()
{
$this->load->model('Model_products');
$data['products'] = $this->Model_products->printer();
$this->load->view('products', $data);
$this->load->view('pricing');
}
And in view
<?
echo '<pre>';
print_r($products);
?>
EDIT Also correct your model code.
foreach ($q->result() as $row)
{
echo $row->name; # THIS WORKS
$data[]['product'] = $row->name; # THIS DOES NOT WORK
$data[]['desc'] = $row->description;
$data[]['size'] = $row->size;
$data[]['options'] = $row->options;
$data[]['more'] = $row->more_info;
}
If you are not using multidimensional array it will overwrite the last record.
You need to return the data from the model back to the controller and then from there, pass the data to the view.
In your model function, place a return statement at the end.
return $data;
Then in your controller, assign the returned value from the model to a variable.
$data = $this->Model_products->printer();
Those 2 steps should fix your problem.
Where your return statement
if ($q->num_rows == 1)
{
foreach ($q->result() as $row)
{
echo $row->name; # THIS WORKS
$data['product'] = $row->name; # THIS DOES NOT WORK
$data['desc'] = $row->description;
$data['size'] = $row->size;
$data['options'] = $row->options;
$data['more'] = $row->more_info;
}
return $data;
}
and/or in your controller
public function products()
{
$this->load->model('Model_products');
$data = $this->Model_products->printer();
$this->load->view('products', $data);
$this->load->view('pricing');
}
I used #raheel's code and it works. But I would just change a bit:
$i=0;
foreach ($q->result() as $row)
{
echo $row->name; # THIS WORKS
$data[$i]['product'] = $row->name; # THIS DOES NOT WORK
$data[$i]['desc'] = $row->description;
$data[$i]['size'] = $row->size;
$data[$i]['options'] = $row->options;
$data[$i]['more'] = $row->more_info;
}
This will help extracting the data