I have a html form with 3 selector:
a. Room -> 1, 2, 3, 4, 5, 6
b. Adults -> 1, 2, 3, 4, 5, 6
c. Childs -> 0, 1, 2, 3, 4, 5
THe php arrays that i need to get looks like:
Example 1 room with 2 adults
$rooms[] = array(array("paxType" => "Adult"), array("paxType" => "Adult"));
Example 2 rooms ( one room is with two adults and the second room si with 2 adults an
one child
$rooms[] = array(array("paxType" => "Adult"), array("paxType" => "Adult"));
$rooms[] = array(array("paxType" => "Adult"), array("paxType" => "Adult"), array("paxType" =>"Child", "age" => 8));
The variables that i receive from the form are as below:
$City= $_POST['City']; - text
$CheckIN= $_POST['CheckIN']; - text (date)
$CheckOUT= $_POST['CheckOUT']; - text (date)
$Rooms= $_POST['Rooms']; - selector (1,2,3,4,5,6)
$Adults= $_POST['Adults']; - selector (1,2,3,4,5,6)
$Childs= $_POST['Childs']; - selector (0,1,2,3,4,5)
Form is workink fine for text and date input fields.
How can i translate the html form request to get into the a bove look like php arrays.
Thank you for your time.
Entire code is:
// create SOAP client object
$client = new SoapClient("http://www.bookingassist.ro/test/book.wsdl", array('trace' => 1));
try {
function addPaxType($type = null, $amount = 0)
{
$pax = array();
for ($i = 0; $i < amount; $i++)
{
array_push($pax, array('paxType' => $type));
}
return $pax;
}
$RoomsLength = 1;//count($_POST['Rooms']);
$Rooms = array();
//iterate over all rooms
for ($i = 0; $i < $RoomsLength ; $i++)
{
$Rooms[$i] = array();
if ( count($Adults) > 0)
{
//use a function to add adults to room
array_push($Rooms[$i] , addPaxType('Adults', count($Adults)));
}
if (count($Childs) > 0)
{
array_push($Rooms[$i], addPaxType('Childs', count($Childs)));
}
}
$filters = array();
$filters[] = array("filterType" => "hotelStar", "filterValue" => "3", "4", "5");
$filters[] = array("filterType" => "resultLimit", "filterValue" => "7");
// make getAvailableHotel request (start search)
$checkAvailability = $client->getAvailableHotel("gfdgdfgVNhTzA4MVZ3Y2hjTkt3QXNHSXZRYUZOb095Qg==", "RHMK", "2015-03-30", "2015-04-12", "EUR", "RO", "false", $rooms, $filters);
}
catch (SoapFault $exception) {
echo $exception->getMessage();
exit;
}
You can get an array setting in the name input method on the html form post
<br />Room: <input type="text" **name="somearray[]"** required/></br>
After that you can do some like this
$room=$_POST['somearray'];
forgive my bad English!
You need a for lus for this. Based upon the data on this page. I cannot define in which rooms the children and adults are and the age of the children based upon the supplied data. I can however show you an attempt to make such an array.
function addPaxType($type = null, $amount = 0)
{
$pax = array();
for ($i = 0; $i < amount; $i++)
{
array_push($pax, array('paxType' => $type));
}
return $pax;
}
$RoomsLength = count($_POST['Rooms']);
$Rooms = array();
//iterate over all rooms
for ($i = 0; $i < $RoomsLength ; $i++)
{
$Rooms[$i] = array();
if ( count($Adults) > 0)
{
//use a function to add adults to room
array_push($Rooms[$i] , addPaxType('adult', count($Adults)));
}
if (count($Childs) > 0)
{
array_push($Rooms[$i], addPaxType('child', count($Childs)));
}
}
When two rooms are selected and two adults this will output:
$Rooms[0] --> [ [paxType => adult], [paxType => adult] ];
$Rooms[1] --> [ [paxType => adult], [paxType => adult] ];
Related
In this project i have a multidimentional array that keeps some employees names, their position, their speciality and their salary. I need to find the average salary of all the managers and all the employees from the array.
<!DOCTYPE html>
<html>
<head>
<meta sharset="UTF-8"/>
<title>Project3</title>
</head>
<body>
<?php
$employees = array(array("name" => "Nikolaou Nikolaos",
"occupation" => "Employee",
"salary" => 1500,
"specialty" => "Web Programmer"),
array("name" => "Papadopoulou Anna",
"occupation" => "Manager",
"salary" => 2300,
"specialty" => "Human resources management"),
array("name" => "Alexiou Nikoleta",
"occupation" => "Employee",
"salary" => 800,
"specialty" => "Secretary"),
);
//Start of the function that prints the arrays in a specific way.
function printTable($table)
{
foreach ($table as $employee => $list) {
// Print a heading:
echo "<h2 style=\"text-align:left; font-size:26px; font-family:times new roman; color:blue\">Employee #$employee</h2><ul>";
// Print each district data:
foreach ($list as $key => $value) {
echo "<li style=\"text-align:left; font-size:18px; font-family:times new roman; color:black\">$key: $value</li>\n";
}// End of nested FOREACH.
// Close the list:
echo '</ul>';
} // End of main FOREACH.
}// End of the function.
//function that calculates and returns the average salary of employees and managers separately.
function calcMeanAges($table, &$hmean, &$gmean)
{
$cEmployees = 0;
$sumsalary_e = 0;
$cManagers = 0;
$sumsalary_m = 0;
foreach ($table as $occupation =>$list) {
foreach ($list as $salary =>$value) {
if ($occupation == "Employee") {
$cEmployees++;
$sumsalary_e += $salary['salary'];
}
if ($occupation == "Manager") {
$cManagers++;
$sumsalary_m += $salary['salary'];
}
}
}
$hmean = $sumsalary_e / $cEmployees;
$gmean = $sumsalary_m / $cManagers;
}
//call of the function that calculates the average salaries.
calcMeanAges($employees, $h, $g);
echo "$h<br>";
echo "$g";
//Printing the elements of the arrays
echo '<p style="color:red;font-size:28px;font-family:times new roman;"><b>Employees and managers</b></p><br>';
printTable($employees);
?>
</body>
</html>
I need to calculate the average salaries of employees and managers separately. So i thought of putting a condition in the Foreach() to check who is who and then count them and keep the sum of their salary. But it doesn't work properly. I would appreciate some help.
Basically:
To calculate the average sum up all different figures and then divide them through the total amount of figures.
So this part should be right.
Can you please check the datatypes of each from them:
$hmean = $sumsalary_e / $cEmployees;
$gmean = $sumsalary_m / $cManagers;
Every type should be an Integer or Int.
You only need one loop not 2 in this case, then all the occurances you want to check or dd up are members of the inner arrays
function calcMeanAges($table, &$hmean, &$gmean)
{
$cEmployees = 0;
$sumsalary_e = 0;
$cManagers = 0;
$sumsalary_m = 0;
foreach ($table as $item) {
if ($item['occupation'] == "Employee") {
$cEmployees++;
$sumsalary_e += $item['salary'];
}
if ($item['occupation'] == "Manager") {
$cManagers++;
$sumsalary_m += $item['salary'];
}
}
$hmean = $sumsalary_e / $cEmployees;
$gmean = $sumsalary_m / $cManagers;
}
this will proccess any occuption and calculate avarage salary. You can add an IF statment if you only need managers and Employee. You can leave it as be if at a later state other occuptions are added.
$employees = array(
array("name" => "Nikolaou Nikolaos",
"occupation" => "Employee",
"salary" => 1500,
"specialty" => "Web Programmer"),
array("name" => "Papadopoulou Anna",
"occupation" => "Manager",
"salary" => 2300,
"specialty" => "Human resources management"),
array("name" => "Alexiou Nikoleta",
"occupation" => "Employee",
"salary" => 800,
"specialty" => "Secretary"),
);
function averageCount($employees)
{
$count = [];
foreach ($employees as $employee) {
$occupation = $employee['occupation'];
if (!array_key_exists($occupation, $count)) {
$count[$occupation]['occupation'] = $occupation;
$count[$occupation]['salary'] = $employee['salary'];
$count[$occupation]['count'] = 1;
} else {
$count[$occupation]['salary'] += $employee['salary'];
$count[$occupation]['count']++;
}
}
if (count($count) > 0) {
foreach ($count as $occupation => $item) {
$count[$occupation]['average'] = $item['salary'] / $item['count'];
}
}
return $count;
}
var_dump(averageCount($employees));
I have 2 tables one called Exams and the other Exam Results. In the exam I enter the passing_score and in the Exam Results I save the answer of the exam. I'm trying to look up the passing_score and if the passing score is greater then the result then the person passed.
Here is the entire function of this code
public function exam($course_id, Request $request)
{
$course = Course::where('id', $course_id)->firstOrFail();
$answers = [];
$exam_score = 0;
foreach ($request->get('question') as $question_id => $answer_id) {
$question = ExamQuestion::find($question_id);
$correct_answer = ExamOption::where('exam_question_id', $question_id)
->where('id', $answer_id)
->where('is_correct', 1)->count() > 0;
$answers[] = [
'exam_question_id' => $question_id,
'exam_option_id' => $answer_id,
'corect' => $correct_answer
];
if ($correct_answer) {
$exam_score += $question->score;
}
}
$exam_result = ExamResult::create([
'exam_id' => $course->exam->id,
'employee_id' => \Auth::id(),
'result' => $exam_score,
]);
$exam_result->answers()->createMany($answers);
$exam_id = ExamResult::all();
$final_results = Exam::where('id', $exam_id)->get(['passing_grade']);
$val = $final_results->passing_grade;
if($exam_result->result >= $val) {
$exam_result->is_complete = 1;
$exam_result->save();
}
return redirect()->route('learn.show', [$course, $request])->with('message', 'Test score: ' . $exam_score);
}
Here is the logic I've tried and I get stuck.
$exam_id = ExamResult::all();
$final_results = Exam::where('id', $exam_id)->get('passing_grade');
$val = $final_results->passing_grade;
if($exam_result->result >= $val) {
$exam_result->is_complete = 1;
$exam_result->save();
}
Here is the error I'm getting
Argument 1 passed to Illuminate\Database\Grammar::columnize() must be
of the type array, string given, called in
/Applications/MAMP/htdocs/QuickHS/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php
on line 137
Here is the database structure for the 2 tables
This is the exam table
This is the exam results table
Here is the correction. Please try this.
$final_results = Exam::where('id', $exam_id)->first();
$val = $final_results->passing_grade;
I've resolved it and here is how I did it
$final_results= Exam::first();
$x = $final_results->passing_grade;
if($exam_result->result >= $x)
{
$exam_result->is_complete = 1;
$exam_result->save();
}
i am having a hard time getting this code block to work:
public function verifyPayment(Request $request)
{
$transaksie = Transaksie::where('trans_nommer', $request->trans_nommer)->firstOrFail();
$transaksie->is_paid = 4;
$transaksie->save();
// Notify user that transaction was successfull
$transaksie->user->notify(new PaymentVerified($transaksie));
// Merk die sertifikaat as verkoop
if($request->sert_nommer >= 1003333) {
$sertifikaat = Alternate::where('sert_nommer', $request->sert_nommer)->firstOrFail();
$sertifikaat->is_paid = 4;
$sertifikaat->save();
}elseif($request->sert_nommer <= 1003604) {
$sertifikaat = Share::where('sert_nommer', $request->sert_nommer)->firstOrFail();
$sertifikaat->is_paid = 4;
$sertifikaat->save();
}elseif($request->sert_nommer <= 50000) {
$sertifikaat = Aandeel::where('sert_nommer', $request->sert_nommer)->firstOrFail();
$sertifikaat->is_paid = 4;
$sertifikaat->save();
}
// Notify seller that transaction was approved
$sertifikaat->user->notify(new BetalingSuccess($sertifikaat));
$sertifikate = Alternate::orderBy('id','DESC')->first();
$old_sert = $sertifikate->sert_nommer;
$aandeel_sert = $old_sert + 1;
$transaksie->user->alternates()->create([
'sert_nommer' => $aandeel_sert,
'user_id' => $transaksie->user->lid_nommer,
'aantal_aandele' => $transaksie->aandele,
'nfc_bedrag' => $transaksie->nfc,
'status_id' => 3,
'is_paid' => 2,
]);
$alt_sertifikate = Alternate::orderBy('id', 'DESC')->first();
$tweede_sertifikaat = $alt_sertifikate->sert_nommer;
$nfc_sert = $tweede_sertifikaat + 1;
$transaksie->user->alternates()->create([
'sert_nommer' => $nfc_sert,
'user_id' => $transaksie->user->lid_nommer,
'aantal_aandele' => $transaksie->aandele,
'nfc_bedrag' => $transaksie->nfc,
'status_id' => 3,
'is_paid' => 2,
]);
// Notify user van nuwe sertifikaate en stuur mail deur queue
$user = $transaksie->user;
dispatch(new StuurSertifikate($aandeel_sert,$nfc_sert,$transaksie,$user))->onQueue('emails');
return redirect()->back()->with('success','Betaling was geverifieer!');
}
What i am trying to achieve is that the $request->sert_nommer must check if it is in the Alternates table, Share table or the Aandeel table. All this code works on my local server with no errors but when I test this on my production server I get a 404 when it reaches this part of the query
Change your query a little bit and it should work (not tested):
if($request->sert_nommer >= 1003333) {
$sertifikaat = Alternate::where('sert_nommer', $request->sert_nommer)->first();
if($sertifikaat){
$sertifikaat->is_paid = 4;
$sertifikaat->save();
}
}elseif($request->sert_nommer <= 1003604) {
$sertifikaat = Share::where('sert_nommer', $request->sert_nommer)->first();
if($sertifikaat){
$sertifikaat->is_paid = 4;
$sertifikaat->save();
}
}elseif($request->sert_nommer <= 50000) {
$sertifikaat = Aandeel::where('sert_nommer', $request->sert_nommer)->first();
if($sertifikaat){
$sertifikaat->is_paid = 4;
$sertifikaat->save();
}
}
The findOrFail and firstOrFail methods will retrieve the first result of the query; however, if no result is found, a Illuminate\Database\Eloquent\ModelNotFoundException will be thrown. If the exception is not caught, a 404 HTTP response is automatically sent back to the user.
For more details see official doc
ok, Going to get this out of the way right now. I suck at php. I am building an angular app that is going to populate a mobile app with data from the database. I having it pull from the database just fine but I need the json formatted in a special way and I have no idea how to do it.
Using json_encode this is how it is coming from the database:
[
{
"id":"1",
"date":"2014-10-03",
"time":"2014-10-03 10:45:05",
"amount":"5"
},
{
"id":"2",
"date":"2014-10-03",
"time":"2014-10-03 12:21:05",
"amount":"2"
}
]
This is how I need it organized (this is just dummy data im using in on the angular side)
[
{
date: '2014-09-04',
feeding: [
{
id: '1',
time: '1409852728000',
amount: '3'
},
{
id: '2',
time: '1409874328000',
amount: '4'
},
]
},
{
date: '2014-09-05',
feeding: [
{
id: '3',
time: '1409915908000',
amount: '3.5'
},
{
id: '4',
time: '1409957908000',
amount: '5'
},
]
},
]
I needs to be seperated out and grouped by date. How would I go about doing this?
Airtech was just about there. Small update to the function though. The feeding value needs to be an array of objects rather than an object. You then need to push individual objects into that array.
function dateparse($in) {
$in = json_decode($in);
$out = array();
for ($i = 0; $i < sizeof($in); $i++) {
$date = $in[$i]->date;
$isFound = false;
for ($i2 = 0; $i2 < sizeof($out); $i2++) {
if ($date == $out[$i2]["date"]) {
// We've already run into this search value before
// So add the the elements
$isFound = true;
$out[$i2]["feeding"][] = array(
"id" => $in[$i]->id,
"time" => $in[$i]->time,
"amount" => $in[$i]->amount);
break;
}
}
if (!$isFound) {
// No matches yet
// We need to add this one to the array
$feeding = array("id" => $in[$i]->id, "time" => $in[$i]->time, "amount" => $in[$i]->amount);
$out[] = array("date" => $in[$i]->date, "feeding" => array($feeding));
}
}
return json_encode($out);
}
How about the following? I tested it on your json input example and it ran fine.
function parse($in)
{
$in = json_decode($in);
$out = array();
for ($i = 0; $i < sizeof($in); $i++) {
$date = $in[$i]->date;
$isFound = false;
for ($i2 = 0; $i2 < sizeof($out); $i2++) {
if ($date == $out[$i2]["date"]) {
// We've already run into this search value before
// So add the the elements
$isFound = true;
$out[$i2][]["feeding"] = array(
"id" => $in[$i]->id,
"time" => $in[$i]->time,
"amount" => $in[$i]->amount);
break;
}
}
if (!$isFound) {
// No matches yet
// We need to add this one to the array
$out[] = array("date" => $in[$i]->date, "feeding" => array(
"id" => $in[$i]->id,
"time" => $in[$i]->time,
"amount" => $in[$i]->amount));
}
}
return json_encode($out);
}
How about this ? This works fine and works as expected :) :-
function dateparse($var)
{
$counter=0; $var = json_decode($var); $date=array();
foreach($var as $key=>$value){foreach($value as $val1=>$val2)
{if($val1 == "date")
{foreach($value as $val3=>$val4)
{if($val3!="date") //because we don't want date again
{
$date[$val2]["feeding"][$counter][$val3] = $val4; continue;
}}continue;
}
}$counter++;}
return json_encode($date);}
I have a script that generates sitemaps based on url index http://example.com/sitemap.index.xml where index is a number >0 that defines what results should be included in each chunk.
$chunk = 10000;
$counter = 0;
$scroll = $es->search(array(
"index" => "index",
"type" => "type",
"scroll" => "1m",
"search_type" => "scan",
"size" => 10,
"from" => $chunk * ($index - 1)
));
$sid = $scroll['_scroll_id'];
while($counter < $chunk){
$docs = $es->scroll(array(
"scroll_id" => $sid,
"scroll" => "1m"
));
$sid = $docs['_scroll_id'];
$counter += count($docs['hits']['hits']);
}
// ...
Now each time I access http://example.com/sitemap.1.xml or http://example.com/sitemap.2.xml the results returned from ES are exactly the same. It returns 50 results (10 per each shard) but does not seem to take count of from = 0, from = 10000.
I'm using elasticsearch-php as ES library.
Any ideas?
In Java, it can be done as follows
QueryBuilder query = QueryBuilders.matchAllQuery();
SearchResponse scrollResp = Constants.client.prepareSearch(index)
.setTypes(type).setSearchType(SearchType.SCAN)
.setScroll(new TimeValue(600000)).setQuery(query)
.setSize(500).execute().actionGet();
while (true) {
scrollResp = Constants.client
.prepareSearchScroll(scrollResp.getScrollId())
.setScroll(new TimeValue(600000)).execute().actionGet();
System.out.println("Record count :"
+ scrollResp.getHits().getHits().length);
total = total + scrollResp.getHits().getHits().length;
System.out.println("Total record count: " + total);
for (SearchHit hit : scrollResp.getHits()) {
//handle the hit
}
// Break condition: No hits are returned
if (scrollResp.getHits().getHits().length == 0) {
System.out.println("All records are fetched");
break;
}
}
Hope it helps.