Undefined variable error when doing calculations - php

I have a web application that I'm rebuilding in Laravel for a cleaning company.
1) I pass in the paramaters to the URL route
http://127.0.0.1:8000/book?bed_range=1&bath_range=1&percentage_range=0&duration_range=
2) It does calculations:
$bedroomCostFactor = [0, 30, 60, 90, 120, 150];
$bathroomCostFactor = [0, 20, 40, 60, 80, 100];
$percentageFactor = [0, 5, 15, 10, 20];
if (isset($bed_range) && !empty($bed_range)) {
if ($bed_range == 1) {
$bed_cost = $bedroomCostFactor[0];
} else if ($bed_range == 2){
$bed_cost = $bedroomCostFactor[1];
} else if ($bed_range == 3){
$bed_cost = $bedroomCostFactor[2];
} else if ($bed_range == 4){
$bed_cost = $bedroomCostFactor[3];
} else if ($bed_range == 5){
$bed_cost = $bedroomCostFactor[4];
} else if ($bed_range == 6){
$bed_cost = $bedroomCostFactor[5];
}
}
if(isset($bath_range) && !empty($bath_range)) {
if ($bath_range == 1) {
$bath_cost = $bathroomCostFactor[0];
} else if ($bath_range == 2){
$bath_cost = $bathroomCostFactor[1];
} else if ($bath_range == 3){
$bath_cost = $bathroomCostFactor[2];
} else if ($bath_range == 4){
$bath_cost = $bathroomCostFactor[3];
} else if ($bath_range == 5){
$bath_cost = $bathroomCostFactor[4];
} else if ($bath_range == 6){
$bath_cost = $bathroomCostFactor[5];
}
}
if(isset($percentage_range) && !empty($percentage_range)) {
if ($percentage_range == 0) {
$percentage_cost = $percentageFactor[0];
} else if ($percentage_range == 5){
$percentage_cost = $percentageFactor[1];
} else if ($percentage_range == 15){
$percentage_cost = $percentageFactor[2];
} else if ($percentage_range == 10){
$percentage_cost = $percentageFactor[3];
} else if ($percentage_range == 20){
$percentage_cost = $percentageFactor[4];
}
}
$subtotal = "100.00";
$tax = number_format($subtotal * .0725, 2);
$total = $subtotal + $tax;
$discount = number_format(($subtotal * $percentage_cost) / 100, 2);
For some reason, when I migrate the code to Laravel, it no longer works and it starts saying variables like $discount and $percentage_cost are undefined.
In my previous application the calculations run just fine like you'd expect. Why isn't it working in Laravel?

I had to remove the if condition
if(isset($percentage_range) && !empty($percentage_range)) {

Related

how to do not update value on update function?

i want this code should not be run if user call upate route code
if($getDistanceValue * 1000 > 300){
if($chk_ord == 0)
{
$order->chk_ord_vst = 1;
}
elseif($chk_ord != 0){
$order->chk_ord_vst = $chk_ord + 1;
}
}
elseif($getDistanceValue * 1000 <= 300 ){
if($chk_ord >= 4){
$order->chk_ord_vst = 2;
}
elseif($chk_ord == 3){
$order->chk_ord_vst = 2;
}
elseif($chk_ord == 2){
$order->chk_ord_vst = 1;
}
elseif($chk_ord <= 1){
$order->chk_ord_vst = 0;
}
}
i am using a same fucntion to update or store order but on update i donot want to run above code i know it is very simple but i am a student and learning help me here it is controller function first user create order the if wants to update then we use get function to pass value to edit view and then pass again to stor function
public function storeOrder(Request $request , $update = null , $customer_id){
$order = !is_null($update) ? Order::find($update) : new Order();
$cus_det = explode("-", $request->customer_id);
$tt_amount = array_sum($request->amount) + $request->old_balance;
if(is_null ($update))
$balance = $tt_amount + $request->old_balance;
$auth_id = Auth::id();
if(Auth::user()->role == 4){
$auth_id = Customer::where('user_id', $auth_id)->first()->created_by;
}
$order->customer_id = $cus_det[0];
$order->user_id = Auth::id();
$order->ot_id = !is_null($update) ? $order->ot_id : $auth_id;
$order->unit = array_sum($request->unit);
$order->amount = $tt_amount;
$order->subtotal = array_sum($request->amount);
$order->order_comments = $request->order_comments;
if( Auth::user()->role == 5){
$order->location_url_ot = $request->location_url_ot;
$order->received_amount = 0;
}
else{
$order->received_amount = $request->received_amount;
}
$order->discount = $request->discount;
$order->order_date= date('Y/m/d', strtotime($request->order_date));
if($tt_amount >= $request->received_amount){
$order->amount_left = ($tt_amount - $request->received_amount - $request->discount);
}
else{
$order->advance = $request->received_amount - $tt_amount;
$order->amount_left = $request->old_balance - $request->received_amount - $request->discount;
}
$checkB = $this->checkMinBalance($cus_det[0] , $order->amount_left);
if($checkB){
return redirect()->back()->with('error' , 'Customer Balance Limit Exceeded ( Limit is '.$checkB.' )');
}
if($request->has('important')){
$order->is_important=1;
}
if($request->has('urgent')){
$order->urgent = urgent ;
}
$getothomDistanceValue = ($this->getothomDistance($order) * 1.37);
$getDistanceValue = ($this->getDistance($order) * 1.37);
$old_order = Order::where('customer_id' , $order->customer_id)->orderBy('id' , 'desc')->get();
$chk_ord = $old_order[0]->chk_ord_vst;
if($getDistanceValue * 1000 > 300){
if($chk_ord == 0)
{
$order->chk_ord_vst = 1;
}
elseif($chk_ord != 0){
$order->chk_ord_vst = $chk_ord + 1;
}
}
elseif($getDistanceValue * 1000 <= 300 ){
if($chk_ord >= 4){
$order->chk_ord_vst = 2;
}
elseif($chk_ord == 3){
$order->chk_ord_vst = 2;
}
elseif($chk_ord == 2){
$order->chk_ord_vst = 1;
}
elseif($chk_ord <= 1){
$order->chk_ord_vst = 0;
}
}
$order->save();
}
What I get from your question is you are using the same storeOrder function for storing and updating an order.
When you are using it for update, You need the id from $request->id or any other parameter from the front-end so that you can find that specific Order and then call update(). If you get the $id, You can just check if the order exists or not like below:
$order_exists = Order::find($request->id);
if(!isset($order_exists))
{
if($getDistanceValue * 1000 > 300){
if($chk_ord == 0)
{
$order->chk_ord_vst = 1;
}
elseif($chk_ord != 0){
$order->chk_ord_vst = $chk_ord + 1;
}
}
elseif($getDistanceValue * 1000 <= 300 ){
if($chk_ord >= 4){
$order->chk_ord_vst = 2;
}
elseif($chk_ord == 3){
$order->chk_ord_vst = 2;
}
elseif($chk_ord == 2){
$order->chk_ord_vst = 1;
}
elseif($chk_ord <= 1){
$order->chk_ord_vst = 0;
}
}
}
// then $order->save() when done
One way to go about this would be to pass a parameter through the form and check.
But if you have route names defined, you could also do something like:
if(\Illuminate\Support\Facades\Route::current()->getName() === 'orders.create'){
// logic here
}
However, I must say that I consider the later solution an anti-pattern.

PHP Function to detect rar files completion (*.r01) not working with *.s01

I have a php function to detect multiple rar file completion. The problem i have is that when old style rar is used eg:
archive.rar
archive.r01
archive.r02
archive.r03
......
archive.r99
The limit is archive.r99 and then goes to:
archive.s01
archive.s02
archive.s03
I can't do anything about the source.. and I have no idea how to adjust this function.
I tried do a || in strpos:
if (strpos($b[0], '.r') > - 1 || strpos($b[0], '.s') > - 1 ) {
but that doesn't work. It doesn't detect the s01 and keeps saying incomplete archive/missing files s01, s02, s03 etc...
here is the function:
private function detectMissingFiles($array) {
$typ = 0;
$tid = $t["id"];
$breaked = false;
$arr = array();
foreach($array as $b) {
if (stripos($b[0], 'disc') > - 1) {
return false;
}
if (strpos($b[0], '/') > - 1) {
continue;
}
if ($typ == 0) {
if (strpos($b[0], '.part0') > - 1) {
$typ = 2;
}
else
if (strpos($b[0], '.r') > - 1) {
$typ = 1;
}
else {
continue;
}
}
if ($typ == 1) {
if (is_numeric($s = substr($b[0], -2))) {
$arr[] = array(
id => $s,
s => $b[1]
);
}
}
else
if ($typ == 2) {
if (is_numeric($s = substr($b[0], -6, 2)) && substr($b[0], -4) == '.rar') {
$arr[] = array(
id => $s,
s => $b[1]
);
}
}
}
asort($arr);
if ($typ == 1) $sista = - 1;
else
if ($typ == 2) $sista = 0;
$status = "";
$antal = count($arr) - 1;
if ($antal > 30) {
foreach($arr as $ar) {
if ($antal > $ar["id"]) {
if ($ar["s"] < 50000000) {
$status.= L::get("WRONG_RAR_FILE_SIZE", [$ar["id"]]);
}
}
if ($sista + 1 != $ar["id"]) {
$status.= L::get("MISSING_FILE", [$sista + 1]);
}
$sista = $ar["id"];
}
if (strlen($status) > 1 && strlen($status) < 200) return $status;
else return 0;
}
}
I am hoping someone can enlighten me here. Thank you in advance!
my best guess is that the "old style rar" name algorithm is as follows:
function rar_int_to_string(int $i):string{
$c="r";
while($i>99){
++$c;
$i-=99;
}
if($i<10){
$c=$c."0";
}
return $c.$i;
}
which produces
r01>r02>r03>r04>r05>r06>r07>r08>r09>r10>r11>r12>r13>r14>r15>r16>r17>r18>r19>r20>r21>r22>r23>r24>r25>r26>r27>r28>r29>r30>r31>r32>r33>r34>r35>r36>r37>r38>r39>r40>r41>r42>r43>r44>r45>r46>r47>r48>r49>r50>r51>r52>r53>r54>r55>r56>r57>r58>r59>r60>r61>r62>r63>r64>r65>r66>r67>r68>r69>r70>r71>r72>r73>r74>r75>r76>r77>r78>r79>r80>r81>r82>r83>r84>r85>r86>r87>r88>r89>r90>r91>r92>r93>r94>r95>r96>r97>r98>r99>s01>s02>s03>s04>s05>s06>s07>s08>s09>s10>s11>s12>s13>s14>s15>s16>s17>s18>s19>s20>s21>s22>s23>s24>s25>s26>s27>s28>s29>s30>s31>s32>s33>s34>s35>s36>s37>s38>s39>s40>s41>s42>s43>s44>s45>s46>s47>s48>s49>s50>s51>s52>s53>s54>s55>s56>s57>s58>s59>s60>s61>s62>s63>s64>s65>s66>s67>s68>s69>s70>s71>s72>s73>s74>s75>s76>s77>s78>s79>s80>s81>s82>s83>s84>s85>s86>s87>s88>s89>s90>s91>s92>s93>s94>s95>s96>s97>s98>s99>t01>t02>t03>t04>t05>t06>t07>t08>t09>t10>t11>t12>t13>t14>t15>t16>t17>t18>t19>t20>t21>t22>t23>t24>t25>t26>t27>t28>t29>t30>t31>t32>t33>t34>t35>t36>t37>t38>t39>t40>t41>t42>t43>t44>t45>t46>t47>t48>t49>t50>t51>t52>t53>t54>t55>t56>t57>t58>t59>t60>t61>t62>t63>t64>t65>t66>t67>t68>t69>t70>t71>t72>t73>t74>t75>t76>t77>t78>t79>t80>t81>t82>t83>t84>t85>t86>t87>t88>t89>t90>t91>t92>t93>t94>t95>t96>t97>t98>t99>u01>u02>u03>u04>u05>u06>u07>u08>u09>u10>u11>u12>u13>u14>u15>u16>u17>u18>u19>u20>u21>u22>u23>u24>u25>u26>u27>u28>u29>u30>u31>u32>u33>u34>u35>u36>u37>u38>u39>u40>u41>u42>u43>u44>u45>u46>u47>u48>u49>u50>u51>u52>u53>u54>u55>u56>u57>u58>u59>u60>u61>u62>u63>u64>u65>u66>u67>u68>u69>u70>u71>u72>u73>u74>u75>u76>u77>u78>u79>u80>u81>u82>u83>u84>u85>u86>u87>u88>u89>u90>u91>u92>u93>u94>u95>u96>u97>u98>u99>v01>v02>v03>v04>v05>v06>v07>v08>v09>v10>v11>v12>v13>v14>v15>v16>v17>v18>v19>v20>v21>v22>v23>v24>v25>v26>v27>v28>v29>v30>v31>v32>v33>v34>v35>v36>v37>v38>v39>v40>v41>v42>v43>v44>v45>v46>v47>v48>v49>v50>v51>v52>v53>v54>v55>v56>v57>v58>v59>v60>v61>v62>v63>v64>v65>v66>v67>v68>v69>v70>v71>v72>v73>v74>v75>v76>v77>v78>v79>v80>v81>v82>v83>v84>v85>v86>v87>v88>v89>v90>v91>v92>v93>v94>v95>v96>v97>v98>v99>w01>w02>w03>w04>w05>w06>w07>w08>w09>w10>w11>w12>w13>w14>w15>w16>w17>w18>w19>w20>w21>w22>w23>w24>w25>w26>w27>w28>w29>w30>w31>w32>w33>w34>w35>w36>w37>w38>w39>w40>w41>w42>w43>w44>w45>w46>w47>w48>w49>w50>w51>w52>w53>w54>w55>w56>w57>w58>w59>w60>w61>w62>w63>w64>w65>w66>w67>w68>w69>w70>w71>w72>w73>w74>w75>w76>w77>w78>w79>w80>w81>w82>w83>w84>w85>w86>w87>w88>w89>w90>w91>w92>w93>w94>w95>w96>w97>w98>w99>x01>x02>x03>x04>x05>x06>x07>x08>x09>x10>x11>x12>x13>x14>x15>x16>x17>x18>x19>x20>x21>x22>x23>x24>x25>x26>x27>x28>x29>x30>x31>x32>x33>x34>x35>x36>x37>x38>x39>x40>x41>x42>x43>x44>x45>x46>x47>x48>x49>x50>x51>x52>x53>x54>x55>x56>x57>x58>x59>x60>x61>x62>x63>x64>x65>x66>x67>x68>x69>x70>x71>x72>x73>x74>x75>x76>x77>x78>x79>x80>x81>x82>x83>x84>x85>x86>x87>x88>x89>x90>x91>x92>x93>x94>x95>x96>x97>x98>x99>y01>y02>y03>y04>y05>y06>y07>y08>y09>y10>y11>y12>y13>y14>y15>y16>y17>y18>y19>y20>y21>y22>y23>y24>y25>y26>y27>y28>y29>y30>y31>y32>y33>y34>y35>y36>y37>y38>y39>y40>y41>y42>y43>y44>y45>y46>y47>y48>y49>y50>y51>y52>y53>y54>y55>y56>y57>y58>y59>y60>y61>y62>y63>y64>y65>y66>y67>y68>y69>y70>y71>y72>y73>y74>y75>y76>y77>y78>y79>y80>y81>y82>y83>y84>y85>y86>y87>y88>y89>y90>y91>y92>y93>y94>y95>y96>y97>y98>y99>z01>z02>z03>z04>z05>z06>z07>z08>z09>z10>z11>z12>z13>z14>z15>z16>z17>z18>z19>z20>z21>z22>z23>z24>z25>z26>z27>z28>z29>z30>z31>z32>z33>z34>z35>z36>z37>z38>z39>z40>z41>z42>z43>z44>z45>z46>z47>z48>z49>z50>z51>z52>z53>z54>z55>z56>z57>z58>z59>z60>z61>z62>z63>z64>z65>z66>z67>z68>z69>z70>z71>z72>z73>z74>z75>z76>z77>z78>z79>z80>z81>z82>z83>z84>z85>z86>z87>z88>z89>z90>z91>z92>z93>z94>z95>z96>z97>z98>z99>aa01>aa02>aa03>aa04>aa05>aa06>aa07>aa08>aa09>aa10>aa11>aa12>aa13>aa14>aa15>aa16>aa17>aa18>aa19>aa20>aa21>aa22>aa23>aa24>aa25>aa26>aa27>aa28>aa29>aa30>aa31>aa32>aa33>aa34>aa35>aa36>aa37>aa38>aa39>aa40>aa41>aa42>aa43>aa44>aa45>aa46>aa47>aa48>aa49>aa50>aa51>aa52>aa53>aa54>aa55>aa56>aa57>aa58>aa59>aa60>aa61>aa62>aa63>aa64>aa65>aa66>aa67>aa68>aa69>aa70>aa71>aa72>aa73>aa74>aa75>aa76>aa77>aa78>aa79>aa80>aa81>aa82>aa83>aa84>aa85>aa86>aa87>aa88>aa89>aa90>aa91>aa92>aa93>aa94>aa95>aa96>aa97>aa98>aa99>ab01>ab02>ab03>ab04>ab05>ab06>ab07>ab08->ab09
and so on, with "ab09" meaning "file chunk #999"

Inserting echo via mysql statement

I am aiming to insert the respective learning style into the database if the echo statement has been triggered by the if statement. The DB columns are as follows: studentNumber, dimension1, dimension2, dimension3, dimension4. Any feedback would be appreciated. Trialing 'mildly active' hence the statement only being present at that stage of the code.
<?php
$active =0;
$reflective=0;
$sensing=0;
$intuitive=0;
$visual=0;
$verbal=0;
$sequential=0;
$global=0;
$mildlyActive = "Mildly Active";
$moderatelyActive = "Moderately Active";
$stronglyActive = "Strongly Active";
$mildlyReflective = "Mildly Reflective";
$moderatelyReflective = "Moderately Reflective";
$stronglyReflective = "Strongly Reflective";
if(isset($_POST['submit'])){
foreach($_POST as $key=>$value){
if($key != "submit"){
$dbQuery1 = $db->prepare("select * FROM answer WHERE answerID = '".$value."'");
$dbQuery1-> execute();
while ($dbRow = $dbQuery1->fetch (PDO::FETCH_ASSOC)){
$answerTypeID=$dbRow["answerTypeID"];
$dbQuery2 = $db->prepare("select * FROM answerType WHERE answerTypeID = '".$answerTypeID."'");
$dbQuery2-> execute();
while ($dbRow2=$dbQuery2->fetch()){
$answerType=$dbRow2["answerType"];
//echo $answerType;
if ($answerType=='active'){
$active++;
}
if ($answerType=='reflective'){
$reflective++;
}
if ($answerType=='sensing'){
$sensing++;
}
if ($answerType=='intuitive'){
$intuitive++;
}
if ($answerType=='visual'){
$visual++;
}
if ($answerType=='verbal'){
$verbal++;
}
if ($answerType=='sequential'){
$sequential++;
}
if ($answerType=='global'){
$global++;
}
}
}
$dbQuery=$db->prepare("INSERT INTO `userResponse` VALUES (:studentID, :questionID, :answerID)");
$dbParams=array('studentID'=>$_SESSION["currentUser"], 'questionID'=>$key, 'answerID'=>$value);
$dbQuery->execute($dbParams);
}
}
}
if ($active>$reflective){
$total = $active - $reflective;
if ($total == 1){
echo "Mildly active";
$dbQuery=$db->prepare("INSERT INTO indexLearningStyle (dimension1) VALUE (:mildlyActive)");
}
if ($total == 2){
echo "Mildly active";
$dbQuery=$db->prepare("INSERT INTO indexLearningStyle (dimension1) VALUE (:mildlyActive)");
}
if($total == 3){
echo "Moderately active";
}
if($total == 4){
echo "Moderately active";
}
if ($total == 5){
echo "Strongly active";
}
}
else {
$total = $reflective - $active;
if ($total == 1){
echo "Mildly reflective";
}
if ($total == 2){
echo "Mildly reflective";
}
if ($total == 3){
echo "Moderately reflective";
}
if ($total == 4){
echo "Moderately reflective";
}
if ($total == 5){
echo "Strongly reflective";
}
}
if ($sensing>$intuitive){
$total = $sensing - $intuitive;
if ($total == 1){
echo "Mildly sensing";
}
if ($total == 2){
echo "Mildly sensing";
}
if($total == 3){
echo "Moderately sensing";
}
if($total == 4){
echo "Moderately sensing";
}
if ($total == 5){
echo "Strongly sensing";
}
}
else {
$total = $intuitive - $sensing;
if ($total == 1){
echo "Mildly intuitive";
}
if ($total == 2){
echo "Mildly intuitive";
}
if ($total == 3){
echo "Moderately intuitive";
}
if ($total == 4){
echo "Moderately intuitive";
}
if ($total == 5){
echo "Strongly intuitive";
}
}
if ($visual>$verbal){
$total = $visual - $verbal;
if ($total == 1){
echo "Mildly visual";
}
if ($total == 2){
echo "Mildly visual";
}
if($total == 3){
echo "Moderately visual";
}
if($total == 4){
echo "Moderately visual";
}
if ($total == 5){
echo "Strongly visual";
}
}
else {
$total = $verbal - $visual;
if ($total == 1){
echo "Mildly verbal";
}
if ($total == 2){
echo "Mildly verbal";
}
if ($total == 3){
echo "Moderately verbal";
}
if ($total == 4){
echo "Moderately verbal";
}
if ($total == 5){
echo "Strongly verbal";
}
}
if ($sequential>$global){
$total = $sequential - $global;
if ($total == 1){
echo "Mildly sequential";
}
if ($total == 2){
echo "Mildly sequential";
}
if($total == 3){
echo "Moderately sequential";
}
if($total == 4){
echo "Moderately sequential";
}
if ($total == 5){
echo "Strongly sequential";
}
}
else {
$total = $global - $sequential;
if ($total == 1){
echo "Mildly global";
}
if ($total == 2){
echo "Mildly global";
}
if ($total == 3){
echo "Moderately global";
}
if ($total == 4){
echo "Moderately global";
}
if ($total == 5){
echo "Strongly global";
}
}
?>

New Error.. Cant Figure This One Out

My timestamp code worked well before i put it in a function.. Get this parse error on line 55, ive marked the line number with "// LINE 55", cant figure it out :(
Here's my error:
Parse error: syntax error, unexpected '.' in C:\wamp\www\flueforumdk\config.php on line 55
Here's the calling of the function:
$GET_UNIX_STAMP_FROM_DB = $art[tidspunkt];
$UNIX_TIME_SECONDS = $GET_UNIX_STAMP_FROM_DB;
echo timestamp_converter($UNIX_TIME_SECONDS);
Here's my function code:
## TIMESTAMP CONVERTER FUNCTION
function timestamp_converter($UNIX_TIME_SECONDS){
// UDREGNING FRA UNIX TIME
$tid = time() - $UNIX_TIME_SECONDS;
$timer = floor($tid/3600);
$minutter = floor($tid/60);
$dage = floor($timer / 24);
$uge = floor($dage / 7);
$month = floor($dage / 30.5);
$aar = floor($dage / 365);
if($tid < 60){
echo"<b>$tid</b> sekunder";
} elseif ($tid > 60){
echo"";
}
if($minutter == 0){
echo"";
} elseif ($minutter < 60){
if($minutter == 1){
echo"<b>$minutter</b> minut";
}else{
echo"<b>$minutter</b> minutter";
}
}
if($timer == 0){
echo"";
} elseif ($timer < 24){
if($timer == 1){
echo"<b>$timer</b> time";
}else{
echo"<b>$timer</b> timer";
}
}
//LINE 55 if($dage == 0){
echo"";
} elseif ($dage < 7){
if($dage == 1){
echo"<b>$dage</b> dag";
}else{
echo"<b>$dage</b> dage";
}
}
if($uge == 0){
echo"";
} elseif ($uge < 4){
if($uge == 1){
echo"<b>$uge</b> uge";
}else{
echo"<b>$uge</b> uger";
}
}
if($month == 0){
echo"";
} elseif ($month < 12){
if($month == 1){
echo"<b>$month</b> måned";
}else{
echo"<b>$month</b> måneder";
}
}
if($aar == 0){
echo"";
} elseif ($aar > 0){
if($aar == 1){
echo"<b>$aar</b> år";
}else{
echo"<b>$aar</b> år";
}
}
}

foreach() skipping first element in array

Trying to create a GPA Calculator. I have a form that submits to a php file and then stores all data from the form in a php array(). The calculator works great until I enter the same value. I think this wont make sense until I show some pictures:
Here is the problem in an image:
So in the first image I enter a A and then another A which outputs array(1) { [0]=> float(4.5) } when using var_dump()
And in the second image the var_dump() is array(2) { [0]=> float(4) 1=> float(3.5) }
It is skipping the first row in the first image... just in case A is supposed to equal 4.0 in REG and 4.5 in HONORS. It might be the array_combine()
Here is my php code:
//$_POST['grades'] for the grades <option> and $_POST['types'] for the type (REG, HONORS)
foreach(array_combine($_POST['grades'], $_POST['types']) as $code => $count)
{
if ($code == "A")
{
if ($count == "REGULAR")
{
$GradeArray[] = 4.0;
}
else if ($count == "HONORS")
{
$GradeArray[] = 4.5;
}
else if ($count == "COLLEGE")
{
$GradeArray[] = 5.0;
}
}
else if ($code == "B")
{
if ($count == "REGULAR")
{
$GradeArray[] = 3.0;
}
else if ($count == "HONORS")
{
$GradeArray[] = 3.5;
}
else if ($count == "COLLEGE")
{
$GradeArray[] = 4.0;
}
}
else if ($code == "C")
{
if ($count == "REGULAR")
{
$GradeArray[] = 2.0;
}
else if ($count == "HONORS")
{
$GradeArray[] = 2.5;
}
else if ($count == "COLLEGE")
{
$GradeArray[] = 3.0;
}
}
else if ($code == "D")
{
if ($count == "REGULAR")
{
$GradeArray[] = 1.0;
}
else if ($count == "HONORS")
{
$GradeArray[] = 1.5;
}
else if ($count == "COLLEGE")
{
$GradeArray[] = 2.0;
}
}
else if ($code == "F")
{
if ($count == "REGULAR")
{
$GradeArray[] = 0.0;
}
else if ($count == "HONORS")
{
$GradeArray[] = .5;
}
else if ($count == "COLLEGE")
{
$GradeArray[] = 1.0;
}
}
}
It might be the whole foreach() statement that needs reworking... I am up to writing the logic again if anyone says so...
I don't want to clog up the question with code so if you absolutely need the html just ask and I will add in an edit.
EDIT: I am also thinking I need to rewrite the logic... I have never used array_combine() before... I just need to make sure the corresponds with the related
Thanks for the help!
If you want to iterate over two arrays (or more) you could consider using the MultipleIterator; it doesn't clobber array keys like array_combine() does.
You could also simplify your logic by using an array to define the scores of each grade / type combination:
$gradesToScores = array(
'REGULAR' => array(
'A' => 4.0, 'B' => 3.0, 'C' => 2.0, 'D' => 1.0, 'F' => 0.0,
),
'HONORS' => array(
'A' => 4.5, 'B' => 3.5, 'C' => 2.5, 'D' => 1.5, 'F' => 0.5,
),
'COLLEGE' => array(
'A' => 5.0, 'B' => 4.0, 'C' => 3.0, 'D' => 2.0, 'F' => 1.0,
),
);
$gradeItemIterator = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC);
$gradeItemIterator->attachIterator(new ArrayIterator($_POST['grades']), 'grade');
$gradeItemIterator->attachIterator(new ArrayIterator($_POST['types']), 'type');
$gradeScores = array();
foreach ($gradeItemIterator as $gradeItem) {
$gradeScores[] = $gradesToScores[$gradeItem['type']][$gradeItem['grade']];
}
look at this array and output look the VALUES "a" which becomes the key of the resultant array,
so if u want the full array to be combined with the key of first array then first array must kave unique values.
<?php
print_r(array_combine(Array('a','a','b'), Array(1,2,3)));
?>
Returns:
Array
(
[a] => 2
[b] => 3
)
solution (may not be the best )
foreach($_POST['grades'] as $KEY=>$code)
{
$count = $_POST['types'][$KEY];
if ($code == "A")
{
if ($count == "REGULAR")
{
$GradeArray[] = 4.0;
}
else if ($count == "HONORS")
{
$GradeArray[] = 4.5;
}
else if ($count == "COLLEGE")
{
$GradeArray[] = 5.0;
}
}
else if ($code == "B")
{
if ($count == "REGULAR")
{
$GradeArray[] = 3.0;
}
else if ($count == "HONORS")
{
$GradeArray[] = 3.5;
}
else if ($count == "COLLEGE")
{
$GradeArray[] = 4.0;
}
}
else if ($code == "C")
{
if ($count == "REGULAR")
{
$GradeArray[] = 2.0;
}
else if ($count == "HONORS")
{
$GradeArray[] = 2.5;
}
else if ($count == "COLLEGE")
{
$GradeArray[] = 3.0;
}
}
else if ($code == "D")
{
if ($count == "REGULAR")
{
$GradeArray[] = 1.0;
}
else if ($count == "HONORS")
{
$GradeArray[] = 1.5;
}
else if ($count == "COLLEGE")
{
$GradeArray[] = 2.0;
}
}
else if ($code == "F")
{
if ($count == "REGULAR")
{
$GradeArray[] = 0.0;
}
else if ($count == "HONORS")
{
$GradeArray[] = .5;
}
else if ($count == "COLLEGE")
{
$GradeArray[] = 1.0;
}
}
}
I agree with Arun on the reason for the behaviour. A possible solution would be:
$lookup = Array("A"=>4, "B"=>3, "C"=>2, "D"=>1, "F"=>0);
for ( $i=0; $i<count($_POST['grades']); $i++ ) {
$temp = $lookup[ $_POST['grades'][$i] ];
if ( $_POST['types'][$i] == "HONORS" ) {
$temp += .5;
}
elseif ( $_POST['types'][$i] == "COLLEGE" ) {
$temp += 1;
}
$GradeArray[] = $temp;
}
This assumes the count of $_POST['grades'] and $_POST['types'] to be equal - otherwise it will either cause an undefined offset notice or not address each value.
Regarding Arun's code: nesting the two loops will create N*M inner loop iterations - one for each combination of grade/type, clearly wrong in this case! We need to go through the arrays in parallel, as they are used to hold pair values.
#Jack: If I'm not mistaken, you need to swap HONORS and COLLEGE in your Array definition.
This solution works for me fine
$array = array('ab', 'bc', 'cd', 'de');
$array = array_combine(range(1, count($array)), array_values($array));
print_r($array);

Categories