Codeigniter model foreach pass array to controller -> to view - php

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

Related

Navigate an array from controller in Codeigniter

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

insert foreach controllers on CodeIgniter

I want to try to add foreach on controllers , because before I show you foreach on views
This my Controllers
$data['report_data'] = $this->hasil_m->get_data($nomor);
//create foreach here, from data $data['report_data']
$data=array('title' =>'KOPKAR - Pelanggan',
'isi' =>'admin/hasil'
);
$this->load->view('dashboard/wrapper',$data);
This my Models
$this->db->select('*');
$this->db->from('tb_produk as pro');
$this->db->join('tb_reseller as res', 'pro.reseller_produk = res.nomor_reseller');
$this->db->join('tb_pelanggan as pel', 'pro.id_barcode = pel.id_barcode');
$this->db->select_sum('jumlah');
$this->db->group_by('res.nomor_reseller');
$ambil = $this->db->get('');
if ($ambil->num_rows() > 0) {
foreach ($ambil->result() as $data) {
$hasil[] = $data;
}
return $hasil;
}
This my Views
Instead of doing :
foreach ($ambil->result() as $data) {
$hasil[] = $data;
}
return $hasil;
just
return $ambil->result_array();

Array printed correctly in controller,but not printed in the view page

print_r($listb) is working correctly in controller. but when we are passing this array to view page, its not working in the view page. Controller and view page and Model function is given below:
Controller
public function index() {
$this->load->helper('url');
$this->load->view('user/templates/header');
$data['banner'] = $this->banner_model->viewbanner();
$this->load->view('user/templates/sidebar', $data);
$data1['list1'] = $this->featured_model->viewfeaturedlist(1);
$listb = array();
foreach ($data1['list1'] as $list1) {
$list = explode(',', $list1->fet_list);
$type = $list1->fet_type;
foreach ($list as $pid) {
if ($type == 1) {
$listb['hhh'] = $this->featured_model->viewb2bproduct($pid);
//print_r($listb);
}
}
}
$this->load->view('user/templates/featured', $listb);
exit;
$this->load->view('user/templates/footer');
}
}`enter code here`
View
<?php
print_r($hhh);
?>
Model
public function viewb2bproduct($id) {
$this->db->select('*');
$this->db->from('jil_products');
$this->db->where('prd_id', $id);
$query = $this->db->get();
return $query->result();
}
In your controller Change this Line :
$listb['hhh'] = $this->featured_model->viewb2bproduct($pid);
to
$listb['hhh'][] = $this->featured_model->viewb2bproduct($pid);
In view :
<?php
foreach ($fff as $product){
echo $products[0]['prd_id']."<br>";
echo $products[0]['prd_name']."<br>"; //and so on
}
Your $listb['hhh'] is in foreach loop and you are overriding it each time for every element of $list. and in controller you are printing it inside the foreach loop thats why it is giving output but not in your view.
In model
public function viewb2bproduct($id) {
$this->db->select('*');
$this->db->from('jil_products');
$this->db->where('prd_id', $id);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
In controller
public function index()
{
$this->load->helper('url');
$data['banner'] = $this->banner_model->viewbanner();
$data1['list1'] = $this->featured_model->viewfeaturedlist(1);
$this->load->view('user/templates/sidebar', $data);
$this->load->view('user/templates/header');
$this->load->view('user/templates/featured', $listb);
$this->load->view('user/templates/footer');
}
}
and in view
<?php
foreach ($list1 as $new_list1)
{
echo "<p>".$new_list1['table_field']."</p>"
}
I have no idea on this part
$listb = array();
foreach ($data1['list1'] as $list1)
{
$list = explode(',', $list1->fet_list);
$type = $list1->fet_type;
foreach ($list as $pid) {
if ($type == 1) {
$listb['hhh'] = $this->featured_model->viewb2bproduct($pid);
//print_r($listb);
}
}
}
$hhh=array();
foreach ($data1['list1'] as $list1) {
$list = explode(',', $list1->fet_list);
$type = $list1->fet_type;
foreach ($list as $pid) {
if ($type == 1) {
$hhh[] = $this->featured_model->viewb2bproduct($pid);
//print_r($listb);
}
}
}
$listb['hhh']=$hhh;
$this->load->view('user/templates/featured', $listb);

yii framework. cats tree functions aren`t working in the model file

public function getCats($model){
$levels = array();
$tree = array();
$cur = array();
foreach($model as $rows){
$cur = &$levels[$rows['id']];
$cur['parent_id'] = $rows['parent_id'];
$cur['title'] = $rows['title'];
if($rows['parent_id'] == 0){
$tree[$rows['id']] = &$cur;
}
else{
$levels[$rows['parent_id']]['children'][$rows['id']] = &$cur;
}
}
return $tree;
}
public function getTree($arr){
echo '<ul>';
foreach($arr as $k=>$v){
echo '<li>';
echo ''.$v['title'].'';
if(!empty($v['children'])){
echo getTree($v['children']);
}
echo '</li>';
}
echo '</ul>';
}
public function allCats($pos) {
$model = Category::model()->findallBySql('SELECT id, parent_id, title FROM {{category}} WHERE position="'.$pos.'"');
$cats = getCats($model);
echo getTree($cats);
}
I wonder, why these functions don`t work in model file(inside class)?
If i use them inside controllers, they work fine, and if i paste them inside model(class) file, they stop working(as if they are becoming invisible). The last function causing the above two functions.
You are calling getCats($model) which would be a global function. But it's defined as a class function/method. Call it $this->getCats($model) and $this->getTree($cats) and it should work.

How to use database object in view

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'];
}

Categories