I want to add Arabic characters using preg_match() function - preg-match

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

Related

I don't want to show null value in array using laravel

i am trying to show value in array but all value is showing in array but i need to show all those record which is not null how can i remove null in array Does anyone have an idea please help me thanks.
Note : only i want to get value which already exist into database not null values
[1,2,3,4,5,6,7,8,null,null,null,null]
COntroller
public function store(Request $request)
{
$project = Project::with('projectBillingInfo')->where('is_amc', 1)->get();
foreach ($project as $key => $value) {
$count = 0;
if ($value->projectBillingInfo->jan != null) {
$count = $count += 1;
}
if ($value->projectBillingInfo->feb != null) {
$count = $count += 1;
}
if ($value->projectBillingInfo->mar != null) {
$count = $count += 1;
}
if ($value->projectBillingInfo->apr != null) {
$count = $count += 1;
}
if ($value->projectBillingInfo->may != null) {
$count = $count += 1;
}
if ($value->projectBillingInfo->june != null) {
$count = $count += 1;
}
if ($value->projectBillingInfo->july != null) {
$count = $count += 1;
}
if ($value->projectBillingInfo->aug != null) {
$count = $count += 1;
}
if ($value->projectBillingInfo->sep != null) {
$count = $count += 1;
}
if ($value->projectBillingInfo->oct != null) {
$count = $count += 1;
}
if ($value->projectBillingInfo->nov != null) {
$count = $count += 1;
}
if ($value->projectBillingInfo->dec != null) {
$count = $count += 1;
}
$months = [
$value->projectBillingInfo->jan,
$value->projectBillingInfo->feb,
$value->projectBillingInfo->mar,
$value->projectBillingInfo->apr,
$value->projectBillingInfo->may,
$value->projectBillingInfo->june,
$value->projectBillingInfo->july,
$value->projectBillingInfo->aug,
$value->projectBillingInfo->sep,
$value->projectBillingInfo->oct,
$value->projectBillingInfo->nov,
$value->projectBillingInfo->dec,
];
return $months;
[1,2,3,4,5,6,7,8,null,null,null,null]
}
use array_filter()
example:
$result = array_filter($array);
array_filter() remove empty array elements from array.
based on this answer:
$month=array_filter($month);
if you did not provide a callback for array_filter, all entries of array equal to FALSE or Null will be removed

Refactor Laravel Eloquent Query

I want to filter data given from eloquent laravel. I have 4 date picker in my view, and I want when each of one has filled and has date the query change.
This is my code in controller
if ($start_publish && $end_publish && $start_close && $end_close) {
$data = Tender::where('published_at','>=',$start_publish)->where('published_at','<=',$end_publish)->where('closed_at','>=',$start_close)->where('closed_at','<=',$end_close);
}elseif (!$start_publish && $end_publish && $start_close && $end_close) {
$data = Tender::where('published_at','<=',$end_publish)->where('closed_at','>=',$start_close)->where('closed_at','<=',$end_close);
} elseif ($start_publish && !$end_publish && $start_close && $end_close) {
$data = Tender::where('published_at','>=',$start_publish)->where('closed_at','>=',$start_close)->where('closed_at','<=',$end_close);
} elseif (!$start_publish && !$end_publish && $start_close && $end_close) {
$data = Tender::where('closed_at','>=',$start_close)->where('closed_at','<=',$end_close);
} elseif ($start_publish && $end_publish && !$start_close && $end_close) {
$data = Tender::where('published_at','>=',$start_publish)->where('published_at','<=',$end_publish)->where('closed_at','<=',$end_close);
}
elseif ($start_publish && $end_publish && $start_close && !$end_close) {
...
...
...
} else {
$data = Tender::get();
}
Because I have 4 input, the number of states for the condition is equal to 16 and the code is messy.
Does anyone have an idea to refactor the code?
The idea: if your variable is set, you check it. Otherwise not
You could try with this ( this is an idea, not the actual code )
$builder = Tender::query();
if($start_publish) $builder->where('published_at','>=',$start_publish);
if($start_close) $builder->where('closed_at','>=',$start_close);
...
return $builder->get()
.... and so on

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

multiple if/else statement is laravel not working for me

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

Pagination codeigniter with query search

i want to create codeigniter pagination in my search form, when i clicked submit,it successfully shows the query search but when i clicked the number page link or next page, it shows message "undefined variable rec" and "Fatal error: Call to a member function num_rows() on a non-object" ,i'm sure the query has been passed to variable $rec.
And i see the problem here when i change the page,the variable return to null, so variable $rec didn't get the value from 'post'.
my question: how do i keep the value from 'post' so the query will get the variable value when i change the page?
here is my controller:
public function search(){
$kat=$this->input->post('kategori');
$prov=$this->input->post('prov');
$kot=$this->input->post('kota');
if($kat=='' && $prov!='' && $kot!=''){
$rec=$this->db->query("select * from showall where id_prov='$prov' and id_kota='$kot' ");
}
else if($kot=='' && $kat!='' && $prov!=''){
$rec=$this->db->query("select * from showall where id_kat='$kat' and id_prov='$prov' ");
}
else if($kat=='' && $kot=='' && $prov!=''){
$rec=$this->db->query("select * from showall where id_prov='$prov' ");
}
else if($kat!='' && $prov=='' && $kot==''){
$rec=$this->db->query("select * from showall where id_kat='$kat' ");
}
else if($kat!='' && $prov!='' && $kot!=''){
$rec=$this->db->query("select * from showall where id_kat='$kat' and id_prov='$prov' and id_kota='$kot' ");
}
$dat=$rec;
$data['count']=$dat->num_rows();
if($data['count'] >0){
$data['db']=$rec;
$config['total_rows'] = $data['db']->num_rows();
$config['base_url'] = base_url().'index.php/lowongan/cari';
$config['per_page'] = 1;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data['paging'] = $this->pagination->create_links();
if($kat=='' && $prov!='' && $kot!=''){
$d = $this->db->get_where('showall',array('id_prov'=>$prov,'id_kota'=>$kot) ,$config['per_page'], $this->uri->segment(3));
}
else if($kot=='' && $kat!='' && $prov!=''){
$d= $this->db->get_where('showall',array('id_kat'=>$kat,'id_prov'=>$prov) ,$config['per_page'], $this->uri->segment(3));
}
else if($kat=='' && $kot=='' && $prov!=''){
$d = $this->db->get_where('showall',array('id_prov'=>$prov) ,$config['per_page'], $this->uri->segment(3));
}
if($kat!='' && $prov=='' && $kot==''){
$d = $this->db->get_where('showall',array('id_kat'=>$kat) ,$config['per_page'], $this->uri->segment(3));
}
else if($kat!='' && $prov!='' && $kot!=''){
$d = $this->db->get_where('showall',array('id_kat'=>$kat,'id_prov'=>$prov,'id_kota'=>$kot) ,$config['per_page'], $this->uri->segment(3));
}
$data['record']=$d;
$data1['kat']=$this->query->kategoriAll();
$data1['prov']=$this->query->showProv();
$data['q1']=$this->query->lokasi();
$data['q2']=$this->query->perusahaan();
$data['q3']=$this->query->kategori();
$this->load->view("user/head");
$this->load->view("user/bar",$data1);
$this->load->view( 'user/hal_cari',$data);
$this->load->view("user/footer");
}
else{
$data1['kat']=$this->query->kategoriAll();
$data1['prov']=$this->query->showProv();
$data['q1']=$this->query->lokasi();
$data['q2']=$this->query->perusahaan();
$data['q3']=$this->query->kategori();
$this->load->view("user/head");
$this->load->view("user/bar",$data1);
$this->load->view( 'user/kosong',$data);
$this->load->view("user/footer");
}
}
You need to keep the condition array in the URL using assoc_to_uri(). I am doing like below.
$this->load->model('classified_model','classified');
$ClassifiedList = $this->classified->searchPage($condition,'classified_view',$perPage,$page*$perPage);
$uriString = $this->uri->assoc_to_uri($condition);
$config['uri_segment'] = count($condition) * 2 + 4;
$base_url = site_url('classified/search/' . $uriString . '/page/');
$base_url = preg_replace("/\.[a-z]*$/","", $base_url);
$config['base_url'] = $base_url;
$config['per_page'] = 1;
To get the $condition from uri again use
$condition = $this->uri->uri_to_assoc();

Resources