Codeigniter: How to declare a global variable for a recursive function, and how to return it? - codeigniter

I want to use recursion in a model in Codeigniter, and am baffled as to where I should declare the global variable to be used in the recursive function.
I'm trying to show a catalog of products, ordered by their geographical classification. The recursion is needed because the geographical database is a hierarchy tree, where a place can have sons, grandsons, great-grandsons etc (for example: London is the son of England, which is the son of Britain, which is the son of Europe).
At the end of the recursion, I want to return the variable $arrCatalog, which will hold all the products. So my other question is how to return that variable in the end?
Here is my code (not tested yet, because I didn't know how to handle the global variable):
class Catalogs_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
function getCatalog($place_id, $prod_type)
{
$sql =
"SELECT id, heb_name, type FROM places p
INNER JOIN places_relations r ON p.id = r.son
WHERE father = ?
ORDER BY sort_name";
$query = $this->db->query($sql, $place_id);
if($query->num_rows() > 0) {
foreach ($query->result() as $place) {
$sql2 =
"SELECT code, heb_title, price, place_id FROM products
INNER JOIN products_places ON prod_code=code
WHERE place_id = ? AND prod_type = ?";
$query2 = $this->db->query($sql, $place_id, $prod_type);
if($query2->num_rows() > 0) {
foreach ($query2->result() as $product) {
$arrCatalog[] = $product; // $arrCatalog is the global variable I don't know where to declare and how to return.
}
$this->getCatalog($place['id'], $prod_type);
}
}
}
}
}

Pass your variable to the current object ($this), then you can pass it around your model or hand it to a controller.
class Catalogs_model extends CI_Model {
public $arrCatalog = array();
public function __construct() {
parent::__construct();
}
function getCatalog($place_id, $prod_type) {
$sql = "SELECT ...";
$query = $this->db->query($sql, $place_id);
if($query->num_rows() > 0) {
foreach ($query->result() as $place) {
$sql2 = "...";
$query2 = $this->db->query($sql, $place_id, $prod_type);
if($query2->num_rows() > 0) {
foreach ($query2->result() as $product) {
$this->arrCatalog[] = $product;
}
$this->getCatalog($place['id'], $prod_type);
}
}
}
}
Your controller:
$this->catalogs_model->getCatalog(2, 'hebBook');
$data['data_rows'] = $this->catalogs_model->arrCatalog;
Testing, alter your model like so ....
class Catalogs_model extends CI_Model {
public $arrCatalog = array();
// add these variables for testing ....
private $greeting = 'Hello'; // notice this is private? You cannot access this from your controller but you can from anywhere within the Catalogs_model class.
public $output = ''; // public
public $string = null; // public
// your existing code ...
function hello ()
{
$this->output = $this->greeting;
}
function hello_string ()
{
if($this->string)
{
$this->output = $this->greeting.' '.$string;
}
}
} // end class
Now test in your controller
$this->catalogs_model->hello(); // runs the hello function which sets the model->output
echo $this->catalogs_model->output; // echos 'Hello'
echo '<br />'; // just so the output stands apart in your browser.
$this->catalogs_model->string = 'World!';
$this->catalogs_model->hello_string();
echo $this->catalogs_model->output; // echos Hello World!

Related

model and controller query to merge timein and timeout with single row in codeigniter

Please teach me how can I achieve this(Codeigniter framework)
Please see images that I want to achieve thank you.
and I'm also new to codeigniter and want to understand the answer.
enter image description here
Here is my database:
enter image description here
model
public function get_attendance_employees() {
$sql = 'SELECT * FROM tbl_employee';
//$binds = array(1);
$query = $this->db->query($sql);
return $query;
}
public function attendance_first_in_check($userid,$attendance_date) {
$sql = "SELECT * FROM tbl_employee_time_log as time_log WHERE userid = ? and substring(time_log.time,1,10) = ? and type = 'timein' limit 1";
$binds = array($userid,$attendance_date);
$query = $this->db->query($sql,$binds);
return $query;
}
public function attendance_first_in($userid,$attendance_date) {
$sql = "SELECT * FROM tbl_employee_time_log as time_log WHERE userid = ? and substring(time_log.time,1,10) = ? and type = 'timein'";
$binds = array($userid,$attendance_date);
$query = $this->db->query($sql, $binds);
return $query->result();
}
public function attendance_first_out_check($userid,$attendance_date) {
$sql = "SELECT * FROM tbl_employee_time_log as time_log WHERE userid = ? and substring(time_log.time,1,10) = ? and type = 'timeout' order by id desc limit 1";
$binds = array($userid,$attendance_date);
$query = $this->db->query($sql, $binds);
return $query;
}
public function attendance_first_out($userid,$attendance_date) {
$sql = "SELECT * FROM tbl_employee_time_log as time_log WHERE userid = ? and substring(time_log.time,1,10) = ? and type = 'timeout' order by id desc limit 1";
$binds = array($userid,$attendance_date);
$query = $this->db->query($sql, $binds);
return $query->result();
}
controller
public function attendance_bio()
{
$session = $this->session->userdata('username');
if(!empty($session)){
$this->load->view("admin/timesheet/attendance_bio", $data);
} else {
redirect('admin/');
}
// Datatables Variables
$draw = intval($this->input->get("draw"));
$start = intval($this->input->get("start"));
$length = intval($this->input->get("length"));
$attendance_date2 = $this->input->get("attendance_date"); //string
//$ref_location_id = $this->input->get("location_id");
//$convert_atten = strtotime($attendance_date2); //int value
//$attendance_date = $convert_atten->format('m-d-Y');
$attendance_date = date("m-d-Y",strtotime($attendance_date2));
//$timein = $this->Bio_model->get_attendance_date();
//$timein = $this->Bio_model->get_attendance_timein($get_day);
//$timein = $this->Bio_model->get_all();
$employee = $this->Bio_model->get_attendance_employees();
$data = array();
//var_dump($attendance_date);
foreach($employee->result() as $r){
$check = $this->Bio_model->attendance_first_in_check($r->userid,$attendance_date);
if($check->num_rows() > 0){
// check clock in time
$first_in = $this->Bio_model->attendance_first_in($r->userid,$attendance_date);
//$first_in2 = $first_in[0]->time->format('m-d-Y');
// clock in
$clock_in = new DateTime(strtotime($first_in[0]->time));
//$clock_in2 = $clock_in->format('H:i');
$clock_in2 = date_format($clock_in->time, 'H:i');
//$clock_in2 = var_dump($clock_in);
} else {
$clock_in2 = '-';
$clock_in = '-';
}
$check_out = $this->Bio_model->attendance_first_out_check($r->userid,$attendance_date);
if($check_out->num_rows() == 1){
$first_out = $this->Bio_model->attendance_first_out($r->userid,$attendance_date);
//$first_out2 = $firs
$clock_out = new DateTime(strtotime($first_out[0]->time));
if ($first_out[0]->time!='') {
//$clock_out2 = $clock_out->format('H:i');
//$clock_out2 = date('H:i',$clock_out);
$clock_out2 = date_format($clock_out->time, 'H:i');
} else {
$clock_out2 = '-';
}
}else {
$clock_out2 = '-';
$clock_out = '-';
}
$data[] = array(
$r->userid,
$attendance_date,
$clock_in2,
$clock_out2,
);
}
$output = array(
"draw" => $draw,
"recordsTotal" => $employee->num_rows(),
"recordsFiltered" => $employee->num_rows(),
"data" => $data
);
echo json_encode($output);
exit();
}
Thank you for providing more information on the topic and the code you have attempted so far. I have supplied an algorithm which I believe will work for you.
Contoller:
<?php namespace App\Controllers;
use App\Models\EmployeeTimeLog;
class Home extends BaseController
{
public function index($employeeId)
{
// get all between dates
$emplyeeModel = new EmployeeTimeLog();
// get one week before today - for example
$startDate = date("Y-m-d", strtotime("-1 week"));
$endDate = date("Y-m-d");
// get clock-in and clock-out for employee between dates
$timeIn = $emplyeeModel->getTimeIn($employeeId, $startDate, $endDate)->getResult();
$timeOut = $emplyeeModel->getTimeOut($employeeId, $startDate, $endDate)->getResult();
$timeInIndex = 0;
$timeOutIndex = 0;
$output = [];
$nextDate = $startDate;
// loop through all the days between start and end date
while($nextDate < $endDate){
// get the lowest "timein" datetime which is the same date as the search date $nextDate
$nextTimeIn = NULL;
while($timeInIndex < count($timeIn) && date("Y-m-d", strtotime($timeIn[$timeInIndex]->Time)) == $nextDate){
if(is_null($nextTimeIn)){
$nextTimeIn = $timeIn[$timeInIndex];
}
$timeInIndex ++;
}
// get the highest "timeout" datetime which is the same day as the search date $nextDate
$nextTimeOut = NULL;
while($timeOutIndex < count($timeOut) && date("Y-m-d", strtotime($timeOut[$timeOutIndex]->Time)) == $nextDate){
$nextTimeOut = $timeOut[$timeOutIndex];
$timeOutIndex ++;
}
// enter into a 2 dimentional array with
// index 0 = timein
// index 1 = timeout
$output[] = [$nextTimeIn, $nextTimeOut];
// get the next date
$nextDate = date('Y-m-d', strtotime($nextDate. ' + 1 days'));
}
// return view with data
return view('welcome_message', ["output" => $output]);
}
}
Model:
<?php
namespace App\Models;
use CodeIgniter\Model;
class EmployeeTimeLog extends Model{
private $tableName = "tbl_employee_time_log";
public function __construct(){
$db = \Config\Database::connect();
$this->builder = $db->table($this->tableName);
}
private function timeData($employeeId, $startDate, $endDate){
// builder function DRY
$this->builder->where("EmployeeId", $employeeId);
$this->builder->where("Time >", $startDate);
$this->builder->where("Time <", $endDate);
$this->builder->orderBy("Time", "ASC");
}
public function getTimeIn($employeeId, $startDate, $endDate){
// call builder function and add timeIn to only get clock in times
$this->timeData($employeeId, $startDate, $endDate);
$this->builder->where("Type", "timein");
return $this->builder->get();
}
public function getTimeOut($employeeId, $startDate, $endDate){
// call builder function and add timeout to only get clock out times
$this->timeData($employeeId, $startDate, $endDate);
$this->builder->where("Type", "timeout");
return $this->builder->get();
}
}
?>
View:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Timesheets</title>
</head>
<body>
<h1>Timesheets</h1>
<?php
echo "<h1>Times</h1>";
foreach ($output as $times) {
echo "Time in: ";
if(!is_null($times[0])){
echo $times[0]->Time;
}else{
echo "-";
}
echo " Time out: ";
if(!is_null($times[1])){
echo $times[1]->Time;
}else{
echo "-";
}
echo "<br>";
}
?>
</body>
</html>
Output:
Database:
The whole algorithm works by searching through the date between the start and end date of the dates provided. You can then check any timein and timeout values which have the same date as the loop date. If a date cannot be found, the default is NULL which is replaced by "-" in the view.
The algorithm specifically gets the earliest timein and the latest timeout value for the employee. This means that if the employee clocks-in twice in one day the earliest date will be used and the opposite for clock-out.
This algorithm specifically works for one employee, but just use your select * employees and loop through instead of the employeeId on the controller. You may want to look in the documentation for the Query Builder in CodeIgniter it makes querying the database much easier, and will escape data to protect from SQL Injections etc.
It could be better to redesign your database table and check on insert rather than manipulate on select. Where you have one row for each day and the timein and timeout which represent the time. Something like this:
You can check to see if the date already exists, if so override the current value if needed. If the current date doesn't exist for that employee create a new record.
If you need any clarification, let me know,
Thanks,

Send Data From Controller to View Code Igniter

I have an function get() on my controller. This function I use to get data from database and I save it to $data variable.
It's possible to call get() function on another function and I send the $data variable to the view? I want to make two view, one is classic and another one is modern view.
This is my code:
public function get()
{
$data['all'] = $this->genbamodel->getAll();
$data['delay'] = $this->genbamodel->getDelay();
$data['ontime'] = $this->genbamodel->getOntime();
$data['ahead'] = $this->genbamodel->getAhead();
$data['unloading'] = $this->genbamodel->getUnloading();
$data['topdelay'] = $this->genbamodel->getTopDelay();
$data['topahead'] = $this->genbamodel->getTopAhead();
$data['jumlah'] = count($this->genbamodel->getID())+1;
for ($j=1; $j < $data['jumlah']; $j++) {
$datas['lalax'.$j] = $this->genbamodel->getRecord($j);
$data['lala'.$j] = array_reverse($datas['lalax'.$j]);
}
$data['time'] = $this->genbamodel->getTime();
}
public function home_classic()
{
$this->get();
$this->load->view('home_classic',$data);
}
public function home_modern()
{
$this->get();
$this->load->view('home_modern',$data);
}
Hope this will help you :
Your get() method should return $data
public function get()
{
$data['all'] = $this->genbamodel->getAll();
$data['delay'] = $this->genbamodel->getDelay();
$data['ontime'] = $this->genbamodel->getOntime();
$data['ahead'] = $this->genbamodel->getAhead();
$data['unloading'] = $this->genbamodel->getUnloading();
$data['topdelay'] = $this->genbamodel->getTopDelay();
$data['topahead'] = $this->genbamodel->getTopAhead();
$data['jumlah'] = count($this->genbamodel->getID())+1;
for ($j=1; $j < $data['jumlah']; $j++) {
$datas['lalax'.$j] = $this->genbamodel->getRecord($j);
$data['lala'.$j] = array_reverse($datas['lalax'.$j]);
}
$data['time'] = $this->genbamodel->getTime();
return $data;
}
in home_classic() do it like this
public function home_classic()
{
$data = $this->get();
$this->load->view('home_classic',$data);
}
in home_modern() do the same
public function home_modern()
{
$data = $this->get();
$this->load->view('home_modern',$data);
}
for more : https://www.codeigniter.com/user_guide/general/views.html#adding-dynamic-data-to-the-view

how to count all the array of data in controler

I want to count the array of data got by using postgresql using codeigniter.I used foreach loop and used count($row['zero']) to count but it results only 1.please guide me.
Here is my controller
public function indicators($pcode = '', $dcode = '') {
$this->data['p_code'] = $pcode;
$this->data['d_code'] = $dcode;
$this->data['pcode'] = $this->employment_model->get_project();
$this->data['attend_household'] = $this->employment_model->household_absent_all($dcode);
var_dump($this->data['attend_household']);
die();
foreach($this->data['attend_household'] as $row){
if($row['zero']>0){
$this->data['zerroo']=count($row['zero']);
}
}
if (count($this->data['attend_household']) > 0) {
$this->data['attend_household'] = $this->data['attend_household'][0];
}
// var_dump($this->data);
$this->load->view("employment_info", $this->data);
}
Here is my model
function household_absent_all($dcode) {
$dcode_query = "";
if ($dcode != '') {
if ($dcode == 'all') {
$dcode_query = "";
} else {
$dcode_query = "where t.dcode='$dcode'";
}
}
$sql = "select (100-(sum(days_worked)/sum(days_offered))*100) as zero,regno,sum(days_offered) as days_offered,sum(days_worked) as days_worked
from(
select*
from assets.tblprojects a
join(
select pcode as project,hh_jobcard_no
from employment.work_group_member
)wg on a.pcode=wg.project
join(
select regno,at.days_offered,at.days_worked
from employment.registration_hh_member
join(
select pcode as a_pcode,hh_jobcard_no as jobcard,mem_id,
sum((shift_1*0.5)+(shift_2*0.5)) as days_worked,count(adate) as days_offered
from employment.attendence
group by a_pcode,jobcard,mem_id
)at on regno=at.jobcard and s_no=at.mem_id
)rhm on wg.hh_jobcard_no=rhm.regno
)t
group by regno
$dcode_query
";
$result = $this->db->query($sql)->result_array();
return $result;
}
try like this
$i=0;
foreach($this->data['attend_household'] as $row){
if($row['zero']>0){
$i++;
}
}
$this->data['zerroo']=$i;
how about using the num_rows() as your counter.
if($num = $result->num_rows() >=1){
$result = array('result_set'=>$result->result_array(),'result_count'=>$num);
}else{
$result = array('result_set'=>FALSE,'result_count'=>FALSE);
}
return $result;
then on your controller you can use it as $variable['result_count'] for count and $variable['result_set'] for the data results.

Laravel Query Builder dynamic nested where/orWhere

I hope someone can help guide me in the right direction. I am new to Laravel and far from the best php coder. I am trying to build dynamic nested where/orWhere clauses and I am stuck, and by stuck I mean I can't figure it out. The code I have so far is:
public static function runNested($rId, $current_id) {
$insidestuff = DB::table('tableName')
->select('all fields necessary')
...
->get();
$count=1;
foreach ($insidestuff as $inside)
{
$var1 = $inside->var1;
$var2 = $inside->var2;
$var3 = $inside->var3;
$var4 = $inside->var4;
if ($count == 1)
{
$outsideMet = $outsideMet->where(function($query) use ($var1, $var2, $var3, $var4, $id)
{
$query->where($var1, $var2, $var3);
$outsideMet = myClass::runNested($var4, $id);
});
} else {
$outsideMet = $outsideMet->orWhere(function($query) use ($var1, $var2, $var3, $var4, $id)
{
$query->where($var1, $var2, $var3);
$outsideMet = myClass::runNested($var4, $id);
});
}
$count++;
}
}
public static function runOutside($rId) {
$outsideQuery = DB::table('tableName')
->select('all necessary fields')
->get();
$outsideMet = dbTable::select('*');
$outsideMet = $outsideMet->leftJoin('necessary fields');
$outsideCount=1;
foreach ($oursideQuery as $oQuery)
{
$oVar1 = $oQuery->oVar1;
$oVar2 = $oQuery->oVar2;
$oVar3 = $oQuery->oVar3;
$oVar4 = $oVar4;
$oVar5 = $oQuery->oVar5;
if ($outsideCount == 1)
{
$outsideMet = $outsideMet->where(function($query) use ($oVar1, $oVar2, $oVar3, $oVar4, $oVar5)
{
$query->where($oVar1, $oVar2, $oVar3);
$outsideMet = myClass::runNested($oVar4, $oVar5);
});
} else {
$outsideMet = $outsideMet->orWhere(function($query) use ($oVar1, $oVar2, $oVar3, $oVar4, $oVar5)
{
$query->where($oVar1, $oVar2, $oVar3);
$outsideMet = myClass::runNested($oVar4, $oVar5);
});
}
$outsideCount++;
}
$outsideMet = $outsideMet->count();
I hope someone can tell me how to do this properly, and if the above code is even close, what I am doing wrong. Thank you in advance for the help and guidance.

Magmi import update option

does Magmi has a option to disable or make quantity to 0 for products not in csv file in import?.
Because my supplier remove all out of stock products from csv file.
If someone can help me on this or finding another solution please.
Thank you in advance
I've created a plugin that disables files not in the CSV. I prefer disabling the items, instead of actually deleting them in case something goes wrong (it won't wipe my database).
Create the plugin file magmi/plugins/extra/general/itemdisabler/magmi_itemdisabler_plugin.php
In the file, paste in the following and save:
Plugin Code:
<?php
class Magmi_ItemdisablerPlugin extends Magmi_ItemProcessor
{
protected $datasource_skus = array();
public function getPluginInfo()
{
return array("name"=>"Magmi Magento Item Disabler",
"author"=>"Axel Norvell (axelnorvell.com)",
"version"=>"1.0.6");
}
public function afterImport()
{
$this->log("Running Item Disabler Plugin","info");
$this->disableItems();
return true;
}
public function getPluginParams($params)
{
return array();
}
public function isRunnable()
{
return array(true,"");
}
public function initialize($params)
{
}
public function processItemAfterId(&$item,$params=null)
{
if(isset($item['sku']))
{
$this->datasource_skus[] = $item['sku'];
}
}
public function disableItems()
{
if(count($this->datasource_skus) <= 0)
{
$this->log('No items were found in datasource. Item Disabler will not run.', "info");
return false; /* Nothing to disable */
}
//Setup tables
$ea = $prefix!=""?$prefix."eav_attribute":"eav_attribute";
$eet = $prefix!=""?$prefix."eav_entity_type":"eav_entity_type";
$cpe = $prefix!=""?$prefix."catalog_product_entity":"catalog_product_entity";
$cpei = $prefix!=""?$prefix."catalog_product_entity_int":"catalog_product_entity_int";
//Get "status" attribute_id
$status_attr_id = "
SELECT ea.attribute_id FROM $ea ea
LEFT JOIN $eet eet ON ea.entity_type_id = eet.entity_type_id
WHERE ea.attribute_code = 'status'
AND eet.entity_type_code = 'catalog_product'";
$result = $this->selectAll($status_attr_id);
if (count($result) == 1) {
$attribute_id = $result[0]['attribute_id'];
}
unset($result);
//Get all active items
$sql = "SELECT e.sku, e.entity_id FROM $cpei i
INNER JOIN $cpe e ON
e.entity_id = i.entity_id
WHERE attribute_id=?
AND i.value = 1";
$all_magento_items = $this->selectAll($sql, array($attribute_id));
//Setup the magento_skus array for easy processing.
$magento_skus = array();
foreach($all_magento_items as $item)
{
$this->log("{$item['sku']} found in Mage", "info");
$magento_skus[$item['sku']] = $item['entity_id'];
}
//process the array, move anything thats in the datasource.
foreach($this->datasource_skus as $sku)
{
if(isset($magento_skus[$sku]))
{
unset($magento_skus[$sku]);
}
}
if(!empty($magento_skus))
{
foreach($magento_skus as $sku => $id)
{
$this->log("Disabling Item Id $id with SKU: $sku", "info");
$this->update("
UPDATE $cpei i
INNER JOIN $cpe e ON
e.entity_id = i.entity_id
SET VALUE = '2'
WHERE attribute_id = ?
AND i.value = 1
AND e.sku=?", array($attribute_id, $sku));
}
}
else
{
//If the Datasource contains all Magento's items.
$this->log('All items present in datasource. No items to disable.', "info");
}
}
}
Then login to Magmi, enable the plugin and run the import. This plugin will execute after the import has completed. It opens the datasource, logs all of the SKUs, then compares them against the Magento database. Any skus that aren't found in the datasource are disabled. This plugin could be optimized a bit better but it works as it is right now.

Resources