Laravel Request does not return result - php
I am using AWS and laravel 5.1.
I have two issues:
Most of the time API returns the expected result but sometimes server
doesn't return result for longer time. It might be due to the server
load(Although we don't have too much load on server).
Clients wait for few minutes and after that application crashes. Any
Advice.
If Client made an API request and before getting result, if device
loses internet connection. Server has process the data but unable to
deliver it to the client. how to handle this (Both in get as well as
post request)
Sample Code for first Part
public function psychometricResponses(Request $request)
{
$questionId=$request->get('questionId');
$answerId=$request->get('answerId');
$from = $request->has('from');
$userId = $request->has('id') ? $request->get('id') : 0;
$name = $request->has('name') ? $request->get('name') : '';
$email = $request->has('email') ? $request->get('email') : '';
$gender = $request->has('gender') ? $request->get('gender') : '';
$relationshipStatus = $request->has('relationshipStatus') ? $request->get('relationshipStatus') : '';
if($userId!=0){
$status = 1;
User::where('id',$userId)->update(['status'=>1]);
}else{
$status = 0;
}
$id = $this->calculatePersonality($questionId, $answerId, $userId, $from, $name, $email, $gender, $relationshipStatus);
return json_encode(['code'=>200,'message'=>"success",'response'=>array('personality'=>$this->_getPersonality($id),'psychometricId'=>$id,'status'=>$status)]);
}
public function calculatePersonality($questionId, $answerId, $userId, $from, $name, $email, $gender, $relationshipStatus)
{
//
$questionArray = explode(',', $questionId); //arraySize is 60
$answerArray = explode(',', $answerId); // //arraySize is 60
$this->questionAll = $this->_questionAll();
$personality = array('O'=>0,'C'=>0,'E'=>0,'A'=>0,'N'=>0);
for($i=0; $i<sizeof($answerArray);$i++){
$type = $this->_getQuestionType($questionArray[$i]);
if($type=='O'){
$personality['O'] = $answerArray[$i]=='1' ? $personality['O']+1 : $personality['O']-1;
}else if($type=='OR'){
$personality['O'] =$answerArray[$i]=='1' ? $personality['O']-1 : $personality['O']+1;
}else if($type=='C'){
$personality['C'] = $answerArray[$i]=='1' ? $personality['C']+1 : $personality['C']-1;
}else if($type=='CR'){
$personality['C'] = $answerArray[$i]=='1' ? $personality['C']-1 : $personality['C']+1;
}else if($type=='E'){
$personality['E'] = $answerArray[$i]=='1' ? $personality['E']+1 : $personality['E']-1;
}else if($type=='ER'){
$personality['E'] = $answerArray[$i]=='1'?$personality['E']-1 : $personality['E']+1;
}else if($type=='A'){
$personality['A'] = $answerArray[$i]=='1' ? $personality['A']+1 : $personality['A']-1;
}else if($type=='AR'){
$personality['A'] = $answerArray[$i]=='1' ? $personality['A']-1 : $personality['A']+1;
}else if($type=='N'){
$personality['N'] = $answerArray[$i]=='1'? $personality['N']+1 : $personality['N']-1;
}else if($type=='NR'){
$personality['N'] = $answerArray[$i]=='1' ? $personality['N']-1 : $personality['N']+1;
}
}
$mod = array();
$mod['O'] = abs($personality['O']);
$mod['C'] = abs($personality['C']);
$mod['E'] = abs($personality['E']);
$mod['A'] = abs($personality['A']);
$mod['N'] = abs($personality['N']);
arsort($mod);
$values = array_keys($mod);
$pTypeOne = $personality[$values[0]] < 0 ? $values[0].'R' : $values[0];
$pTypeTwo = $personality[$values[1]] < 0 ? $values[1].'R' : $values[1];
$pTypeThree = $personality[$values[2]] < 0 ? $values[2].'R' : $values[2];
$pTypeFour = $personality[$values[3]] < 0 ? $values[3].'R' : $values[3];
$pTypeFive = $personality[$values[4]] < 0 ? $values[4].'R' : $values[4];
$psychometricResponse = PsychometricResponse::create(array('typeA'=>$pTypeOne, 'valueA'=>$personality[$values[0]],'typeB'=>$pTypeTwo, 'valueB'=>$personality[$values[1]],
'typeC'=>$pTypeThree, 'valueC'=>$personality[$values[2]],'typeD'=>$pTypeFour, 'valueD'=>$personality[$values[3]],
'typeE'=>$pTypeFive, 'valueE'=>$personality[$values[4]],'questionId'=>$questionId,'answerId'=>$answerId, 'userId'=>$userId, 'from'=>$from,'name'=>$name,'email'=>$email, 'gender'=>$gender,'relationshipStatus'=>$relationshipStatus));
return $psychometricResponse->id;
}
private function _questionAll(){
$question = PsychometricQuestion::where('isActive',1)->get(['id','type']);
$result =array();
for($i=0;$i<sizeof($question);$i++){
$result[$question[$i]->id] = $question[$i]->type;
}
return $result;
}
private function _getQuestionType($id){
return $this->questionAll[$id];
}
private function _getPersonality($id){
$personality = PsychometricResponse::where('id',$id)->first(['typeA','typeB','typeC','typeD','typeE','valueA','valueB']);
$combined = array($personality->typeA,$personality->typeB);
sort($combined);
$combinedPersonality = PsychometricResult::where('type',$combined[0]."-".$combined[1])->first(['type','keyword','description','colorCode']); //Table PsychometricResult has 40 rows only.
$primaryPersonality = PsychometricResult::where('type',$personality->typeA)->first(['type','keyword','description','colorCode']);
$secondaryPersonality = PsychometricResult::where('type',$personality->typeB)->first(['type','keyword','description','colorCode']);
$permutation = $this->_getAllPermutation($personality);
$stylePersonality = PsychometricStyle::whereIn('type',$permutation)->get(['type','aspect','style','description']);
$primaryPercentage = round(abs($personality->valueA)/(abs($personality->valueA)+abs($personality->valueB)), 2);
$secondaryPercentage = 1 - $primaryPercentage;
return array('primaryPersonality'=>$primaryPersonality,'secondaryPersonality'=>$secondaryPersonality,'combinedPersonality'=>$combinedPersonality,
'styles'=>$stylePersonality,'primaryPercentage'=>$primaryPercentage,'secondaryPercentage'=>$secondaryPercentage);
}
private function _getAllPermutation($personality){
$combined = array($personality->typeA,$personality->typeB,$personality->typeC,$personality->typeD,$personality->typeE);
sort($combined);
$permutation = array();
for($i=0;$i<sizeof($combined);$i++){
for($j=$i+1;$j<5;$j++){
array_push($permutation,$combined[$i]."-".$combined[$j]);
}
}
return $permutation;
}
Related
POST API Issue - Not functioning
Good afternoon, I'm having trouble with an api POST request - it simply isn't going anywhere, it's not showing on the API log at all - I've got various get requests which work fine but this one just doesn't - I'm unsure what I am doing wrong. public function broadband_plan(){ $common = array(); $common['main_menu'] = 'Broadband plan'; $cli_or_postcode = isset($_GET["cli_or_postcode"]) ? $_GET["cli_or_postcode"] : ""; $district_id = isset($_GET["district_id"]) ? $_GET["district_id"] : ""; $sub_premises = isset($_GET["sub_premises"]) ? $_GET["sub_premises"] : ""; $premises_name = isset($_GET["premises_name"]) ? $_GET["premises_name"] : ""; $thoroughfare_number = isset($_GET["thoroughfare_number"]) ? $_GET["thoroughfare_number"] : ""; $thoroughfare_name = isset($_GET["thoroughfare_name"]) ? $_GET["thoroughfare_name"] : ""; $locality = isset($_GET["locality"]) ? $_GET["locality"] : ""; $postcode = isset($_GET["postcode"]) ? $_GET["postcode"] : ""; $nad_key = isset($_GET["nad_key"]) ? $_GET["nad_key"] : ""; $post_town = isset($_GET["post_town"]) ? $_GET["post_town"] : ""; $county = isset($_GET["county"]) ? $_GET["county"] : ""; $district_id = isset($_GET["district_id"]) ? $_GET["district_id"] : ""; $request = array(); $request['apiUsername'] = 'XXXXXXX'; $request['apiPassword'] = 'XXXXXXX'; $request['encryption'] = 'SHA-512'; $request['platform'] = 'LIVE'; $request['method'] = 'POST'; $address = '{ "sub_premises": $sub_premises, "premises_name": $premises_name, "thoroughfare_number": $thoroughfare_number, "thoroughfare_name": $thoroughfare_name, "post_town": $post_town, "postcode": $postcode, "district_id": $district_id, "nad_key": $nad_key }'; $address = json_encode($address); $request['url'] = "/broadband/availability" . $address; $broadband_plan_detail = array(); if (!empty($address)) { $broadband_plan_detail = $this->get_plan($request); } return view('front.broadband_plan',compact('common','broadband_plan_detail')); }
The $address after $address = json_encode($address); will be an object, it cannot be concatenated as a string in $request['url'] = "/broadband/availability" . $address; Please check your API URL and ensure you follow their structure, for example if the API URL accept the parameters as GET request, you can pass the parameters likes: $request['url'] = "/broadband/availability?sub_premises=" . $sub_premises . "&premises_name=" . $premises_name... Base on your code, the API method is POST, so you might need to prepare the parameters as an array.
PHP Warning: number_format() expects parameter 1 to be float [duplicate]
This question already has answers here: Warning: number_format() expects parameter 1 to be double, string given [closed] (3 answers) Closed 5 months ago. Can you please someone help me with this code I got PHP Warning: number_format() expects parameter 1 to be float, string given in line 39 I am copied the whole class I hope that will help you better to find the problem. class stats_site extends stats { function __construct() { global $CONF, $DB, $FORM, $LNG, $TMPL; $TMPL['header'] = $LNG['stats_header']; $TMPL['username'] = $DB->escape($FORM['u'], 1); $stats = $DB->fetch("SELECT * FROM {$CONF['sql_prefix']}_stats WHERE username = '{$TMPL['username']}'", __FILE__, __LINE__); unset($stats['username']); $sites = array($DB->fetch("SELECT * FROM {$CONF['sql_prefix']}_sites WHERE username = '{$TMPL['username']}'", __FILE__, __LINE__)); if ($stats) { $TMPL = array_merge($TMPL, $stats, $sites); $TMPL['unq_pv_max_daily'] = $TMPL['unq_pv_0_daily'] > $TMPL['unq_pv_max_daily'] ? $TMPL['unq_pv_0_daily'] : $TMPL['unq_pv_max_daily']; $TMPL['tot_pv_max_daily'] = $TMPL['tot_pv_0_daily'] > $TMPL['tot_pv_max_daily'] ? $TMPL['tot_pv_0_daily'] : $TMPL['tot_pv_max_daily']; $TMPL['unq_in_max_daily'] = $TMPL['unq_in_0_daily'] > $TMPL['unq_in_max_daily'] ? $TMPL['unq_in_0_daily'] : $TMPL['unq_in_max_daily']; $TMPL['tot_in_max_daily'] = $TMPL['tot_in_0_daily'] > $TMPL['tot_in_max_daily'] ? $TMPL['tot_in_0_daily'] : $TMPL['tot_in_max_daily']; $TMPL['unq_out_max_daily'] = $TMPL['unq_out_0_daily'] > $TMPL['unq_out_max_daily'] ? $TMPL['unq_out_0_daily'] : $TMPL['unq_out_max_daily']; $TMPL['tot_out_max_daily'] = $TMPL['tot_out_0_daily'] > $TMPL['tot_out_max_daily'] ? $TMPL['tot_out_0_daily'] : $TMPL['tot_out_max_daily']; $TMPL['unq_pv_max_weekly'] = $TMPL['unq_pv_0_weekly'] > $TMPL['unq_pv_max_weekly'] ? $TMPL['unq_pv_0_weekly'] : $TMPL['unq_pv_max_weekly']; $TMPL['tot_pv_max_weekly'] = $TMPL['tot_pv_0_weekly'] > $TMPL['tot_pv_max_weekly'] ? $TMPL['tot_pv_0_weekly'] : $TMPL['tot_pv_max_weekly']; $TMPL['unq_in_max_weekly'] = $TMPL['unq_in_0_weekly'] > $TMPL['unq_in_max_weekly'] ? $TMPL['unq_in_0_weekly'] : $TMPL['unq_in_max_weekly']; $TMPL['tot_in_max_weekly'] = $TMPL['tot_in_0_weekly'] > $TMPL['tot_in_max_weekly'] ? $TMPL['tot_in_0_weekly'] : $TMPL['tot_in_max_weekly']; $TMPL['unq_out_max_weekly'] = $TMPL['unq_out_0_weekly'] > $TMPL['unq_out_max_weekly'] ? $TMPL['unq_out_0_weekly'] : $TMPL['unq_out_max_weekly']; $TMPL['tot_out_max_weekly'] = $TMPL['tot_out_0_weekly'] > $TMPL['tot_out_max_weekly'] ? $TMPL['tot_out_0_weekly'] : $TMPL['tot_out_max_weekly']; $TMPL['unq_pv_max_monthly'] = $TMPL['unq_pv_0_monthly'] > $TMPL['unq_pv_max_monthly'] ? $TMPL['unq_pv_0_monthly'] : $TMPL['unq_pv_max_monthly']; $TMPL['tot_pv_max_monthly'] = $TMPL['tot_pv_0_monthly'] > $TMPL['tot_pv_max_monthly'] ? $TMPL['tot_pv_0_monthly'] : $TMPL['tot_pv_max_monthly']; $TMPL['unq_in_max_monthly'] = $TMPL['unq_in_0_monthly'] > $TMPL['unq_in_max_monthly'] ? $TMPL['unq_in_0_monthly'] : $TMPL['unq_in_max_monthly']; $TMPL['tot_in_max_monthly'] = $TMPL['tot_in_0_monthly'] > $TMPL['tot_in_max_monthly'] ? $TMPL['tot_in_0_monthly'] : $TMPL['tot_in_max_monthly']; $TMPL['unq_out_max_monthly'] = $TMPL['unq_out_0_monthly'] > $TMPL['unq_out_max_monthly'] ? $TMPL['unq_out_0_monthly'] : $TMPL['unq_out_max_monthly']; $TMPL['tot_out_max_monthly'] = $TMPL['tot_out_0_monthly'] > $TMPL['tot_out_max_monthly'] ? $TMPL['tot_out_0_monthly'] : $TMPL['tot_out_max_monthly']; $this->averages(); $TMPL['average_rating'] = $TMPL['num_ratings'] > 0 ? round($TMPL['total_rating'] / $TMPL['num_ratings'], 0) : 0; $stats = array_map('number_format', $stats); $TMPL = array_merge($TMPL, $stats); $this->locale(); $TMPL['header'] .= " - {$TMPL['title']}"; $TMPL['category_url'] = urlencode($TMPL['category']); $query = "SELECT id, date, review FROM {$CONF['sql_prefix']}_reviews WHERE username = '{$TMPL['username']}' AND active = 1"; if (isset($FORM['all_reviews']) && $FORM['all_reviews']) { $result = $DB->query("{$query} ORDER BY date DESC", __FILE__, __LINE__); } else { $result = $DB->select_limit("{$query} ORDER BY RAND()", 2, 0, __FILE__, __LINE__); } $TMPL['reviews'] = ''; while (list($TMPL['id'], $TMPL['date'], $TMPL['review']) = $DB->fetch_array($result)) { $TMPL['reviews'] .= $this->do_skin('stats_review'); } $TMPL['content'] = $this->do_skin('stats'); } else { $this->error($LNG['g_invalid_u']); } } } And line 39 is this line here: $stats = array_map('number_format', $stats); Many thanks in advance! EDIT I've put here THE FULL CODE so you can better fix the problem with it. I tried everything you advice here but it doesn't help :( Here is the full code: <?php if (!defined('ATSPHP')) { die("This file cannot be accessed directly."); } class stats extends base { function __construct() { global $FORM; if (isset($FORM['u'])) { $stats = new stats_site; } else { $stats = new stats_overall; } } function averages() { global $TMPL; $ranking_periods = array('daily', 'weekly', 'monthly'); $ranking_methods = array('unq_pv', 'tot_pv', 'unq_in', 'tot_in', 'unq_out', 'tot_out'); foreach ($ranking_periods as $ranking_period) { foreach ($ranking_methods as $ranking_method) { $TMPL["{$ranking_method}_avg_{$ranking_period}"] = 0; for ($i = 0; $i < 10; $i++) { $TMPL["{$ranking_method}_avg_{$ranking_period}"] = $TMPL["{$ranking_method}_avg_{$ranking_period}"] + $TMPL["{$ranking_method}_{$i}_{$ranking_period}"]; } $TMPL["{$ranking_method}_avg_{$ranking_period}"] = number_format($TMPL["{$ranking_method}_avg_{$ranking_period}"] / 10, 1); } } } function locale() { global $CONF, $LNG, $TMPL; setlocale(LC_ALL, $CONF['default_language']); for ($i = 2; $i < 10; $i++) { $TMPL["{$i}_daily"] = strftime('%B %d', time()-3600*24*$i + (3600*$CONF['time_offset'])); } for ($i = 2; $i < 10; $i++) { $TMPL["{$i}_weekly"] = "{$LNG['stats_week']} ".date('W', time()-3600*24*7*$i + (3600*$CONF['time_offset'])); } for ($i = 2; $i < 10; $i++) { $TMPL["{$i}_monthly"] = strftime('%B %y', mktime(0, 0, 0, date('m')-$i, 1)); } } } class stats_site extends stats { function __construct() { global $CONF, $DB, $FORM, $LNG, $TMPL; $TMPL['header'] = $LNG['stats_header']; $TMPL['username'] = $DB->escape($FORM['u'], 1); $stats = $DB->fetch("SELECT * FROM {$CONF['sql_prefix']}_stats WHERE username = '{$TMPL['username']}'", __FILE__, __LINE__); unset($stats['username']); $sites = array($DB->fetch("SELECT * FROM {$CONF['sql_prefix']}_sites WHERE username = '{$TMPL['username']}'", __FILE__, __LINE__)); if ($stats) { $TMPL = array_merge($TMPL, $stats, $sites); $TMPL['unq_pv_max_daily'] = $TMPL['unq_pv_0_daily'] > $TMPL['unq_pv_max_daily'] ? $TMPL['unq_pv_0_daily'] : $TMPL['unq_pv_max_daily']; $TMPL['tot_pv_max_daily'] = $TMPL['tot_pv_0_daily'] > $TMPL['tot_pv_max_daily'] ? $TMPL['tot_pv_0_daily'] : $TMPL['tot_pv_max_daily']; $TMPL['unq_in_max_daily'] = $TMPL['unq_in_0_daily'] > $TMPL['unq_in_max_daily'] ? $TMPL['unq_in_0_daily'] : $TMPL['unq_in_max_daily']; $TMPL['tot_in_max_daily'] = $TMPL['tot_in_0_daily'] > $TMPL['tot_in_max_daily'] ? $TMPL['tot_in_0_daily'] : $TMPL['tot_in_max_daily']; $TMPL['unq_out_max_daily'] = $TMPL['unq_out_0_daily'] > $TMPL['unq_out_max_daily'] ? $TMPL['unq_out_0_daily'] : $TMPL['unq_out_max_daily']; $TMPL['tot_out_max_daily'] = $TMPL['tot_out_0_daily'] > $TMPL['tot_out_max_daily'] ? $TMPL['tot_out_0_daily'] : $TMPL['tot_out_max_daily']; $TMPL['unq_pv_max_weekly'] = $TMPL['unq_pv_0_weekly'] > $TMPL['unq_pv_max_weekly'] ? $TMPL['unq_pv_0_weekly'] : $TMPL['unq_pv_max_weekly']; $TMPL['tot_pv_max_weekly'] = $TMPL['tot_pv_0_weekly'] > $TMPL['tot_pv_max_weekly'] ? $TMPL['tot_pv_0_weekly'] : $TMPL['tot_pv_max_weekly']; $TMPL['unq_in_max_weekly'] = $TMPL['unq_in_0_weekly'] > $TMPL['unq_in_max_weekly'] ? $TMPL['unq_in_0_weekly'] : $TMPL['unq_in_max_weekly']; $TMPL['tot_in_max_weekly'] = $TMPL['tot_in_0_weekly'] > $TMPL['tot_in_max_weekly'] ? $TMPL['tot_in_0_weekly'] : $TMPL['tot_in_max_weekly']; $TMPL['unq_out_max_weekly'] = $TMPL['unq_out_0_weekly'] > $TMPL['unq_out_max_weekly'] ? $TMPL['unq_out_0_weekly'] : $TMPL['unq_out_max_weekly']; $TMPL['tot_out_max_weekly'] = $TMPL['tot_out_0_weekly'] > $TMPL['tot_out_max_weekly'] ? $TMPL['tot_out_0_weekly'] : $TMPL['tot_out_max_weekly']; $TMPL['unq_pv_max_monthly'] = $TMPL['unq_pv_0_monthly'] > $TMPL['unq_pv_max_monthly'] ? $TMPL['unq_pv_0_monthly'] : $TMPL['unq_pv_max_monthly']; $TMPL['tot_pv_max_monthly'] = $TMPL['tot_pv_0_monthly'] > $TMPL['tot_pv_max_monthly'] ? $TMPL['tot_pv_0_monthly'] : $TMPL['tot_pv_max_monthly']; $TMPL['unq_in_max_monthly'] = $TMPL['unq_in_0_monthly'] > $TMPL['unq_in_max_monthly'] ? $TMPL['unq_in_0_monthly'] : $TMPL['unq_in_max_monthly']; $TMPL['tot_in_max_monthly'] = $TMPL['tot_in_0_monthly'] > $TMPL['tot_in_max_monthly'] ? $TMPL['tot_in_0_monthly'] : $TMPL['tot_in_max_monthly']; $TMPL['unq_out_max_monthly'] = $TMPL['unq_out_0_monthly'] > $TMPL['unq_out_max_monthly'] ? $TMPL['unq_out_0_monthly'] : $TMPL['unq_out_max_monthly']; $TMPL['tot_out_max_monthly'] = $TMPL['tot_out_0_monthly'] > $TMPL['tot_out_max_monthly'] ? $TMPL['tot_out_0_monthly'] : $TMPL['tot_out_max_monthly']; $this->averages(); $TMPL['average_rating'] = $TMPL['num_ratings'] > 0 ? round($TMPL['total_rating'] / $TMPL['num_ratings'], 0) : 0; function formatter($stats) { return number_format(floatval($stats)); } $stats = array_map('number_format', $stats); //$stats = array_map(function($v){return is_numeric($v) ? number_format($v) : $v;}, $stats); $TMPL = array_merge($TMPL, $stats); $this->locale(); $TMPL['header'] .= " - {$TMPL['title']}"; $TMPL['category_url'] = urlencode($TMPL['category']); $query = "SELECT id, date, review FROM {$CONF['sql_prefix']}_reviews WHERE username = '{$TMPL['username']}' AND active = 1"; if (isset($FORM['all_reviews']) && $FORM['all_reviews']) { $result = $DB->query("{$query} ORDER BY date DESC", __FILE__, __LINE__); } else { $result = $DB->select_limit("{$query} ORDER BY RAND()", 2, 0, __FILE__, __LINE__); } $TMPL['reviews'] = ''; while (list($TMPL['id'], $TMPL['date'], $TMPL['review']) = $DB->fetch_array($result)) { $TMPL['reviews'] .= $this->do_skin('stats_review'); } $TMPL['content'] = $this->do_skin('stats'); } else { $this->error($LNG['g_invalid_u']); } } } class stats_overall extends stats { function __construct() { global $CONF, $DB, $FORM, $LNG, $TMPL; $TMPL['header'] = $LNG['stats_overall']; $stats = $DB->fetch("SELECT SUM(unq_pv_overall), SUM(tot_pv_overall), SUM(unq_in_overall), SUM(tot_in_overall), SUM(unq_out_overall), SUM(tot_out_overall), SUM(unq_pv_0_daily), SUM(unq_pv_1_daily), SUM(unq_pv_2_daily), SUM(unq_pv_3_daily), SUM(unq_pv_4_daily), SUM(unq_pv_5_daily), SUM(unq_pv_6_daily), SUM(unq_pv_7_daily), SUM(unq_pv_8_daily), SUM(unq_pv_9_daily), SUM(tot_pv_0_daily), SUM(tot_pv_1_daily), SUM(tot_pv_2_daily), SUM(tot_pv_3_daily), SUM(tot_pv_4_daily), SUM(tot_pv_5_daily), SUM(tot_pv_6_daily), SUM(tot_pv_7_daily), SUM(tot_pv_8_daily), SUM(tot_pv_9_daily), SUM(unq_in_0_daily), SUM(unq_in_1_daily), SUM(unq_in_2_daily), SUM(unq_in_3_daily), SUM(unq_in_4_daily), SUM(unq_in_5_daily), SUM(unq_in_6_daily), SUM(unq_in_7_daily), SUM(unq_in_8_daily), SUM(unq_in_9_daily), SUM(tot_in_0_daily), SUM(tot_in_1_daily), SUM(tot_in_2_daily), SUM(tot_in_3_daily), SUM(tot_in_4_daily), SUM(tot_in_5_daily), SUM(tot_in_6_daily), SUM(tot_in_7_daily), SUM(tot_in_8_daily), SUM(tot_in_9_daily), SUM(unq_out_0_daily), SUM(unq_out_1_daily), SUM(unq_out_2_daily), SUM(unq_out_3_daily), SUM(unq_out_4_daily), SUM(unq_out_5_daily), SUM(unq_out_6_daily), SUM(unq_out_7_daily), SUM(unq_out_8_daily), SUM(unq_out_9_daily), SUM(tot_out_0_daily), SUM(tot_out_1_daily), SUM(tot_out_2_daily), SUM(tot_out_3_daily), SUM(tot_out_4_daily), SUM(tot_out_5_daily), SUM(tot_out_6_daily), SUM(tot_out_7_daily), SUM(tot_out_8_daily), SUM(tot_out_9_daily), SUM(unq_pv_0_weekly), SUM(unq_pv_1_weekly), SUM(unq_pv_2_weekly), SUM(unq_pv_3_weekly), SUM(unq_pv_4_weekly), SUM(unq_pv_5_weekly), SUM(unq_pv_6_weekly), SUM(unq_pv_7_weekly), SUM(unq_pv_8_weekly), SUM(unq_pv_9_weekly), SUM(tot_pv_0_weekly), SUM(tot_pv_1_weekly), SUM(tot_pv_2_weekly), SUM(tot_pv_3_weekly), SUM(tot_pv_4_weekly), SUM(tot_pv_5_weekly), SUM(tot_pv_6_weekly), SUM(tot_pv_7_weekly), SUM(tot_pv_8_weekly), SUM(tot_pv_9_weekly), SUM(unq_in_0_weekly), SUM(unq_in_1_weekly), SUM(unq_in_2_weekly), SUM(unq_in_3_weekly), SUM(unq_in_4_weekly), SUM(unq_in_5_weekly), SUM(unq_in_6_weekly), SUM(unq_in_7_weekly), SUM(unq_in_8_weekly), SUM(unq_in_9_weekly), SUM(tot_in_0_weekly), SUM(tot_in_1_weekly), SUM(tot_in_2_weekly), SUM(tot_in_3_weekly), SUM(tot_in_4_weekly), SUM(tot_in_5_weekly), SUM(tot_in_6_weekly), SUM(tot_in_7_weekly), SUM(tot_in_8_weekly), SUM(tot_in_9_weekly), SUM(unq_out_0_weekly), SUM(unq_out_1_weekly), SUM(unq_out_2_weekly), SUM(unq_out_3_weekly), SUM(unq_out_4_weekly), SUM(unq_out_5_weekly), SUM(unq_out_6_weekly), SUM(unq_out_7_weekly), SUM(unq_out_8_weekly), SUM(unq_out_9_weekly), SUM(tot_out_0_weekly), SUM(tot_out_1_weekly), SUM(tot_out_2_weekly), SUM(tot_out_3_weekly), SUM(tot_out_4_weekly), SUM(tot_out_5_weekly), SUM(tot_out_6_weekly), SUM(tot_out_7_weekly), SUM(tot_out_8_weekly), SUM(tot_out_9_weekly), SUM(unq_pv_0_monthly), SUM(unq_pv_1_monthly), SUM(unq_pv_2_monthly), SUM(unq_pv_3_monthly), SUM(unq_pv_4_monthly), SUM(unq_pv_5_monthly), SUM(unq_pv_6_monthly), SUM(unq_pv_7_monthly), SUM(unq_pv_8_monthly), SUM(unq_pv_9_monthly), SUM(tot_pv_0_monthly), SUM(tot_pv_1_monthly), SUM(tot_pv_2_monthly), SUM(tot_pv_3_monthly), SUM(tot_pv_4_monthly), SUM(tot_pv_5_monthly), SUM(tot_pv_6_monthly), SUM(tot_pv_7_monthly), SUM(tot_pv_8_monthly), SUM(tot_pv_9_monthly), SUM(unq_in_0_monthly), SUM(unq_in_1_monthly), SUM(unq_in_2_monthly), SUM(unq_in_3_monthly), SUM(unq_in_4_monthly), SUM(unq_in_5_monthly), SUM(unq_in_6_monthly), SUM(unq_in_7_monthly), SUM(unq_in_8_monthly), SUM(unq_in_9_monthly), SUM(tot_in_0_monthly), SUM(tot_in_1_monthly), SUM(tot_in_2_monthly), SUM(tot_in_3_monthly), SUM(tot_in_4_monthly), SUM(tot_in_5_monthly), SUM(tot_in_6_monthly), SUM(tot_in_7_monthly), SUM(tot_in_8_monthly), SUM(tot_in_9_monthly), SUM(unq_out_0_monthly), SUM(unq_out_1_monthly), SUM(unq_out_2_monthly), SUM(unq_out_3_monthly), SUM(unq_out_4_monthly), SUM(unq_out_5_monthly), SUM(unq_out_6_monthly), SUM(unq_out_7_monthly), SUM(unq_out_8_monthly), SUM(unq_out_9_monthly), SUM(tot_out_0_monthly), SUM(tot_out_1_monthly), SUM(tot_out_2_monthly), SUM(tot_out_3_monthly), SUM(tot_out_4_monthly), SUM(tot_out_5_monthly), SUM(tot_out_6_monthly), SUM(tot_out_7_monthly), SUM(tot_out_8_monthly), SUM(tot_out_9_monthly) FROM {$CONF['sql_prefix']}_stats", __FILE__, __LINE__); // Get rid of SUM() in array keys foreach ($stats as $key => $value) { $new_key = str_replace(array('SUM(', ')'), '', $key); $stats[$new_key] = $value; unset($stats[$key]); } $TMPL = array_merge($TMPL, $stats); $this->averages(); $stats = array_map('number_format', $stats); $TMPL = array_merge($TMPL, $stats); $this->locale(); $TMPL['content'] = $this->do_skin('stats_overall'); } } ?> Error happen at line 85: $stats = array_map('number_format', $stats); Latest Error after using Miken32's Answer: PHP Notice: Undefined index: title in sources/stats.php on line 91 PHP Notice: Undefined index: category in sources/stats.php on line 92 PHP Notice: Undefined index: title in sources/misc/skin.php(84) : runtime-created function on line 1 PHP Notice: Undefined index: url in sources/misc/ etc.
array_map() applies a function to each element of the array. In this case you're passing it the name of a function ("number_format") that expects a number but it's getting a string. Instead, you can pass it your own function that can check it: $stats = array_map(function($v){return is_numeric($v) ? number_format($v) : $v;}, $stats);
By default numeric types from MySQL are returned as string values in php. The easiest thing to do would be create a custom function and pass that in: function formatter($value) { return number_format(floatval($value)); }
array or variable loses value when entering if/else statement
here is some of the code where I declared the values for $list. $list['first_name'] = Request::input("first_name"); $list['last_name'] = Request::input("last_name"); when I use dd($list), it shows the value like in my snipped image but when it enters this if else statement, if($existing_customer) { $customer_id = $existing_customer->customer_id; } else { dd($list); $insert_customer["first_name"] = ($list['first_name'] == "undefined" ? "John" : ucfirst($list['first_name'])); $insert_customer["last_name"] = ($list['last_name'] == "undefined" ? "Doe" : ucfirst($list['last_name'])); } this is the result. here is the photo of my function, if it could help. first second public function import_submit() { $data = Self::get_initial_settings(); /* INITIALIZE AND CAPTURE DATA */ $shop_id = $this->user_info->shop_id; $sponsor = Tbl_mlm_slot::where("slot_no", Request::input("sponsor"))->where("shop_id", $shop_id)->value("slot_id"); $placement = Tbl_mlm_slot::where("slot_no", Request::input("placement"))->where("shop_id", $shop_id)->value("slot_id"); /* POSITIONING DATA */ $slot_sponsor = $sponsor; $slot_placement = $placement; $slot_position = strtolower(Request::input("position")); /* SLOT GENERATION */ $membership_package_id = Request::input("package_number"); /* JUST ADD TO SLOT IF EXISTING CUSTOMER */ if(Request::input("date_created") == "undefined") { $slot_date_created = date("Y-m-d h:i"); } else { $slot_date_created = date("Y-m-d", strtotime(Request::input("date_created"))); } $existing_customer = Tbl_customer::where("shop_id", $shop_id)->where("email", Request::input("email"))->first(); $list['first_name'] = Request::input("first_name"); $list['last_name'] = Request::input("last_name"); $list['email'] = Request::input("email"); $list['slot_no'] = Request::input("slot_no"); $list['password'] = Request::input("password"); $list['birthday'] = Request::input("birthday"); $list['contact'] = Request::input("contact_number"); $list['gender'] = Request::input("gender"); dd($list); <---- first run of the function, I put one here if($existing_customer) { $customer_id = $existing_customer->customer_id; } else { dd($list); <--- for the second run of the function, to check what happens to my $list $insert_customer["shop_id"] = $shop_id; $insert_customer["first_name"] = ($list['first_name'] == "undefined" ? "John" : ucfirst($list['first_name'])); $insert_customer["last_name"] = ($list['last_name'] == "undefined" ? "Doe" : ucfirst($list['last_name'])); $insert_customer["email"] = ($list['email'] == "undefined" ? "dummy#gmail.com" : $list['email']); $insert_customer["ismlm"] = 1; $insert_customer["mlm_username"] = $list['slot_no']; $insert_customer["password"] = ($list['password'] == "undefined" ? Crypt::encrypt(randomPassword()) : Crypt::encrypt($list['password'])); $insert_customer["created_date"] = $slot_date_created; $insert_customer["b_day"] = date("Y-m-d", strtotime($list['birthday'])); $insert_customer["birthday"] = date("Y-m-d", strtotime($list['birthday'])); $insert_customer["contact"] = $list['contact']; $insert_customer["gender"] = strtolower($list['gender']); $customer_id = Tbl_customer::insertGetId($insert_customer); /* Insert Customer Address */ $address_purpose[0] = "permanent"; $address_purpose[1] = "billing"; $address_purpose[2] = "shipping"; foreach ($address_purpose as $key => $value) { $insert_customer_address["customer_id"] = $customer_id; $insert_customer_address["country_id"] = 420; $insert_customer_address["customer_state"] = ""; $insert_customer_address["customer_city"] = ""; $insert_customer_address["customer_zipcode"] = ""; $insert_customer_address["customer_street"] = Request::input("address"); $insert_customer_address["purpose"] = $value; $insert_customer_address["archived"] = 0; $insert_customer_address["created_at"] = Carbon::now(); $insert_customer_address["updated_at"] = Carbon::now(); Tbl_customer_address::insert($insert_customer_address); } } } I can't really see where I went wrong as the values went null just because they entered that if/else statement.
Issue with createQueryBuilder and MongoRegex
After several research for the createquerybuilder for mongodb, I cannot solve my issue.Can you please help me? Here is the structure of the data : “Field1” : { “Field2”: “value1a”,“Field3” : {“Field4” : “value2a”,”Field5” : “value3a”}} “Field1” : { “Field2”: “value1b”,“Field3” : {“Field4” : “value2b”,”Field5” : “value3b”}} “Field1” : { “Field2”: “value1c”,“Field3” : {“Field4” : “value2c”,”Field5” : “value3c”}} How can I get the whole row where Field5 = value3a ? I have proceeded with the below code but it doesn’t return me any record : $keywords = "test"; $keywords_Array = explode(',',$keywords); $nbKeyword = count($keywords_Array); $odm = $this->get("doctrine mongodb"); $donnees = $odm->createQueryBuilder("MyBundle:MyCollection"); for($i=0; $i<$nbKeyword; $i++){ $search = new \MongoRegex('/"Field5":"'.$ keywords_Array [$i].'"/'); $donnees = $donnees->field("Field1")->equals(new \MongoRegex('/"Field3":"'.$search.'"/')); } $donnees = $donnees->getQuery()->execute(); Foreach($donnees as $ data) { $resp = $data->getResult(); } Return $resp; And another structure of data (here, the data is an array of values): “Field1” : {“Field2”: [“value1”,”value2”,”value3”]} How can I get the whole row where Field2 = value3 ? It is the same as above with the below code, it doesn’t return me any record : $keywords = "test"; $keywords_Array = explode(',',$keywords); $nbKeyword = count($keywords_Array); $odm = $this->get("doctrine mongodb"); $donnees = $odm->createQueryBuilder("MyBundle:MyCollection"); for($i=0; $i<$nbKeyword; $i++){ $donnees = $donnees-> field("Field1")->equals(new \MongoRegex('/"Field2":"'. $keywords_Array [$i].'"/i')); } $donnees = $donnees->getQuery()->execute(); Foreach($donnees as $ data) { $resp = $data->getResult(); } Return $resp; Thanks in advance
The issue is fixed.I post the solution just in case anyone has also the same problem. For the first structure of data, the code should be like this : $keywords = "test"; $keywords_Array = explode(',',$keywords); $nbKeyword = count($keywords_Array); $odm = $this->get("doctrine mongodb"); $donnees = $odm->createQueryBuilder("MyBundle:MyCollection"); for($i=0; $i<$nbKeyword; $i++){ $donnees = $donnees->field('Field1')->equals(new \MongoRegex('/"Field5":'.$keywords_Array [$i].'/')); } $donnees = $donnees->getQuery()->execute(); Foreach($donnees as $ data) { $resp = $data->getResult(); } Return $resp; And for the second structure of data,the code should be like this : $keywords = "test"; $keywords_Array = explode(',',$keywords); $nbKeyword = count($keywords_Array); $odm = $this->get("doctrine mongodb"); $donnees = $odm->createQueryBuilder("MyBundle:MyCollection"); for($i=0; $i<$nbKeyword; $i++){ $keys = new \MongoRegex("/".$keywords_Array [$i]."/"); $donnees = $donnees->field("Field1")->in(array("Field2"=>$keys)); } $donnees = $donnees->getQuery()->execute(); Foreach($donnees as $ data) { $resp = $data->getResult(); } Return $resp; Thank you.
Search based on URL varibles
I want my search to focus on variables in the URL string. Currently it uses a form based search. It uses this <?php include('db.php'); // include your code to connect to DB. $tbl_name="mobile"; //your table name $whereClauses = array(); if (! empty($_POST['Model'])) $whereClauses[] ="model='".mysql_real_escape_string($_POST['Model'])."'"; if (! empty($_POST['Mins'])) $whereClauses[] ="minutes='".mysql_real_escape_string($_POST['Mins'])."'"; if (! empty($_POST['Texts'])) $whereClauses[] ="texts='".mysql_real_escape_string($_POST['Texts'])."'"; if (! empty($_POST['Freegifts'])) $whereClauses[] ="free_gift='".mysql_real_escape_string($_POST['Freegifts'])."'"; if (! empty($_POST['Network'])) $whereClauses[] ="network_name='".mysql_real_escape_string($_POST['Network'])."'"; if (! empty($_POST['Merchant'])) $whereClauses[] ="merchant_category='".mysql_real_escape_string($_POST['Merchant'])."'"; $where = ''; if (count($whereClauses) > 0) { $where = ' WHERE '.implode(' AND ',$whereClauses); } $sql = mysql_query("SELECT * FROM $tbl_name".$where); ?> However, I have added this to my page: <?php $model = $_GET['model']; //gets model from URL $mins = $_GET['mins']; //gets mins from URL $texts = $_GET['texts']; //gets mins from URL $freegift = $_GET['free-gift']; //gets mins from URL $network = $_GET['network']; //gets mins from URL $plan = $_GET['plan']; //gets mins from URL ?> It needs to be so not all variables are required. Any help would be appreciated. Thanks in advance :)
There are a number of options that will return value or NULL: <?php $model = (isset($_GET['model']) ? $_GET['model'] : NULL); $mins = (isset($_GET['mins']) ? $_GET['mins'] : NULL); $texts = (isset($_GET['texts']) ? $_GET['texts'] : NULL); $freegift = (isset($_GET['free-gift']) ? $_GET['free-gift'] : NULL); $network = (isset($_GET['network']) ? $_GET['network'] : NULL); $plan = (isset($_GET['plan']) ? $_GET['plan'] : NULL); ?> This will get the variables from their correponding $_GET element, only if there is one set. Alternative syntax (shorter): <?php $model = ($_GET['model'] ? $_GET['model'] : NULL); $mins = ($_GET['mins'] ? $_GET['mins'] : NULL); $texts = ($_GET['texts'] ? $_GET['texts'] : NULL); $freegift = ($_GET['free-gift'] ? $_GET['free-gift'] : NULL); $network = ($_GET['network'] ? $_GET['network'] : NULL); $plan = ($_GET['plan'] ? $_GET['plan'] : NULL); ?> Alternative (untested though): <?php $vars = array('model','mins','texts','free-gift','network','plan'); foreach($vars as $value) { $$value = (isset($_GET[$value]) ? $_GET[$value] : NULL); } ?> EDIT: In order to set a WHERE clause based on it, I suggest: <?php $vars = array('model','mins','texts','free-gift','network','plan'); foreach($vars as $value) { $$value = (isset($_GET[$value]) ? $_GET[$value] : NULL); unset $vars[$value]; //sweeping the NULL ones } $where_clause = $vars[0]; //the only remaining value after previous cleanup ?>