The problem lies in the fact that I have to type the full name for the search phrase.
Maybe can I use strpos in this?
if ($filter && $searchOption && $searchPhrase && $sortField == "createDate" && $order == "asc") {
usort($caseList, function ($a, $b) {
/* #var $a CMCase */
/* #var $b CMCase */
$time1 = strtotime($a->createDate);
$time2 = strtotime($b->createDate);
return $time1 > $time2;
});
} else if ($filter && $searchOption == "search_customer" && $searchPhrase && $sortField && $order) {
$list = $caseList;
$caseList = array();
foreach ($list as $case) {
if ($case->customerName == $searchPhrase) {
$caseList[] = $case;
}
}
}
You can rewrite your code like this:
$result = array_filter($caseList, function($case) use ($searchPhrase){
return stripos($case->customerName, $searchPhrase) !== FALSE;
})
But you still need to be more concrete.
! I used stripos to case insensitive search. See the manual
P.S. Just try this instead of your code:
if ($searchOption == "search_customer" && $searchPhrase) use ($searchPhrase) {
$list = array_filter($caseList,function($case) {
return stripos($case->customerName, $searchPhrase) !== false;
});
}
This is my solution:
// Searching
{
if ($filter && $searchOption == "search_customer" && $searchPhrase && $sortField && $order) {
$list = array();
foreach ($caseList as $case) {
if (stripos($case->customerName,$searchPhrase)!== FALSE) {
$list[] = $case;
}
}
$caseList = $list;
} else if ($filter && $searchOption == "search_request" && $searchPhrase && $sortField && $order) {
$list = array();
foreach ($caseList as $case) {
if (stripos($case->identifier,$searchPhrase)!== FALSE) {
$list[] = $case;
}
}
$caseList = $list;
}
}
Related
I update my code from PHP 5 to PHP 7 and i got problem with foreach loop. I loocked wor answers here, but none is working. Or i dont understand where i have problem
function getStructure($pid = 0, $expand = array(), $alevel = 0)
{
$struct = array();
if ( $alevel > $this->levelCount) $this->levelCount = (int)$alevel;
$str = $this->dbStructure->getStructure($pid);
foreach ($str as &$row)
{
if ($row["type"] == STRUCT_PAGE)
{
$row["editLink"] = "editPage";
$row["target"] = "_self";
}
elseif ($row["type"] == STRUCT_MODULE)
{
$row["editLink"] = "editModule";
$row["target"] = "_self";
}
elseif ($row["type"] == STRUCT_LINK)
{
$row["editLink"] = "editLink";
$row["target"] = "_blank";
}
elseif ($row["type"] == STRUCT_CATALOG)
{
$row["editLink"] = "editCatalog";
$row["target"] = "_self";
}
$row["childrens"] = $this->getStructure((int)$row["id"], $expand, $alevel+1);
if ($row["type"] == STRUCT_CATALOG and isset($row["childrens"][0]["shortcut"]))
{
$row["shortcut"] = $row["childrens"][0]["shortcut"];
$row["target"] = $row["childrens"][0]["type"] == STRUCT_LINK ? "_blank" : "_self";
}
$struct[] = $row;
}
unset($row);
return $struct;
}
All the time $struct is NULL and I need to be multidimensional array
This code by itself is good. Has no problems, only ampersand is not needet. The problem was in different place. Sorry for spam
I'm trying to export data from leads table to excel using PHP/Laravel, the leads table caמ be customized to show specific columns and the export columns should be same as the table columns. the problem is - if column not exists in a specific row the column not shown empty and the 'xls' file is exported really messy...
my code looks like this:
public function excelExport(Request $request, $client_id=null)
{
$client_id = is_null($client_id) ? \Auth::client()->id : $client_id;
if(is_null($this->client_users)){
$this->client_users = $this->clientsController->listClientUsers($client_id);
}
$columns = $this->account_settings->getColumnsDef($client_id);
$query = Lead::select(array_keys($columns))->where('client_id',strval($client_id))->where('deleted',0);
$query = $this->setQueryDates($request,$query,$client_id);
$query = $this->leadsFiltersController->setExportQuery($request,$query);
$arr = $query->get()->toArray();
Excel::create('leads' , function($excel) use ($arr,$columns){
$excel->sheet('Leads' , function($sheet) use ($arr,$columns){
$rows = [];
$count = 0;
foreach($arr as $lead){
$row = $this->setExportRowData($lead,$count,$columns);
$rows[] = $row;
$count++;
}
$sheet->fromArray($rows);
});
})->export('xls');
private function setExportRowData($lead,$count,$columns,$order = true)
{
$row = [];
$order = null;
foreach($lead as $k => $v) {
if (is_array($v)) {
switch (strtolower($k)) {
case "ga_details":
if (count($v) > 2) {
foreach ($v as $key => $ga_detail) {
if ($key != "realTime_data" && $key != "uacid") {
$row[$key] = $ga_detail;
}
}
}
break;
case "status":
if (isset($v['name']) && count($v['name'])) {
$row['status'] = $v['name'];
} else {
$row['status'] = "no status";
}
break;
case "owner":
if (isset($v['owner_id']) && count($v)) {
$row['owner'] = $this->getClientOwnerUserName($this->client_users, $v['owner_id']);
} else {
$row['owner'] = "no owner";
}
break;
case "prediction":
if (isset($v['score']) && count($v)) {
$row['prediction'] = $v['score'];
} else {
$row['prediction'] = "no prediction";
}
break;
case "the_lead":
foreach ($v as $key => $lead_detail) {
$row[$key] = $lead_detail;
}
break;
case "quality":
if (isset($v['name'])) {
$row['quality'] = $v['name'];
} else {
$row['quality'] = "no quality";
}
break;
case "feeds":
if (isset($v['feeds']) && count($v['feeds'])) {
$row['feeds'] = array_pop($v['feeds'])['message'];
}
break;
default :
$row[$k] = $v;
break;
}
} else {
if ($k != "_id") {
$row[$k] = $v;
}
}
}
return $order == true ? sortArrayByArrayValues($this->order,$row) : $row;
}
sortArrayByArrayValues function looks like this:
function sortArrayByArrayValues(Array $array, Array $orderArray)
{
$rtn = [];
foreach($array as $arr){
if(isset($orderArray[$arr])){
$rtn[$arr] = $orderArray[$arr];
}else{
$rtn[$arr] = '';
}
}
return $rtn;
}
I really have no clue how to solve it, appreciate any help!!! :)
I have a calendar which is an array that I have populated with dates which are keys and with reports that are their corresponding values (whether they are done depending on day, week or month).
This consists of 3 methods MakeCalendar(), PopulateDatesWithReports(), and MakeFullCalendar().
I know I need my href buttons e.g. previous month and next month, to send a variable to my MakeCalendar() and MakeFullCalendar() functions e.g. -1 for previous months and +1 for next month.
These are my functions...
public function MakeCalendar(/*$monthNumber*/)
{
// $month = $monthNumber;
$month = date("m");
$year = date("Y");
$num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$dates_month=array();
for($i=1;$i<=$num;$i++)
{
$mktime=mktime(0,0,0,$month,$i,$year);
$date=date("Y-m-d",$mktime);
$dates_month[$i]=$date;
}
return $dates_month;
}
public function PopulateDatesWithReports($parkId = null)
{
$completeCalendar = array();
$calendar = $this->MakeCalendar();
$reports = DB::table('ReportRecord')
->where('ParkId','=', $parkId)
->orderBy('DateCompleted', 'ASC')
->get();
foreach ($calendar as $key => $date)
{
foreach ($reports as $report)
{
if ($report->DateCompleted == $date)
{
$completeCalendar[$date][] = $report->ReportNameId;
}
}
}
return $completeCalendar;
}
public function MakeFullCalendar(/*$monthNumber,*/$parkId = null)
{
$reportdates = $this->PopulateDatesWithReports($parkId);
$reportcounter = 0;
$weekreportcounter = 0;
$monthreportcounter = 0;
$weeklyreports = array();
$monthlyreports = array();
$month = date("m");
$year = date("Y");
$reports = DB::table('ReportRecord')
->where('ParkId','=', $parkId)
->orderBy('DateCompleted', 'ASC')
->get();
$num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$dates_month_with_reports=array();
for($i=1;$i<=$num;$i++)
{
$mktime=mktime(0,0,0,$month,$i,$year);
$date=date("Y-m-d",$mktime);
$dates_month_with_reports[$date]=$date;
}
foreach ($dates_month_with_reports as $cal)
{
foreach ($reportdates as $key => $rdate)
{
if ($key == $cal)
{
$dates_month_with_reports[$cal] = $rdate;
}
}
}
$daycounter = 0;
foreach ($dates_month_with_reports as $key => $value)
{
$daycounter++;
if (! is_array($value))
{
$dates_month_with_reports[$key] = "No reports done today";
}
else {
foreach ($value as $int => $val)
{
if ($val == 1 || $val == 3 || $val == 4 || $val == 5)
{
$reportcounter++;
if ($reportcounter >= 4)
{
$dates_month_with_reports[$key] = "Daily Reports are done";
}
else {
$dates_month_with_reports[$key] = "Daily Repoorts are missing";
}
}
elseif ($val == 41 || $val == 10 || $val == 9 || $val == 8 || $val == 7 || $val == 6 || $val == 2)
{
$weeklyreports[$key][] = $val;
}
elseif($val == 42 || $val == 22 || $val == 21 || $val == 20 || $val == 18 || $val == 17 || $val == 16 || $val == 15 || $val == 14 || $val == 13 || $val == 12 )
{
$monthlyreports[$key][] = $val;
}
}
}
if ($daycounter == 7||$daycounter == 14||$daycounter == 21||$daycounter == 28)
{
foreach ($weeklyreports as $index => $val)
{
foreach ($val as $va)
{
$weekreportcounter++;
}
}
if ($weekreportcounter >= 7)
{
$dates_month_with_reports[$key] = "Weekly Reports done";
}
else {
$dates_month_with_reports[$key] = "Weekly Reports Not done they only achieved ".$weekreportcounter."/7";
}
}
if ($daycounter == count($dates_month_with_reports))
{
foreach ($monthlyreports as $index => $val)
{
foreach ($val as $va)
{
$monthreportcounter++;
}
}
if ($monthreportcounter >= 11)
{
$dates_month_with_reports[$key] = "Monthly Reports done";
}
else {
$dates_month_with_reports[$key] = "Monthly Reports Not done they only achieved ".$monthreportcounter."/11";
}
}
}
return $dates_month_with_reports;
}
I'm using laravel 4, and I know I need to send parameters to both MakeCalendar(), and MakeFullCalnedar() using a href input, to increment a month.
So my question is what ways can I send data to the functions that don't involve using routes, or is it the only way?
You can call the MakeCalendar function from within the PopulateDatesWithReports function, sending the $monthNumber as a paramater like this,
$this->MakeCalendar($monthNumber);
You can do this at any point as long as you're sending the $monthNumber parameter to the PopulateDatesWithReports function.
So your href would be something like
http://www.example.com/calender/MakeFullCalendar/1/99
I want to remove some duplicate values on an array, but there is a condition that the script has to ignore the array that contains a specific word.
Below code is adapted from PHP: in_array.
$array = array( 'STK0000100001',
'STK0000100002',
'STK0000100001', //--> This should be remove
'STK0000100001-XXXX', //--> This should be ignored
'STK0000100001-XXXX' ); //--> This should be ignored
$ignore_values = array('-XXXX');
if(make_unique($array, $ignore_values) > 0) {
//ERROR HERE
}
The function to make the array unique is:
function make_unique($array, $ignore) {
$i = 0;
while($values = each($array)) {
if(!in_array($values[1], $ignore)) {
$dupes = array_keys($array, $values[1]);
unset($dupes[0]);
foreach($dupes as $rmv) {
$i++;
}
}
}
return $i;
}
I have tried to use if(!in_array(str_split($values[1]), $ignore)) ... but it just the same.
The array should become like:
STK0000100001
STK0000100002
STK0000100001-XXXX
STK0000100001-XXXX
How to do that?
Try this one, just remove the print_r(); inside the function when using in production
if(make_unique($array, $ignore_values) > 0) {
//ERROR HERE
}
function make_unique($array, $ignore) {
$array_hold = $array;
$ignore_val = array();
$i = 0;
foreach($array as $arr) {
foreach($ignore as $ign) {
if(strpos($arr, $ign)) {
array_push( $ignore_val, $arr);
unset($array_hold[$i]);
break;
}
}
$i++;
}
$unique_one = (array_unique($array_hold));
$unique_one = array_merge($unique_one,$ignore_val);
print_r($unique_one);
return count($array) - count($unique_one);
}
This should work for >= PHP 5.3.
$res = array_reduce($array, function ($res, $val) use ($ignore_values) {
$can_ignore = false;
foreach ($ignore_values as $ignore_val) {
if (substr($val, 0 - strlen($ignore_val)) == $ignore_val) {
$can_ignore = true;
break;
}
}
if ( $can_ignore || ! in_array($val, $res)) {
$res[] = $val;
}
return $res;
}, array()
);
Otherwise
$num_of_duplicates = 0;
$res = array();
foreach ($array as $val) {
$can_ignore = false;
foreach ($ignore_values as $ignore_val) {
if (substr($val, 0 - strlen($ignore_val)) == $ignore_val) {
$num_of_duplicates++;
$can_ignore = true;
break;
}
}
if ( $can_ignore || ! in_array($val, $res)) {
$res[] = $val;
}
}
Edit: Added duplicate count to the second snippet.
There is an array:
$bounds = array([0]=>array('lower'=>2,'upper'=>5),
[1]=>array('lower'=>0,'upper'=>3));
and a variable:
$val = 4;
Is there any PHP function that can say whether $val belongs to any interval defined by 'lower' and 'upper' bounds in $bounds array? In this example 4 belongs to the 1st interval [2; 5]. So, the answer should be 'true'.
I don't think there is a built-in function to do this.
However, you can do it with a foreach statement:
function check_interval($bounds, $val) {
foreach ($bounds as $array) {
if($array['lower'] <= $val && $array['upper'] >= $val)
return true;
}
return false;
}
I'm not aware of any. You'll probably have to code it. Something like this will do:
function isFromInterval($bounds, $val) {
foreach ($bounds as $value) {
if ($val >= $value['lower'] && $val <= $value['upper']) {
return true;
}
}
return false;
}
No.
You would have to make a loop for the array like this
$val = 4;
$key_id = FALSE;
foreach($bounds as $key => $data){
if($val <= $data['upper'] AND $val >= $data['lower']){
$key_id = $key;
break;
}
}
if($key_id !== FALSE){
// found something
// $bounds[$key_id] is your result in the array
} else {
// found nothing
}
As a function
function find_range($bounds=array(), $val=0, $return_key=TRUE){
if(is_array($bounds) === FALSE){
$bounds = array();
}
if(is_numeric($val) === FALSE){
$val = 0;
}
if(is_bool($return_key) === FALSE){
$return_key = TRUE;
}
$key_id = FALSE;
foreach($bounds as $key => $data){
if($val < $data['upper'] AND $val > $data['lower']){
$key_id = $key;
break;
}
}
if($key_id !== FALSE){
return ($return_key === TRUE ? $key_id : TRUE);
} else {
return FALSE;
}
}
No, but you can do:
$bounds = array(3=>array('lower'=>2,'upper'=>5),
4=>array('lower'=>0,'upper'=>3));
$val = 4;
foreach($bounds as $num => $bound){
if(max($bound) >= $val && $val >= min($bound)){
echo $num;
}
}