multiple if/else statement is laravel not working for me - laravel

please help me out, i want to perform this conditional statement is one or two of this conditional statement will be through but none of the expression i provide will be save to database... Thanks
This are the function that i called
public function updateuserstage($userid){
$user = User::where("user_id",$userid)->first();
//get the stage bonus for this stage
$stageBonus = StageBonus::where("user_id",$userid)-
where("stage",$user->stage)->first();
//get the completion bonus
$bonus = $this->completionbonus($user->stage);
$directdownline = $this->loaduserdirectdownlines($userid);
// $downlineright = $this-
loaduserdirectdownlines($directdownline[0]->user_id);
// $downlineleft = $this-
loaduserdirectdownlines($directdownline[1]->user_id);
$downlineright = $this-
loaduserdirectdownlinesrightleft($directdownline[0]->user_id);
$downlineleft = $this-
loaduserdirectdownlinesrightleft($directdownline[1]->user_id);
// matching bonus reward
if(($user->boosted == 1) && ($directdownline[0]->boosted == 1) &&
($directdownline[1]->boosted == 1) && ($downlineright == 2) &&
($downlineleft == 2)){
if (isset($bonus['point']))
$stageBonus->point = $stageBonus->point+$bonus['point'];
if (isset($bonus['ultimapv']))
$stageBonus->point = $stageBonus-
point+$bonus['ultimapv'];
if (isset($bonus['cash']))
$stageBonus->cash = $stageBonus->cash+$bonus['cash'];
// direct downline reward
This function here return true when i log it outside loop but never save to database in here
} else if(($user->boosted == 1) && ($directdownline[0]->boosted ==
1) || ($directdownline[1]->boosted == 1)){
if (isset($bonus['point']))
$stageBonus->point = $stageBonus->point+$bonus['point'];
if (isset($bonus['totalultimarefbonus']))
$stageBonus->cash = $stageBonus-
cash+$bonus['totalultimarefbonus'];
} else if(($user->boosted == 1) && ($directdownline[0]->boosted ==
1) || ($directdownline[1]->boosted == 0)){
if (isset($bonus['point']))
$stageBonus->point = $stageBonus->point+$bonus['point'];
if (isset($bonus['ultimarefbonus']))
$stageBonus->cash = $stageBonus-
cash+$bonus['ultimarefbonus'];
} else if(($user->boosted == 1) && ($directdownline[0]->boosted ==
0) || ($directdownline[1]->boosted == 1)){
if (isset($bonus['point']))
$stageBonus->point = $stageBonus->point+$bonus['point'];
if (isset($bonus['ultimarefbonus']))
$stageBonus->cash = $stageBonus-
>cash+$bonus['ultimarefbonus'];
} else if(($user->boosted == 1) && ($downlineright[0]->boosted ==
1) || ($downlineright[1]->boosted == 1) || ($downlineleft[0]-
>boosted == 1) || ($downlineleft[1]->boosted == 1)){
if (isset($bonus['ultimarefbonuschild']))
$stageBonus->cash = $stageBonus-
>cash+$bonus['ultimarefbonuschild'];
// referral reward for parent user if boosted
} else if($user->boosted == 1){
if (isset($bonus['refbonus']))
$stageBonus->cash = $stageBonus->cash+$bonus['refbonus'];
// referral reward for user that is not boosted
} else if($user->boosted == 0){
if (isset($bonus['point']))
$stageBonus->point = $stageBonus->point+$bonus['point'];
if (isset($bonus['refbonus']))
$stageBonus->cash = $stageBonus->cash+$bonus['refbonus'];
}
$stageBonus->completed = 1;
$stageBonus->completed_on = Carbon::now();
$stageBonus->save();
//increase user stage
$user->stage+= 1;
$user->save();
}
}
This is the function that will determine what to save in those expression
public function completionbonus($stage){
switch($stage){
case 1:
return [ 'voucher'=>600, 'cash'=>5000, 'point'=>100, 'refbonus'=>2000, 'totalultimarefbonus'=>4000, 'ultimarefbonus'=>2000, 'ultimarefbonuschild'=>1500, 'ultimapv'=>700];
break;
}
}
This is the model
Schema::create('stage_bonuses', function (Blueprint $table) {
$table->increments('id');
$table->string('user_id');
$table->bigInteger('point')->default(0);
$table->bigInteger('cash')->default(0);
$table->date('date_entered')->nullable();
$table->timestamps();
$table->foreign('user_id')->references('user_id')->on('users');
});

Thank you very much i really appreciate your effort
Have actually got the answer the condition is returning two or true and if is returning two or more true it will not get save to database... Thanks

Related

QT - Wrong numbering in the case of two or more overlapping numberings

I need to use two nested lists.
The first is a numeric list and the second is an alphabetical one.
When the second is over and I go to the first list, the numbers are counted from the beginning.
I tried to create a new list createList() but it didn't work
else if (object == ui->textEdit_left && event->type() == QEvent::KeyPress)
{
QTextCursor cursor = ui->textEdit_left->textCursor();
QTextListFormat::Style currentStyle = cursor.currentList()->format().style();
QTextBlockFormat format = cursor.blockFormat();
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
QTextListFormat::Style newStyle = createNewStyle(currentStyle, keyEvent->key());
if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter)
{
format.setIndent(format.indent() - 1);
cursor.setBlockFormat(format);
cursor.createList(newStyle);
return true;
}
else if (keyEvent->key() == Qt::Key_Tab)
{
format.setIndent(format.indent() + 1);
cursor.setBlockFormat(format);
cursor.createList(newStyle);
return true;
}
else
{
return false;
}
}
return false;

How to add another array value in codeigniter using getRecords

The orignial code was like this , I want to get landline_no value also in getRecords, How to do that
public function checklead() {
$lead = $_POST['number'];
$check = $this->common_model->getRecords('leads',array("phone_no"=>$lead));
if(count($check) > 0) {
$lead = $this->common_model->getRecored_row('leads',array("phone_no"=>$lead));
if($lead->assignto_self != 0) {
$assignto = $lead->assignto_self;
$key = 'Self Assign';
} else if($lead->assignto_se != 0) {
$assignto = $lead->assignto_se;
$key = '';}
What I have achieved so far,but not getting array values from getRecords
$lead = $_POST['number'];
$check = $this->common_model->getRecords('leads',array("phone_no"=>$lead),array("landline_no"=>$lead));
//echo "<pre>";
//print_r($check);
//echo $check[0]['landline_no'];exit;
if(count($check) > 0) {
$lead = $this->common_model->getRecored_row('leads',array("phone_no"=>$lead,"landline_no"=>$check[0]['landline_no']));
Code for getRecords:
function getRecords($table,$db = array(),$select = "*",$ordercol = '',$group = '',$start='',$limit=''){
$this->db->select($select);
if(!empty($ordercol)){
$this->db->order_by($ordercol);
}
if($limit != '' && $start !=''){
$this->db->limit($limit,$start);
}
if($group != ''){
$this->db->group_by($group);
}
$q=$this->db->get_where($table, $db);
return $q->result_array();
}
// Get Recored row
public function getRecored_row($table,$where)
{
$q = $this->db->where($where)
->select('*')
->get($table);
return $q->row();
}
Check my answer: This code also working well, i have written, but i am not sure , this logic is correct or not kindly check this one.
public function checklead() {
$lead = $_POST['number'];
if($this->common_model->getRecords('leads',array("phone_no"=>$lead)))
{
$check=$this->common_model->getRecords('leads',array("phone_no"=>$lead));
}
else
{
$check=$this->common_model->getRecords('leads',array("landline_no"=>$lead));
}
echo "<pre>";
//echo $check;
//print_r($check); exit;
$p= $check[0]['phone_no'];
$l= $check[0]['landline_no'];
// exit;
if(count($p) > 0 || count($l)>0) {
$lead = $this->common_model->getRecored_row('leads',array("phone_no"=>$p));
$lead1 = $this->common_model->getRecored_row('leads',array("landline_no"=>$l));
if($lead->assignto_self != 0 || $lead1->assignto_self != 0) {
$assignto = $lead->assignto_self;
$key = 'Self Assign';
} else if($lead->assignto_se != 0 || $lead1->assignto_se != 0) {
$assignto = $lead->assignto_se;
$key = '';
}else if($lead->assignto_tl != 0 || $lead1->assignto_tl != 0) {
$assignto = $lead->assignto_tl;
$key = '';
} else if($lead->uploaded_by != 0 || $lead1->uploaded_by != 0) {
$assignto = $lead->uploaded_by;
$key = 'Uploaded by';
}
$user = $this->common_model->getRecored_row('admin',array("id"=>$assignto));
$role = $this->common_model->getRecored_row('role',array("id"=>$user->role));
$this->session->set_flashdata('message', array('message' => 'This Lead Already exist with '.$user->name.' ('.$role->role.') '.' ','class' => 'danger'));
redirect(base_url().'leads');
} else {
redirect(base_url().'leads/add_newlead/'.$lead);
}
}
There does not seem to be any reason to use getRecords(). The $check value has no useful purpose and creating it is a waste of resources.
We don't need $check because getRecord_row() will return the "lead" if found so the only check needed is to see if getRecord_row() returns anything. getRecord_row() uses the database function row() which returns only one row or null if no rows are found. Read about row() here.
If what you want is to find the "lead" that has either a "phone_no" or a "landline_no" equal to $_POST['number'] then you need to use a custom string for the where clause. (See #4 at on this documentation page.) You need a custom string because getRecord_row() does not allow any other way to ask for rows where a='foo' OR b='foo'. Here is what I think you are looking for.
public function checklead()
{
// use input->post() it is the safe way to get data from $_POST
$phone = $this->input->post('number');
// $phone could be null if $_POST['number'] is not set
if($phone)
{
$lead = $this->common_model->getRecored_row('leads', "phone_no = $phone OR landline_no = $phone");
// $lead could be null if nothing matches where condition
if($lead)
{
if($lead->assignto_self != 0)
{
$assignto = $lead->assignto_self;
$key = 'Self Assign';
}
else if($lead->assignto_se != 0)
{
$assignto = $lead->assignto_se;
$key = '';
}
}
}
}
The main difference between getRecords() and getRecord_row() is the number of records (rows of data) to return. getRecord_row() will return a maximum of one record while getRecords() might return many records.
getRecords() accepts arguments that allow control of what data is selected ($db, $select), how it is arranged ($ordercol, $group), and the number of rows to retrieve ($limit) starting at row number x ($start) .

How to write a condition based query?

I have a query to run based on the user type selected. I have around 9 users. Based on 9 users, only first line will be different remaining conditions are going to be same.
Below is the code I am writing. I dont want to repeat the same line again for 9 users.
How can I this query without repeating the condition?
if($user_id == 1) {
$slmids = Production::groupBy('gid', 'rid', 'aid', 'sid')
//same
->where("yrsno", $currentYear)
->orderBy("order")
->orderBy("rid")
->orderBy("aid")
->get(['gid', 'rid', 'aid', 'sid'])
->toArray();
}
else if($user_id == 2) {
$slmids = Production::where('gid', $user['sgrp'])
//same
->where("yrsno", $currentYear)
->orderBy("order")
->orderBy("rid")
->orderBy("aid")
->get(['gid', 'rid', 'aid', 'sid'])
->toArray();
}
else if($user_id == 3){
.....
}
$production = Production::where("yrsno", $currentYear)
->orderBy("order")
->orderBy("rid")
->orderBy("aid")
->get(['gid', 'rid', 'aid', 'sid']);
if ($user_id == 1) {
$production = $production->groupBy('gid', 'rid', 'aid', 'sid');
} else if($user_id == 2) {
$production = $production->where('gid', $user['sgrp']);
} else if($user_id == 3) {
....
}
$result = $production->toArray();
You can try like this way.
$slmids = Production::where("yrsno", $currentYear);
if ($user_id == 1)
$slmids = $slimds->groupBy('gid', 'rid', 'aid', 'sid');
elseif ($user_id == 2)
$slmids = $slmids->where('gid', $user['sgrp']);
...
// you can use switch-case if you want
$slmids = $slmids->orderBy('order')
->orderBy('rid')
->orderBy('aid')
->get(['gid', 'rid', 'aid', 'sid'])
->toArray();
I believe groupBy must be placed first before orderBy
Use When to make easy Try following code:
$production = Production::where("yrsno", $currentYear)
->orderBy("order")
->orderBy("rid")
->orderBy("aid")
->query();
$production->when($user_id == 1, function ($query) {
$query->groupBy('gid', 'rid', 'aid', 'sid');
});
$production->when($user_id == 2, function ($query) use ($user) {
$query->where('gid', $user['sgrp']);
});
$results = $production->get()->toArray();
check more example https://stackoverflow.com/a/52333718/8970463

I want to add Arabic characters using preg_match() function

function validate_name($name, $name_limit, $name_system){
if(preg_match("/^[a-zA-Z0-9]{1,}[_-]?[a-zA-Z0-9]{1,}[_-]?[a-zA-Z0-9]{1,}$/", $name) && strlen($name) <= $name_limit && !ctype_digit($name) && $name !== $name_system && strlen($name) >= 4){
$valid_name = 1;
}
else {
$valid_name = 0;
}
return $valid_name;
}

sorting showing unexpected results

I am sorting a grid with knockout.js. Following is my sort function
this.sortByName = function() {
var event = arguments[1];
var targeElement = event.originalTarget;
// console.info(targeElement);
console.log(targeElement.attributes[1].nodeValue);
order = 'sorting';
configuration.data.sort(function(a, b) {
if(a.name<b.name){
order = 'sorting_desc';
return a.name > b.name ? -1 : 1;
}
else if(a.name>b.name){
order = 'sorting_asc'
return a.name < b.name ? -1 : 1;
}
});
$(targeElement).removeClass('sorting_asc sorting_desc').addClass(order);
};
The by default grid view
The Sorted image 1
The sorted image 2
As you can see the order of sorting is not correct. I have been playing around for 3 days in this issue.
Well I found a solution to this problem
this.sortByName = function() {
currentOrder = arguments[0].sortClass();
var sortColumn = arguments[0].rowText;
if(currentOrder =='sorting' || currentOrder =='sorting_desc'){
currentOrder='sorting_asc';
configuration.data.sort(function(a, b) {
if (a[sortColumn] == b[sortColumn])
return 0;
else if (a[sortColumn] < b[sortColumn])
return -1;
else
return 1;
});
}else{
currentOrder='sorting_desc';
configuration.data.sort(function(a, b) {
if (a[sortColumn] == b[sortColumn])
return 0;
else if (a[sortColumn] > b[sortColumn])
return -1;
else
return 1;
});
}
self.resetSortColumns();
arguments[0].sortClass(currentOrder);
};

Resources