Fatal error in CodeIgniter while calling function itselft - codeigniter

I want to insert dynamic data into my preorder traversal table. I am using rebuild_tree technique by referencing this tutorial : http://www.sitepoint.com/hierarchical-data-database-3/
Here I modified the code as per the need in codeigniter. But when I calling function rebuild_tree in foreach loop it showing the fatal error like :
Fatal error: Maximum function nesting level of '100' reached, aborting! in /var/www/hr/system/database/drivers/mysql/mysql_driver.php on line 199 Call Stack: 0.0389 360600 1. {main}() /var/www/hr/index.php:0 0.0401 468096 2. require_once('/var/www/hr/system/core/CodeIgniter.php') /var/www/hr/index.php:240 0.0689 3798516 3. call_user_func_array() /var/www/hr/system/core/CodeIgniter.php:359 0.0689 3798588 4. Structure->add_structure() /var/www/hr/system/core/CodeIgniter.php:0 0.0689 3798632 5.
structure_model->rebuild_tree() /var/www/hr/application/controllers/test/structure.php:42 0.0701 3928356 6. structure_model->rebuild_tree() /var/www/hr/application/models/test/structure_model.php:35 0.0703 3931572 7. structure_model->rebuild_tree() /var/www/hr/application/models/test/structure_model.php:35 0.0705 3934788 8. structure_model->rebuild_tree() /var/www/hr/application/models/test/structure_model.php:35 0.0706 3938008 9.
CI_DB_driver->query() /var/www/hr/application/models/xome/structure_model.php:29 0.0848 4216988 97. CI_DB_driver->simple_query() /var/www/hr/system/database/DB_driver.php:299 0.0848 4216988 98. CI_DB_mysql_driver->_execute() /var/www/hr/system/database/DB_driver.php:453 0.0848 4216988 99. CI_DB_mysql_driver->_prep_query() /var/www/hr/system/database/drivers/mysql/mysql_driver.php:178
And Following is my code:
function rebuild_tree($parent, $left) {
$resultArr = array();
$parent = $_GET['level'];
$left = $_GET['lft'];
$right = $_GET['rgt'];
$right = $left + 1;
$sql = 'SELECT * FROM subunit WHERE level="' . $parent . '";';
$query = $this->db->query($sql);
$data = $query->result();
foreach ($data as $datap) {
$resultArr['name'] = $datap->name;
$resultArr['level'] = $datap->level;
$right = $this->rebuild_tree($resultArr['level'], $right);
}
$sql = 'UPDATE subunit SET lft=' . $left . ', rgt=' . $right . ' WHERE name="' . $parent . '";';
$query = $this->db->query($sql);
return $right + 1;
}
Is there any solution for this. Thank you.

Be careful when calling the function itself. Often it might end up calling itself forever. You need some kind of limitation.
You can check most of Codeigniters own libraries, and check the initialize(). That's a good example on how to handle a function which calls itself. Basicly it calls itself till no array remains, thus having an end.
In your case if 2 mysql rows are each other's parents or any similiar, you've opened the door for infinite loops.
Solution
The real problem in your function is that you pass an argument which is supposed to get a new value, however you override that value through your $_GET. That means that the 2nd function calls does exactly the same as the first function call, the third the same, the fourth the same and so on. Since the function calls never changes AND calls itself, your problem occur.
Change above part to:
$parent = $this->input->get('level');
$left = ($left ?: $this->input->get('lft'));
$right = $this->input->get('rgt');

Related

Lumen: Auto increment and Reset Transaction_ID Automatically every month

I trying to generate random transaction_id, with format "2000-yymmm-0000".
I already know how to set the transaction_id, but I have a problem with the auto-increment from "2000-yymm-0000" to "2000-yymm-0001", and reseting automatically to "2000-yymm-0000" at every new month.
I put this logic in different path of controller.
PS: I'm using Lumen 6.0 with Laravel 6.0.
I'm trying to create the increment with:
$number = sprintf('%04d', 0000);
$number++;
but it didn't work.
$year = 2000;
$time = date('ym');
$number = sprintf('%04d', 0000);
$transaction_id = $year . '-' . $time . '-' . $number;
$transaction_data = explode('-', $transaction_id);
$month = date("m", strtotime($transaction_data[0]));
I expect the result to automatically increment every time a new data is stored to the database. But the actual result is transaction_id being having always the same value 2000-yymm-0000.
What am I doing wrong?
I know it's weird to answer my own question, but i already find the solution.
in Models/Order.php
i create a function like this.
public function count()
{
$this->where(transaction_id)->count();
}
after that i call the function from Model/Order.php to OrdersController.php
public function create(Request $request)
{
$year = 2000;
$date = date('ym');
$total_data = $this->table->count();
$number = str_pad($total_data + 1, 4, 0, STR_PAD_LEFT);
$transaction_id = $year . '-' . $date . '-' . $number;
return $this->success('count all transaction id', $t_id);
}

Laravel chained jobs as array not working. Attempt to assign property of non-object

I'm trying to loop over multiple servers that need to run 1 at a time using Laravel's withChain. The first job completes just fine but the data I'm passing within the chained jobs gives me the
Attempt to assign property of non-object
When I log out the initial dispatched data it looks just like the constructed data in my array so I'm not sure what I'm doing wrong.
$new_jobs_array = [];
foreach ($this->wasRequest->nodes->sortByDesc('pivot.node_type') as $node) {
if ($node->pivot->node_type != 'WAS_DMGR')
{
$snode = strtolower($node->hostname);
$shortname = strtok($snode, '.');
$fileName = strtolower($mnemonic).'_'.$shortname.'_'.$reqId.'.json';
$sourceJsonPath = base_path() . "/json/was/" . $fileName;
$new_job = 'new BootStrapWasNode('. $node .', '. $this->wasRequest .', '.$sourceJsonPath.')';
array_push($new_jobs_array, $new_job);
} else {
$dmgr_node = $node;
}
}
//Log::info($new_jobs_array);
$dmgr_node_sname = strtok($this->wasRequest->nodes->where('pivot.node_type', 'WAS_DMGR')->pluck('hostname')[0], '.');
$fileName = strtolower($mnemonic).'_'.$dmgr_node_sname.'_'.$reqId.'.json';
$sourceJsonPath = base_path() . "/json/was/" . $fileName;
$this->wasRequest->status = 'Bootstrapping Nodes';
$this->wasRequest->save();
//Log::info("DMGR-------------------".$dmgr_node.", ".$this->wasRequest.", ".$sourceJsonPath);
BootStrapWasNode::withChain($new_jobs_array)->dispatch($dmgr_node, $this->wasRequest, $sourceJsonPath);
I can attach the log view if needed but there is a lot of data for each node. The issue is with the $new_nodes_array, the initial dispatch($dmgr_node,$this->wasRequest,$sourceJsonPath) completes without issue.
Was able to figure out the issue.
This line was incorrect
$new_job = 'new BootStrapWasNode('. $node .', '. $this->wasRequest .','.$sourceJsonPath.')';
It should be
$new_job = new BootStrapWasNode($node, $this->wasRequest, $sourceJsonPath);

Php Generate random number without repeat

$C = $_POST['Cc'];
$X = $_POST['X'];
$CX = $_POST['Cc'] . $_POST['X'];
$NC = preg_replace_callback("/x/" ,function() {return rand(0,9);}, $CX);
$New = $NC ;
$NNew = str_repeat($New,10);
echo $NNew;
what's wrong when i output it , it gives me the same number How to make It Don't Give me the same Numbers ??
It's basically you are not changing the seed for the rand method. Each time it's getting same seed and generating same number.
Read this PHP manual : http://php.net/manual/en/function.srand.php
Check the code snippet below:
<?php
// seed with microseconds
function make_seed()
{
list($usec, $sec) = explode(' ', microtime());
return $sec + $usec * 1000000;
}
srand(make_seed());
$randval = rand(0,9);
echo $randval;
?>
Or you can use mt_rand() which is seeded differently on each execution.

Laravel Function at Scheduler

I have a laravel 5.2 project and i want to run laravel scheduler by specified time from database data, so i create schedule kernel like this :
protected function schedule( Schedule $schedule )
{
// Run proposal schedule to generate albums
$proposals = Proposal::whereStatus( true )->whereGenerated( false )->get();
foreach ( $proposals as $key => $proposal ) {
$time = date( 'i G ', strtotime( $proposal->duedate ) );
$send_date = date( $time . 'j n * Y', strtotime( $proposal->created_at . ' + ' . $proposal->timeout . ' days' ) );
$schedule->call( function() use( $proposal ) {
$proposal->generateAlbums();
} )->cron( $send_date );
}
}
And it's work fine, until i reset my migration and try to migrate from the begining, i got an error like this
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "proposals" does not exist
So i think it's not good if i place my code there, right??
where should i place my code for this??
Or it's already right and what i need just to check if database table are exists or not??
I need best solution to code just the way laravel provide..
You can check if table exists or not by
if (Schema::hasTable('proposals')) {
//Your code which needs to execute
}

Timepicker that removes times as they're selected (ajax)

I'm building a booking form for a moving business that uses a calendar combined with a start and end time. I built the timepicker with Formidable Pro, and it allows me to check "unique" on time fields which automatically removes them on the selected date. However it doesn't automatically remove the times from within the range between start and end times (ie: if someone chooses to rent a truck from 1am-3am I need 1am,2am,and 3am to be removed from future options but right now it only removes 1am and 3am) . I need to write ajax to remove the in-between times from the options. I'm not sure where to begin. This is the current ajax_time_ options function. Any push in the right direction would be appreciated.
function ajax_time_options(){
global $frmpro_settings, $frmdb, $wpdb;
//posted vars = $time_field, $date_field, $step, $start, $end, $date, $clock
extract($_POST);
$time_key = str_replace('field_', '', $time_field);
$date_key = str_replace('field_', '', $date_field);
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', trim($date)))
$date = FrmProAppHelper::convert_date($date, $frmpro_settings->date_format, 'Y-m-d');
$date_entries = FrmEntryMeta::getEntryIds("fi.field_key='$date_key' and meta_value='$date'");
$opts = array('' => '');
$time = strtotime($start);
$end = strtotime($end);
$step = explode(':', $step);
$step = (isset($step[1])) ? ($step[0] * 3600 + $step[1] * 60) : ($step[0] * 60);
$format = ($clock) ? 'H:i' : 'h:i A';
while($time <= $end){
$opts[date($format, $time)] = date($format, $time);
$time += $step;
}
if($date_entries and !empty($date_entries)){
$used_times = $wpdb->get_col("SELECT meta_value FROM $frmdb->entry_metas it LEFT JOIN $frmdb->fields fi ON (it.field_id = fi.id) WHERE fi.field_key='$time_key' and it.item_id in (". implode(',', $date_entries).")");
if($used_times and !empty($used_times)){
$number_allowed = apply_filters('frm_allowed_time_count', 1, $time_key, $date_key);
$count = array();
foreach($used_times as $used){
if(!isset($opts[$used]))
continue;
if(!isset($count[$used]))
$count[$used] = 0;
$count[$used]++;
if((int)$count[$used] >= $number_allowed)
unset($opts[$used]);
}
unset($count);
}
}
echo json_encode($opts);
die();
}

Resources