insert foreach controllers on CodeIgniter - php

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();

Related

Limit query is not working in codeigniter when using foreach loop

I am trying to limit the result for pagination in foreach loop with the following query but instead of limiting I am getting all the results with the following query.
public function get_tags($limit, $start, $tag_id)
{
$snippetstagdata = $this->getDataOneColumn("snippets_tags","tag_id",$tag_id);
foreach ($snippetstagdata as $getSnippet) {
$snippet_id = $getSnippet->snippet_id;
$this->db->where("id", $snippet_id);
$this->db->select('*');
$this->db->from('snippets');
$this->db->limit($limit, $start);
$query = $this->db->get();
$result[] = $query->result();
}
$this->db->save_queries = false;
return $result;
}
Whereas getDataOneColumn
public function getDataOneColumn($table, $col1_name, $col1_value)
{
$this->db->where("$col1_name", $col1_value);
$query = $this->db->get("$table");
$result = $query->result();
$this->db->save_queries = false;
return $result;
}
Updated
I hope this will help you
public function get_tags($params = array(), $tag_id)
{ $result = array();
$snippetstagdata = $this->getDataOneColumn("snippets_tags","tag_id",$tag_id);
foreach ($snippetstagdata as $data) {
$this->db->where('id',$data['snippet_id']);
if(isset($params) && !empty($params))
{
$this->db->limit($params['limit'], $params['offset']);
}
$result[$data['snippet_id']] = $this->db->get('snippets')->result_array();
}
return $result;
}
and
public function getDataOneColumn($table, $col1_name, $col1_value)
{
return $this->db->get_where($table,array($col1_name=>$col1_value))->result_array();
}
Your approach to query your database with the help of the foreach loop is wrong. The correct approach would be using a join in the query:
from your function getDataOneColumn() you are only using the snippet_id values, which you can use to create the join:
public function getDataOneColumn($table, $col1_name, $col1_value, $limit, $start)
{
$this->db->select("t1.*,t2.*")
->where("t1.$col1_name", $col1_value)
->join("snippets t2","t1.snippet_id=t2.id")
->limit($limit, $start);
$query = $this->db->get("$table t1");
$result = $query->result();
return $result;
}
note: in your example $tag_id=$col1_value, so use whatever variable name fits better.
Your second function get_tags() is then not necessary at all.

Yii2 Update Parent Child

I have a structure table like this
I want to change record data field goid = 1, but I as you can see my code bellow, there's limit looping. Is there a better way to do it?
public function actionShareGoid($folder)
{
$one = DokumenFolder::findOne($folder);
$one->goid = '1';
$one->update(false);
$models_1 = DokumenFolder::find()->where(['parent_id'=>$one->id])->all();
foreach ($models_1 as $key_1 => $model_1) {
$model_1->goid = '1';
$model_1->update(false);
$models_2 = DokumenFolder::find()->where(['parent_id'=>$model_1->id])->all();
foreach ($models_2 as $key_2 => $model_2) {
$model_2->goid = '1';
$model_2->update(false);
$models_3 = DokumenFolder::find()->where(['parent_id'=>$model_2->id])->all();
foreach ($models_3 as $key_3 => $model_3) {
$model_3->goid = '1';
$model_3->update(false);
$models_4 = DokumenFolder::find()->where(['parent_id'=>$model_3->id])->all();
foreach ($models_4 as $key_4 => $model_4) {
$model_4->goid = '1';
$model_4->update(false);
}
}
}
}
return $this->redirect(Yii::$app->request->referrer);
}
Thanks to someone, I got the answer. ^_^
So here it is...
public function folderParent($id)
{
$models = DokumenFolder::find()->where(['parent_id'=>$id])->all();
foreach ($models as $key => $model) {
$model->goid = '1';
$model->update(false);
}
}
public function actionShareGoid($id)
{
$models = DokumenFolder::find()->where(['parent_id'=>$id])->all();
foreach ($models as $key => $model) {
$this->folderParent($model->id);
}
return $this->redirect(Yii::$app->request->referrer);
}

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);

arrange category and subcategory in php and Codeigniter

Here I have my database table and my table name is category where I store my data category and subcategory in category column
i want to fetch this data like this format in ul li for this code i have trying to arrange but i am not fetch data perfectly like this please help me to arrange my code this format.
You can try like this
//to select categories
$this ->db-> select();
$this->db->from('category');
$this->db->where('pid',0);
$query = $this->db->get();
$categories = array();
$i=0;
foreach ($query->result_array() as $row) {
//put all category names to $categories array
$categories[$i] =$row['category'];
$i++;
}
//to select relevant sub categories
$sub_categories=array();
foreach ($categories as $category) {
$this ->db-> select();
$this->db->from('category');
$this->db->like('category', $category, 'after');
$query = $this->db->get();
$j=0;
foreach ($query->result_array() as $row) {
//put all sub categories names to $sub_categories array
$sub_categories[$category][$j] =$row['category'];
$j++;
}
}
$return_data['categories']=$categories;
$return_data['sub_categories']=$sub_categories;
return $return_data;
This code is useful for get nth level of category,subcategory array
controller:
$this->load->model('getmenu_model');
function get_menu() {
$ci = & get_instance();
$ci->load->model("getmenu_model");
$menu['menu'] = $ci->getmenu_model->get_menu();
$echo = echoMenu($menu['menu']);
//$echo.= "<li>Loose Diamonds</li>";
$menu['print_menu']=$echo;
print_r($menu);
//return $menu;
}
function echoMenu($arr) {
$ci = & get_instance();
$echo = "";
//$echo.="<ul>";
foreach ($arr as $subArr) {
if (!empty($subArr['sub_menu'])) {
$echo.= "<li>";
$echo.= "" . $subArr['name'] . "";
if ($subArr['sub_menu']) {
$echo.= "<ul>";
$echo .=echoMenu($subArr['sub_menu']);
$echo.= "</ul>";
}
$echo.= "</li>";
} else {
$echo.= "<li>" . $subArr['name'] . "</li>";
}
}
//$echo.= "</ul>";
return $echo;
}
model:
public function get_menu() {
$this->db->select('id,name,parent_id');
$menu = $this->db->get('category')->result_array();
$data=$this->menu_child($menu);
//print_r($data);
return $data;
}
function menu_child($menu, $parent = NULL) {
// echo $parent."---";
$main_menu = array_filter($menu, function($a)use($parent) {
return $a['parent_id'] == $parent;
});
// print_r($main_menu);
if ($main_menu) {
foreach ($main_menu as $key => $value) {
$main_menu[$key]['sub_menu'] = $this->menu_child($menu, $value['id']);
}
}
// print_r($main_menu);
return $main_menu;
}

What is wrong with this PHP code?

I have two database tables, one for allowances and one for deductions. I want to calculate net salaries.
I'm using CodeIgniter. Here's my current code:
function get_allowances($eid)
{
$this->db->from('allowances');
$this->db->where('eid',$eid);
$query = $this->db->get();
if($query->num_rows()==1)
{
return $query->row();
}
else
{
//Get empty base parent object, as $item_id is NOT an item
$salary_obj=new stdClass();
//Get all the fields from items table
$fields = $this->db->list_fields('allowances');
foreach ($fields as $field)
{
$salary_obj->$field='';
}
return $salary_obj;
}
}
function get_deductions($eid)
{
$this->db->from('deductions');
$this->db->where('eid',$eid);
$query = $this->db->get();
if($query->num_rows()==1)
{
return $query->row();
}
else
{
//Get empty base parent object, as $item_id is NOT an item
$salary_obj=new stdClass();
//Get all the fields from items table
$fields = $this->db->list_fields('deductions');
foreach ($fields as $field)
{
$salary_obj->$field='';
}
return $salary_obj;
}
}
and in controller,
function net_salary($eid)
{
$allownces[] = $this->Salary->get_allowances($eid);
$deductions[] = $this->Salary->get_deductions($eid);
return $net_salary = array_sum($allownces) - array_sum($deductions);
}
My net_salary() function gives me a result of 0. What am I doing wrong, and how can I fix it?
Your models with plural names are only going to return a single object.
so what you are ending up with is...
Array
(
[0] => allowance_object
)
and
Array
(
[0] => deduction_object
)
While we really need the schema of your database try this (and make same edits for deductions)...
function get_allowances($eid)
{
$this->db->from('allowances');
$this->db->where('eid',$eid);
$query = $this->db->get();
if($query->num_rows()==1)
{
return $query->row_array(); //<--- return an Array
}
else
{
// make an array instead of object
$salary_obj = array();
//Get all the fields from items table
$fields = $this->db->list_fields('allowances');
foreach ($fields as $field)
{
$salary_array[$field] = 0; //<---- add array keys and set to integer 0 instead of empty string.
}
return $salary_array;
}
}
then in your net_salary function
function net_salary($eid)
{
$allownce = $this->Salary->get_allowances($eid);
$deduction = $this->Salary->get_deductions($eid);
return array_sum($allownce) - array_sum($deduction);
}
Try something like this:
function get_values($eid, $table_name)
{
$this->db->where('eid',$eid);
$query = $this->db->get($table_name);
$salary_obj = $query->result();
$values = array();
foreach($salary_obj as $row){
$values[] = $row->value_column_name;
}
return $values;
}
where value_column_name is the name of the table column (filedname) where the desired value stands.
call in controller:
function net_salary($eid)
{
$allownces = $this->Salary->get_values($eid, 'allowances');
$deductions = $this->Salary->get_values($eid, 'deductions');
return $net_salary = array_sum($allownces) - array_sum($deductions);
}

Categories