How to find and make sum of different numbers from array? - php

I've read some texts and searched topics, but nothing help me. I'm beginner in PHP. I have array where are variables $qA01_1 up to $qA30_5 and their values can be different 0 or 1 or 5. From array I would like to find all variables with value 1 and make sum. Same for number 5.
$qA01_1 = $_SESSION['qA01_1'];
$qA01_2 = $_SESSION['qA01_2'];
$qA01_3 = $_SESSION['qA01_3'];
$qA01_4 = $_SESSION['qA01_4'];
$qA01_5 = $_SESSION['qA01_5'];
$qA02_1 = $_SESSION['qA02_1'];
$qA02_2 = $_SESSION['qA02_2'];
$qA02_3 = $_SESSION['qA02_3'];
$qA02_4 = $_SESSION['qA02_4'];
$qA02_5 = $_SESSION['qA02_5'];
$qA03_1 = $_SESSION['qA03_1'];
$qA03_2 = $_SESSION['qA03_2'];
$qA03_3 = $_SESSION['qA03_3'];
$qA03_4 = $_SESSION['qA03_4'];
$qA03_5 = $_SESSION['qA03_5'];
$qA04_1 = $_SESSION['qA04_1'];
$qA04_2 = $_SESSION['qA04_2'];
$qA04_3 = $_SESSION['qA04_3'];
$qA04_4 = $_SESSION['qA04_4'];
$qA04_5 = $_SESSION['qA04_5'];
$qA05_1 = $_SESSION['qA05_1'];
$qA05_2 = $_SESSION['qA05_2'];
$qA05_3 = $_SESSION['qA05_3'];
$qA05_4 = $_SESSION['qA05_4'];
$qA05_5 = $_SESSION['qA05_5'];
$qA06_1 = $_SESSION['qA06_1'];
$qA06_2 = $_SESSION['qA06_2'];
$qA06_3 = $_SESSION['qA06_3'];
$qA06_4 = $_SESSION['qA06_4'];
$qA06_5 = $_SESSION['qA06_5'];
$qA07_1 = $_SESSION['qA07_1'];
$qA07_2 = $_SESSION['qA07_2'];
$qA07_3 = $_SESSION['qA07_3'];
$qA07_4 = $_SESSION['qA07_4'];
$qA07_5 = $_SESSION['qA07_5'];
$qA08_1 = $_SESSION['qA08_1'];
$qA08_2 = $_SESSION['qA08_2'];
$qA08_3 = $_SESSION['qA08_3'];
$qA08_4 = $_SESSION['qA08_4'];
$qA08_5 = $_SESSION['qA08_5'];
$qA09_1 = $_SESSION['qA09_1'];
$qA09_2 = $_SESSION['qA09_2'];
$qA09_3 = $_SESSION['qA09_3'];
$qA09_4 = $_SESSION['qA09_4'];
$qA09_5 = $_SESSION['qA09_5'];
$qA10_1 = $_SESSION['qA10_1'];
$qA10_2 = $_SESSION['qA10_2'];
$qA10_3 = $_SESSION['qA10_3'];
$qA10_4 = $_SESSION['qA10_4'];
$qA10_5 = $_SESSION['qA10_5'];
$qA11_1 = $_SESSION['qA11_1'];
$qA11_2 = $_SESSION['qA11_2'];
$qA11_3 = $_SESSION['qA11_3'];
$qA11_4 = $_SESSION['qA11_4'];
$qA11_5 = $_SESSION['qA11_5'];
$qA12_1 = $_SESSION['qA12_1'];
$qA12_2 = $_SESSION['qA12_2'];
$qA12_3 = $_SESSION['qA12_3'];
$qA12_4 = $_SESSION['qA12_4'];
$qA12_5 = $_SESSION['qA12_5'];
$qA13_1 = $_SESSION['qA13_1'];
$qA13_2 = $_SESSION['qA13_2'];
$qA13_3 = $_SESSION['qA13_3'];
$qA13_4 = $_SESSION['qA13_4'];
$qA13_5 = $_SESSION['qA13_5'];
$qA14_1 = $_SESSION['qA14_1'];
$qA14_2 = $_SESSION['qA14_2'];
$qA14_3 = $_SESSION['qA14_3'];
$qA14_4 = $_SESSION['qA14_4'];
$qA14_5 = $_SESSION['qA14_5'];
$qA15_1 = $_SESSION['qA15_1'];
$qA15_2 = $_SESSION['qA15_2'];
$qA15_3 = $_SESSION['qA15_3'];
$qA15_4 = $_SESSION['qA15_4'];
$qA15_5 = $_SESSION['qA15_5'];
$qA16_1 = $_SESSION['qA16_1'];
$qA16_2 = $_SESSION['qA16_2'];
$qA16_3 = $_SESSION['qA16_3'];
$qA16_4 = $_SESSION['qA16_4'];
$qA16_5 = $_SESSION['qA16_5'];
$qA17_1 = $_SESSION['qA17_1'];
$qA17_2 = $_SESSION['qA17_2'];
$qA17_3 = $_SESSION['qA17_3'];
$qA17_4 = $_SESSION['qA17_4'];
$qA17_5 = $_SESSION['qA17_5'];
$qA18_1 = $_SESSION['qA18_1'];
$qA18_2 = $_SESSION['qA18_2'];
$qA18_3 = $_SESSION['qA18_3'];
$qA18_4 = $_SESSION['qA18_4'];
$qA18_5 = $_SESSION['qA18_5'];
$qA19_1 = $_SESSION['qA19_1'];
$qA19_2 = $_SESSION['qA19_2'];
$qA19_3 = $_SESSION['qA19_3'];
$qA19_4 = $_SESSION['qA19_4'];
$qA19_5 = $_SESSION['qA19_5'];
$qA20_1 = $_SESSION['qA20_1'];
$qA20_2 = $_SESSION['qA20_2'];
$qA20_3 = $_SESSION['qA20_3'];
$qA20_4 = $_SESSION['qA20_4'];
$qA20_5 = $_SESSION['qA20_5'];
$qA21_1 = $_SESSION['qA21_1'];
$qA21_2 = $_SESSION['qA21_2'];
$qA21_3 = $_SESSION['qA21_3'];
$qA21_4 = $_SESSION['qA21_4'];
$qA21_5 = $_SESSION['qA21_5'];
$qA22_1 = $_SESSION['qA22_1'];
$qA22_2 = $_SESSION['qA22_2'];
$qA22_3 = $_SESSION['qA22_3'];
$qA22_4 = $_SESSION['qA22_4'];
$qA22_5 = $_SESSION['qA22_5'];
$qA23_1 = $_SESSION['qA23_1'];
$qA23_2 = $_SESSION['qA23_2'];
$qA23_3 = $_SESSION['qA23_3'];
$qA23_4 = $_SESSION['qA23_4'];
$qA23_5 = $_SESSION['qA23_5'];
$qA24_1 = $_SESSION['qA24_1'];
$qA24_2 = $_SESSION['qA24_2'];
$qA24_3 = $_SESSION['qA24_3'];
$qA24_4 = $_SESSION['qA24_4'];
$qA24_5 = $_SESSION['qA24_5'];
$qA25_1 = $_SESSION['qA25_1'];
$qA25_2 = $_SESSION['qA25_2'];
$qA25_3 = $_SESSION['qA25_3'];
$qA25_4 = $_SESSION['qA25_4'];
$qA25_5 = $_SESSION['qA25_5'];
$qA26_1 = $_SESSION['qA26_1'];
$qA26_2 = $_SESSION['qA26_2'];
$qA26_3 = $_SESSION['qA26_3'];
$qA26_4 = $_SESSION['qA26_4'];
$qA26_5 = $_SESSION['qA26_5'];
$qA27_1 = $_SESSION['qA27_1'];
$qA27_2 = $_SESSION['qA27_2'];
$qA27_3 = $_SESSION['qA27_3'];
$qA27_4 = $_SESSION['qA27_4'];
$qA27_5 = $_SESSION['qA27_5'];
$qA28_1 = $_SESSION['qA28_1'];
$qA28_2 = $_SESSION['qA28_2'];
$qA28_3 = $_SESSION['qA28_3'];
$qA28_4 = $_SESSION['qA28_4'];
$qA28_5 = $_SESSION['qA28_5'];
$qA29_1 = $_SESSION['qA29_1'];
$qA29_2 = $_SESSION['qA29_2'];
$qA29_3 = $_SESSION['qA29_3'];
$qA29_4 = $_SESSION['qA29_4'];
$qA29_5 = $_SESSION['qA29_5'];
$qA30_1 = $_POST['qA30_1'];
$qA30_2 = $_POST['qA30_2'];
$qA30_1 = (isset($_POST['qA30_1'])) ? $_POST['qA30_1'] : 0;
$qA30_2 = (isset($_POST['qA30_2'])) ? $_POST['qA30_2'] : 0;
$qA30_3 = (isset($_POST['qA30_3'])) ? $_POST['qA30_3'] : 0;
$qA30_4 = (isset($_POST['qA30_4'])) ? $_POST['qA30_4'] : 0;
$qA30_5 = (isset($_POST['qA30_5'])) ? $_POST['qA30_5'] : 0;
Here is my proposal which do not work. I thing that I should to do something with "array" .
// Sum of all numbers 1
$sumOne = 0;
for ($i = 0; $i <= 3; $i++) {
for($j = 0; $j <= 9; $j++) {
for($k = 1; $k <= 5; $k++){
$u = 'qA_' . $i . $j. '_' . $k;
if ($u==1){
$sumOnet+=1;
}
}
}
}
echo "<br/>SumOne:" . " " . round($sumOne[0], 0);

You really should stick with an array. If you define an array within the $_SESSION array and use that it will be much simpler:
$_SESSION['data']['qA01_1'] = 0;
$_SESSION['data']['qA01_2'] = 1;
$_SESSION['data']['qA01_3'] = 5;
$counts = array_count_values($_SESSION['data']);
if(isset($counts[0])) { echo $counts[0]; } // sum of any 0s is 0 so here is the count
if(isset($counts[1])) { echo $counts[1]; } // sum of any 1s will be the count
if(isset($counts[5])) { echo $counts[5] * 5; }

If I understand the question correctly, you want to sum up all individual values in your array.
You can use array_count_values() for that, it returns you the number of occurances of each value where the value itself is the key:
foreach (array_count_values($_SESSION) as $value => $count) {
echo 'sum of ' . $value . ' values: ' . ($value * $count) . "<br>";
}
No idea where the POST variables fit in though...

Related

HTML form to CSV and insert into database

I currently have a site that uploads a CSV and displays it in a preview table as a form that can be edited (if any CSV values are wrong).
It works great, but I need to insert this into a database table with any edits that are made within the form. I have an array that puts the uploaded CSV into this form, but now I think I need to create a new CSV from this form and submit THAT CSV into the database. I've seen some tutorials but I'm unclear on how to do it from this table/form.
Here is the code for the existing preview form:
if(isset($_POST['preview']))
{
ini_set('auto_detect_line_endings', true);
$file = $_FILES["file"]["tmp_name"];
$handle = fopen($file, "r");
$maxPreviewRows = PHP_INT_MAX; // this will be ~2 billion on 32-bit system, or ~9 quintillion on 64-bit system
$hasHeaderRow = true;
?><form><?
echo '<table>';
/*WE WILL NEED TO QA CONDITIONS AND HIGHLIGHT IN RED HERE. ALSO NEED BORDER STYLINGS*/
if ($hasHeaderRow) {
$headerRow = fgetcsv($handle);
echo '<thead><tr>';
foreach($headerRow as $value) {
echo "<th>$value</th>";
}
echo '</tr></thead>';
}
echo '<tbody>';
$rowCount = 0;
while ($row = fgetcsv($handle)) {
echo '<tr>';
foreach($row as $value) {
echo "<td>$value</td>";
}
echo '</tr>';
if (++$rowCount > $maxPreviewRows) {
break;
}
}
echo '</tbody></table>';
}
?></form>
Since this doesn't have values for each field in the form, I'm unsure of the best way to create a CSV from this and insert it into my staging table.
UPDATE - the below code is my first attempt at using my CSV upload array
if(isset($_POST['submit']))
{
$file = $_FILES["file"]["tmp_name"];
$handle = fopen($file, "r");
$filesop = fgetcsv($handle, 0, ",");
while (($filesop = fgetcsv($handle)) !== FALSE) {
$coldata = array();
$coldata["orderNumber"] = $filesop[0];
$coldata["workOrderPacket"] = $filesop[1];
$coldata["workOrderNum"] = $filesop[2];
$coldata["lowSideMIUNumArriv"] = $filesop[3];
$coldata["lowSideMIUNumDepart"] = $filesop[4];
$coldata["highSideMIUNumArriv"] = $filesop[5];
$coldata["highSideMIUNumDepart"] = $filesop[6];
$coldata["accountNum"] = $filesop[7];
$coldata["filler1"] = $filesop[8];
$coldata["address"] = $filesop[9];
$coldata["filler2"] = $filesop[10];
$coldata["date"] = $filesop[11];
$coldata["utility"] = $filesop[12];
$coldata["serialNumber"] = $filesop[13];
$coldata["serviceName"] = $filesop[14];
$coldata["locationNotes"] = $filesop[15];
$coldata["locationComments"] = $filesop[16];
$coldata["filler3"] = $filesop[17];
$coldata["WaterValveArriv"] = $filesop[18];
$coldata["WaterValveDepart"] = $filesop[19];
$coldata["meterSize"] = $filesop[20];
$coldata["meterType"] = $filesop[21];
$coldata["manufacturer"] = $filesop[22];
$coldata["registration"] = $filesop[23];
$coldata["technician"] = $filesop[24];
$coldata["linePressurePSI"] = $filesop[25];
$coldata["filler4"] = $filesop[26];
$coldata["filler5"] = $filesop[27];
$coldata["lowSideRrBefore"] = $filesop[28];
$coldata["highSideRrBefore"] = $filesop[29];
$coldata["lowSideRrAfter"] = $filesop[30];
$coldata["highSideRrAfter"] = $filesop[31];
$coldata["vgOxygen"] = $filesop[32];
$coldata["vgCombustGas"] = $filesop[33];
$coldata["vgCarbonMon"] = $filesop[34];
$coldata["vgHydroSulf"] = $filesop[35];
$coldata["test1TestRateGPM"] = $filesop[36];
$coldata["test1MeterVol"] = $filesop[37];
$coldata["test1TesterVol"] = $filesop[38];
$coldata["test1Accuracy"] = $filesop[39];
$coldata["test1CorrectAcc"] = $filesop[40];
$coldata["test2TestRateGPM"] = $filesop[41];
$coldata["test2MeterVol"] = $filesop[42];
$coldata["test2TesterVol"] = $filesop[43];
$coldata["test2Accuracy"] = $filesop[44];
$coldata["test2CorrectAcc"] = $filesop[45];
$coldata["test3TestRateGPM"] = $filesop[46];
$coldata["test3MeterVol"] = $filesop[47];
$coldata["test3TesterVol"] = $filesop[48];
$coldata["test3Accuracy"] = $filesop[49];
$coldata["test3CorrectAcc"] = $filesop[50];
$coldata["test4TestRateGPM"] = $filesop[51];
$coldata["test4MeterVol"] = $filesop[52];
$coldata["test4TesterVol"] = $filesop[53];
$coldata["test4Accuracy"] = $filesop[54];
$coldata["test4CorrectAcc"] = $filesop[55];
$coldata["test5TestRateGPM"] = $filesop[56];
$coldata["test5MeterVol"] = $filesop[57];
$coldata["test5TesterVol"] = $filesop[58];
$coldata["test5Accuracy"] = $filesop[59];
$coldata["test5CorrectAcc"] = $filesop[60];
$coldata["test6TestRateGPM"] = $filesop[61];
$coldata["test6MeterVol"] = $filesop[62];
$coldata["test6TesterVol"] = $filesop[63];
$coldata["test6Accuracy"] = $filesop[64];
$coldata["test6CorrectAcc"] = $filesop[65];
$coldata["test7TestRateGPM"] = $filesop[66];
$coldata["test7MeterVol"] = $filesop[67];
$coldata["test7TesterVol"] = $filesop[68];
$coldata["test7Accuracy"] = $filesop[69];
$coldata["test7CorrectAcc"] = $filesop[70];
$coldata["test8TestRateGPM"] = $filesop[71];
$coldata["test8MeterVol"] = $filesop[72];
$coldata["test8TesterVol"] = $filesop[73];
$coldata["test8Accuracy"] = $filesop[74];
$coldata["test8CorrectAcc"] = $filesop[75];
$coldata["inletValveLoc"] = $filesop[76];
$coldata["inletValveSize"] = $filesop[77];
$coldata["InletValveType"] = $filesop[78];
$coldata["inletValveCond"] = $filesop[79];
$coldata["outletValveLoc"] = $filesop[80];
$coldata["outletValveSize"] = $filesop[81];
$coldata["outletValveType"] = $filesop[82];
$coldata["outletValveCond"] = $filesop[83];
$coldata["bypassValveLoc"] = $filesop[84];
$coldata["bypassValveSize"] = $filesop[85];
$coldata["bypassValveType"] = $filesop[86];
$coldata["bypassValveCond"] = $filesop[87];
$coldata["vaultLength"] = $filesop[88];
$coldata["vaultWidth"] = $filesop[89];
$coldata["vaultHeight"] = $filesop[90];
$coldata["meterLocation"] = $filesop[91];
$coldata["testPort"] = $filesop[92];
$coldata["testPortInstalled"] = $filesop[93];
$coldata["testPortSize"] = $filesop[94];
$coldata["picture"] = $filesop[95];
$coldata["timeTested"] = $filesop[96];
$coldata["comments"] = $filesop[97];
$coldata["testResults"] = $filesop[98];
$coldata["retest"] = $filesop[99];
$coldata["test1TestRateGPM2"] = $filesop[100];
$coldata["test1MeterVol2"] = $filesop[101];
$coldata["test1TesterVol2"] = $filesop[102];
$coldata["test1Accuracy2"] = $filesop[103];
$coldata["test1CorrectAcc2"] = $filesop[104];
$coldata["test2TestRateGPM2"] = $filesop[105];
$coldata["test2MeterVol2"] = $filesop[106];
$coldata["test2TesterVol2"] = $filesop[107];
$coldata["test2Accuracy2"] = $filesop[108];
$coldata["test2CorrectAcc2"] = $filesop[109];
$coldata["test3TestRateGPM2"] = $filesop[110];
$coldata["test3MeterVol2"] = $filesop[111];
$coldata["test3TesterVol2"] = $filesop[112];
$coldata["test3Accuracy2"] = $filesop[113];
$coldata["test3CorrectAcc2"] = $filesop[114];
$coldata["test4TestRateGPM2"] = $filesop[115];
$coldata["test4MeterVol2"] = $filesop[116];
$coldata["test4TesterVol2"] = $filesop[117];
$coldata["test4Accuracy2"] = $filesop[118];
$coldata["test4CorrectAcc2"] = $filesop[119];
$coldata["test5TestRateGPM2"] = $filesop[120];
$coldata["test5MeterVol2"] = $filesop[121];
$coldata["test5TesterVol2"] = $filesop[122];
$coldata["test5Accuracy2"] = $filesop[123];
$coldata["test5CorrectAcc2"] = $filesop[124];
$coldata["test6TestRateGPM2"] = $filesop[125];
$coldata["test6MeterVol2"] = $filesop[126];
$coldata["test6TesterVol2"] = $filesop[127];
$coldata["test6Accuracy2"] = $filesop[128];
$coldata["test6CorrectAcc2"] = $filesop[129];
$coldata["test7TestRateGPM2"] = $filesop[130];
$coldata["test7MeterVol2"] = $filesop[131];
$coldata["test7TesterVol2"] = $filesop[132];
$coldata["test7Accuracy2"] = $filesop[133];
$coldata["test7CorrectAcc2"] = $filesop[134];
$coldata["test8TestRateGPM2"] = $filesop[135];
$coldata["test8MeterVol2"] = $filesop[136];
$coldata["test8TesterVol2"] = $filesop[137];
$coldata["test8Accuracy2"] = $filesop[138];
$coldata["test8CorrectAcc2"] = $filesop[139];
$coldata["filler6"] = $filesop[140];
$coldata["filler7"] = $filesop[141];
$coldata["filler8"] = $filesop[142];
$coldata["filler9"] = $filesop[143];
$coldata["filler10"] = $filesop[144];
$coldata["filler11"] = $filesop[145];
$coldata["filler12"] = $filesop[146];
$coldata["serviceAddCorrect"] = $filesop[147];
$coldata["serviceLoccCorrect"] = $filesop[148];
$coldata["meterNumberCorrect"] = $filesop[149];
$coldata["lowRegisterCorrect"] = $filesop[150];
$coldata["lowRegisterType"] = $filesop[151];
$coldata["lowRegisterSize"] = $filesop[152];
$coldata["highRegisterCorrect"] = $filesop[153];
$coldata["highRegisterSize"] = $filesop[154];
$coldata["highRegisterType"] = $filesop[155];
$coldata["meterLidType"] = $filesop[156];
$coldata["meterLidMaterial"] = $filesop[157];
$coldata["lidFit"] = $filesop[158];
$coldata["lidCondition"] = $filesop[159];
$coldata["antennaeMountCor"] = $filesop[160];
$coldata["antennaePosition"] = $filesop[161];
$coldata["registerCondition"] = $filesop[162];
$coldata["MIUwire"] = $filesop[163];
$coldata["registerPinArriv"] = $filesop[164];
$coldata["registerPinDepart"] = $filesop[165];
$coldata["vaultType"] = $filesop[166];
$coldata["vaultSafe"] = $filesop[167];
$coldata["vaultLadder"] = $filesop[168];
$coldata["workOrderType"] = $filesop[169];
$coldata["workOrderLocation"] = $filesop[170];
$coldata["completeMeter"] = $filesop[171];
$coldata["ume"] = $filesop[172];
$coldata["discChamber"] = $filesop[173];
$coldata["turbineChamber"] = $filesop[174];
$coldata["automaticValve"] = $filesop[175];
$coldata["strainer"] = $filesop[176];
$coldata["lowRegister"] = $filesop[177];
$coldata["highRegister"] = $filesop[178];
$coldata["miu"] = $filesop[179];
$coldata["antennae"] = $filesop[180];
$coldata["calibrationVane"] = $filesop[181];
$coldata["meterLeakRepaired"] = $filesop[182];
$coldata["workOrderType2"] = $filesop[183];
$coldata["strainerPresent"] = $filesop[184];
$coldata["apparentLeak"] = $filesop[185];
$coldata["leakLocation"] = $filesop[186];
$coldata["leakType"] = $filesop[187];
$coldata["locateBypassValve"] = $filesop[188];
$coldata["locateInletValve"] = $filesop[189];
$coldata["locateOutletValve"] = $filesop[190];
$coldata["PreformShutdown"] = $filesop[191];
$coldata["turnOnWater"] = $filesop[192];
$coldata["repairLid"] = $filesop[193];
$coldata["repairVault"] = $filesop[194];
$coldata["repairLadder"] = $filesop[195];
$coldata["repairLeak"] = $filesop[196];
$coldata["repairBypassValve"] = $filesop[197];
$coldata["repairInletValve"] = $filesop[198];
$coldata["repairOutletValve"] = $filesop[199];
$coldata["latitude"] = $filesop[200];
$coldata["longitude"] = $filesop[201];
$coldata["onsiteSurveyTestCost"] = $filesop[202];
$coldata["onsiteSurveyTestRepairCost"] = $filesop[203];
$coldata["offsiteSurveyTestCost"] = $filesop[204];
$coldata["offsiteSurveyTestRepairCost"] = $filesop[205];
$coldata["onsiteTestOnlyCost"] = $filesop[206];
$coldata["onsiteTestRepairOnlyCost"] = $filesop[207];
$coldata["onsiteRepairOnly"] = $filesop[208];
$coldata["testPort2"] = $filesop[209];
$coldata["repairCompleteMeterReplacement"] = $filesop[210];
$coldata["repairCompleteMeterReplacementLaborCost"] = $filesop[211];
$coldata["umeCost"] = $filesop[212];
$coldata["umeLaborCost"] = $filesop[213];
$coldata["rotatingLowSideDiskChamber"] = $filesop[214];
$coldata["rotatingLowSideDiskChamberLaborCost"] = $filesop[215];
$coldata["turbineChamberCost"] = $filesop[216];
$coldata["turbineChamberLaborCost"] = $filesop[217];
$coldata["automaticValveCost"] = $filesop[218];
$coldata["automaticValveLaborCost"] = $filesop[219];
$coldata["strainerCost"] = $filesop[220];
$coldata["strainerLaborCost"] = $filesop[221];
$coldata["lowRegisterCost"] = $filesop[222];
$coldata["lowRegisterLaborCost"] = $filesop[223];
$coldata["highRegisterCost"] = $filesop[224];
$coldata["highRegisterLaborCost"] = $filesop[225];
$coldata["miuCost"] = $filesop[226];
$coldata["miuLaborCost"] = $filesop[227];
$coldata["totalCost"] = $filesop[228];

array_multisort how to mount with foreach

I need to mount an array_multisort with the values from one array.
I tryied to mount a string concated and call on the array_multidimensional like here:
function ordenar_matriz_ultima_posicion_por_distancia($matriz_up,$m_vehiculo_distancias){
$total_vehiculos=count($matriz_up[id_vehiculo]);
//resetear las keys de vehiculos para coger bien los kms y asignarlos
$a_vehiculo_distancia = array_values($m_vehiculo_distancias);
$ordenar = array();
foreach ($a_vehiculo_distancia as $key) {
$ordenar[] = $key;
}
sort($m_vehiculo_distancias);
$string= "";
$ultim_key = end(array_keys($matriz_up));
foreach ($matriz_up as $key => $valor) {
if ($key != $ultim_key) $string.= $matriz_up[$key].',';
else $string.= $matriz_up[$key];
$aaa = '$matriz_up[$key]';
}
echo $string;
echo "<br>";
array_multisort($ordenar, SORT_ASC, $string);
for($i=0;$i<$total_vehiculos;$i++){
$matriz_up['cercanos'][$i] = $m_vehiculo_distancias[$i];
echo $matriz_up['id_vehiculo'][$i]."<br>";
echo $matriz_up['fecha_gps'][$i]."<br>";
echo $matriz_up['id_tipo_posicion'][$i]."<br>";
echo $matriz_up['cercanos'][$i]."<br>";
echo $matriz_up['vaina'][$i]."<br>";
echo "------------<br>";
}
return $matriz_up;
}
$matriz_up['id_vehiculo'][0] = 9;
$matriz_up['fecha_gps'][0] = '2014';
$matriz_up['id_tipo_posicion'][0] = 11111;
$matriz_up['cercanos'][0] = 0;
$matriz_up['vaina'][0] = 12345;
$matriz_up['id_vehiculo'][1] = 3;
$matriz_up['fecha_gps'][1] = '2015';
$matriz_up['id_tipo_posicion'][1] = 22222;
$matriz_up['cercanos'][1] = 0;
$matriz_up['vaina'][1] = 5555;
$matriz_up['id_vehiculo'][2] = 1;
$matriz_up['fecha_gps'][2] = '2016';
$matriz_up['id_tipo_posicion'][2] = 33333;
$matriz_up['cercanos'][2] = 0;
$matriz_up['vaina'][2] = 988;
$matriz_up['id_vehiculo'][3] = 4;
$matriz_up['fecha_gps'][3] = '2017';
$matriz_up['id_tipo_posicion'][3] = 44444;
$matriz_up['cercanos'][3] = 0;
$matriz_up['vaina'][3] = 777;
$m_vehiculo_distancias[9] = 345;
$m_vehiculo_distancias[3] = 712;
$m_vehiculo_distancias[1] = 10;
$m_vehiculo_distancias[4] = 35;
ordenar_matriz_ultima_posicion_por_distancia($matriz_up,$m_vehiculo_distancias);
With this array_multisort works, but i need to take all the key without put manually..
array_multisort($ordenar, SORT_ASC, $matriz_up['id_vehiculo'], $matriz_up['fecha_gps'], $matriz_up['id_tipo_posicion'], $matriz_up['vaina'] );
Try this code:
<?php
$matriz_up = $m_vehiculo_distancias = array();
$matriz_up['id_vehiculo'][0] = 9;
$matriz_up['fecha_gps'][0] = '2014';
$matriz_up['id_tipo_posicion'][0] = 11111;
$matriz_up['cercanos'][0] = 0;
$matriz_up['vaina'][0] = 12345;
$matriz_up['id_vehiculo'][1] = 3;
$matriz_up['fecha_gps'][1] = '2015';
$matriz_up['id_tipo_posicion'][1] = 22222;
$matriz_up['cercanos'][1] = 0;
$matriz_up['vaina'][1] = 5555;
$matriz_up['id_vehiculo'][2] = 1;
$matriz_up['fecha_gps'][2] = '2016';
$matriz_up['id_tipo_posicion'][2] = 33333;
$matriz_up['cercanos'][2] = 0;
$matriz_up['vaina'][2] = 988;
$matriz_up['id_vehiculo'][3] = 4;
$matriz_up['fecha_gps'][3] = '2017';
$matriz_up['id_tipo_posicion'][3] = 44444;
$matriz_up['cercanos'][3] = 0;
$matriz_up['vaina'][3] = 777;
$m_vehiculo_distancias[9] = 345;
$m_vehiculo_distancias[3] = 712;
$m_vehiculo_distancias[1] = 10;
$m_vehiculo_distancias[4] = 35;
function sortArray($arrayToSortParam, $orderArray)
{
$result = array();
$arrayToSort = $arrayToSortParam;
$keys = array_keys($arrayToSort);
asort($orderArray, true);
$newSort = $cercanos = array();
foreach($orderArray as $key => $value)
{
$newSort[] = array_keys($arrayToSort['id_vehiculo'], $key)[0];
$cercanos[] = $orderArray[$key];
}
foreach($keys as $keyName)
{
uksort($arrayToSort[$keyName], function($key1, $key2) use ($newSort) {
return (array_search($key1, $newSort) > array_search($key2, $newSort));
});
}
$arrayToSort['cercanos'] = $cercanos;
//reset indexes
foreach($keys as $keyName)
{
$arrayToSort[$keyName] = array_values($arrayToSort[$keyName]);
}
return $arrayToSort;
}
echo '<pre>';
//print_r($matriz_up);
//print_r($m_vehiculo_distancias);
print_r(sortArray($matriz_up, $m_vehiculo_distancias)); //this is result
Working fiddle: CLICK!

How to remove the last line of CSV file

<?php
$row = 0;
$handle = fopen("data20150804.log", "r");
while (!feof($handle) ) {
$line_of_text = fgetcsv($handle, 1024, ",");
$num = count($line_of_text);
$timeStamp = $line_of_text[0];
$visibility = $line_of_text[1];
$pressure = $line_of_text[2];
$rain = $line_of_text[3];
$temperature = $line_of_text[4];
$humidity = $line_of_text[5];
$dewpoint = $line_of_text[6];
$R1windSpeed = $line_of_text[7];
$R1wSpeed3s = $line_of_text[8];
$R1wSpeed2min = $line_of_text[9];
$R1wSpeed10min = $line_of_text[10];
$R1windDirDeg = $line_of_text[11];
$R1wDirDeg3s = $line_of_text[12];
$R1wDirDeg2min = $line_of_text[13];
$R1wDirDeg10min = $line_of_text[14];
$R2windSpeed = $line_of_text[15];
$R2wSpeed3s = $line_of_text[16];
$R2wSpeed2min = $line_of_text[17];
$R2wSpeed10min = $line_of_text[18];
$R2windDirDeg = $line_of_text[19];
$R2wDirDeg3s = $line_of_text[20];
$R2wDirDeg2min = $line_of_text[21];
$R2wDirDeg10min = $line_of_text[22];
$gust = $line_of_text[23];
//$row++;
}
fclose($handle);
?>
my csv file is keeping generate the data in a few second.
I want to remove the last row of the csv because my system is reading the last row data and my last row is blank.
If you check the documentation for fgetcsv you'll see the note about blank line returns. So if you do a check after:
$line_of_text = fgetcsv($handle, 1024, ",");
Along the lines of:
if (count($line_of_text) == 0 || $line_of_text[0] === null) {
continue;
}
This should do what you need.
Try this one
<?php
$row = 0;
$file = new SplFileObject("data20150804.log.csv");
$file->setFlags(SplFileObject::READ_CSV);
$file->setFlags(SplFileObject::SKIP_EMPTY);
foreach ($file as $line_of_text) {
$num = count($line_of_text);
$timeStamp = $line_of_text[0];
$visibility = $line_of_text[1];
$pressure = $line_of_text[2];
$rain = $line_of_text[3];
$temperature = $line_of_text[4];
$humidity = $line_of_text[5];
$dewpoint = $line_of_text[6];
$R1windSpeed = $line_of_text[7];
$R1wSpeed3s = $line_of_text[8];
$R1wSpeed2min = $line_of_text[9];
$R1wSpeed10min = $line_of_text[10];
$R1windDirDeg = $line_of_text[11];
$R1wDirDeg3s = $line_of_text[12];
$R1wDirDeg2min = $line_of_text[13];
$R1wDirDeg10min = $line_of_text[14];
$R2windSpeed = $line_of_text[15];
$R2wSpeed3s = $line_of_text[16];
$R2wSpeed2min = $line_of_text[17];
$R2wSpeed10min = $line_of_text[18];
$R2windDirDeg = $line_of_text[19];
$R2wDirDeg3s = $line_of_text[20];
$R2wDirDeg2min = $line_of_text[21];
$R2wDirDeg10min = $line_of_text[22];
$gust = $line_of_text[23];
}
?>
I hope it will help full for you.

How do i fill an objects attributes dynamically?

I am trying to save an object with a variable amount of "cols". The number of cols is equal to the number of headers. This is how the code looked before:
if(isset($_POST['submit'])){
$sub = new Sub();
$sub->product_id = $_POST['product_id'];
$sub->col1 = $_POST['col1'];
$sub->col2 = $_POST['col2'];
$sub->col3 = $_POST['col3'];
$sub->col4 = $_POST['col4'];
$sub->col5 = $_POST['col5'];
$sub->col6 = $_POST['col6'];
$sub->col7 = $_POST['col7'];
$sub->col8 = $_POST['col8'];
$sub->col9 = $_POST['col9'];
$sub->col10 = $_POST['col10'];
$sub->col11 = $_POST['col11'];
$sub->col12 = $_POST['col12'];
$sub->col13 = $_POST['col13'];
$sub->col14 = $_POST['col14'];
$sub->col15 = $_POST['col15'];
This is how I want it to look:
if(isset($_POST['submit'])){
$sub = new Sub();
$sub->product_id = $_POST['product_id'];
$i = 0;
foreach($headers as $header){
$i++ ;
$sub->col.$i = $_POST['col'.$i];
}
How do I pass the variable $i into the object's attributes? $sub->(col.$i) ? $sub->(col{$i}) ? Please help me figure this out =) Thank you
Try this:
$sub = new Sub();
$sub->product_id = $_POST['product_id'];
for($i = 1; $i <= count($headers); ++$i)
$sub->{'col' . $i} = $_POST['col' . $i];
But, this is really not the way that the columns should be stored in the Sub object, you should use an array:
$sub->columns = array();
for($i = 1; $i <= count($headers); ++$i) {
$sub->columns[] = $_POST['col' . $i];
}
You have to use {} :
$sub->{'col' . $i} = ...
$field = "col$i";
$sub->$field = "whatver"
I'd prefer a setter method.
class Sub {
public function set($attribute, $value) {
$this->$attribute = $value;
}
}
Now you can do:
foreach($_POST as $key => $value) {
$sub->set($key, $value)
}
Or without the loose coupling:
$i = 1;
while($value = $_POST['col' . $i]) {
$sub->set('col' . $i, $value);
$i++;
}

List images from directory by function

I'm using this function:
function getmyimages($qid){
$imgdir = 'modules/Projects/uploaded_project_images/'. $qid .''; // the directory, where your images are stored
$allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes you want to show
$dimg = opendir($imgdir);
while($imgfile = readdir($dimg))
{
if(in_array(strtolower(substr($imgfile,-3)),$allowed_types))
{
$a_img[] = $imgfile;
sort($a_img);
reset ($a_img);
}
}
$totimg = count($a_img); // total image number
for($x=0; $x < $totimg; $x++)
{
$size = getimagesize($imgdir.'/'.$a_img[$x]);
// do whatever
$halfwidth = ceil($size[0]/2);
$halfheight = ceil($size[1]/2);
$mytest = 'name: '.$a_img[$x].' width: '.$size[0].' height: '.$size[1].'<br />'. $a_img[$x]. '';
}
return $mytest;
}
And I call this function between a while row as:
$sql_select = $db->sql_query('SELECT * from '.$prefix.'_projects WHERE topic=\''.$cid.'\'');
OpenTable();
while ($row2 = $db->sql_fetchrow($sql_select)){
$qid = $row2['qid'];
$project_query = $db->sql_query('SELECT p.uid, p.uname, p.subject, p.story, p.storyext, p.date, p.topic, p.pdate, p.materials, p.bidoptions, p.projectduration, pd.id_duration, pm.material_id, pbo.bidid, pc.cid FROM ' . $prefix . '_projects p, ' . $prefix . '_projects_duration pd, ' . $prefix . '_project_materials pm, ' . $prefix . '_project_bid_options pbo, ' . $prefix . '_project_categories pc WHERE p.topic=\''.$cid.'\' and p.qid=\''.$qid.'\' and p.bidoptions=pbo.bidid and p.materials=pm.material_id and p.projectduration=pd.id_duration');
while ($project_row = $db->sql_fetchrow($project_query)) {
//$qid = $project_row['qid'];
$uid = $project_row['uid'];
$uname = $project_row['uname'];
$subject = $project_row['subject'];
$story = $project_row['story'];
$storyext = $project_row['storyext'];
$date = $project_row['date'];
$topic = $project_row['topic'];
$pdate = $project_row['pdate'];
$materials = $project_row['materials'];
$bidoptions = $project_row['bidoptions'];
$projectduration = $project_row['projectduration'];
//Get the topic name
$topic_query = $db->sql_query('SELECT cid,title from '.$prefix.'_project_categories WHERE cid =\''.$cid.'\'');
while ($topic_row = $db->sql_fetchrow($topic_query)) {
$topic_id = $topic_row['cid'];
$topic_title = $topic_row['title'];
}
//Get the material text
$material_query = $db->sql_query('SELECT material_id,material_name from '.$prefix.'_project_materials WHERE material_id =\''.$materials.'\'');
while ($material_row = $db->sql_fetchrow($material_query)) {
$material_id = $material_row['material_id'];
$material_name = $material_row['material_name'];
}
//Get the bid methode
$bid_query = $db->sql_query('SELECT bidid,bidname from '.$prefix.'_project_bid_options WHERE bidid =\''.$bidoptions.'\'');
while ($bid_row = $db->sql_fetchrow($bid_query)) {
$bidid = $bid_row['bidid'];
$bidname = $bid_row['bidname'];
}
//Get the project duration
$duration_query = $db->sql_query('SELECT id_duration,duration_value,duration_alias from '.$prefix.'_projects_duration WHERE id_duration =\''.$projectduration.'\'');
while ($duration_row = $db->sql_fetchrow($duration_query)) {
$id_duration = $duration_row['id_duration'];
$duration_value = $duration_row['duration_value'];
$duration_alias = $duration_row['duration_alias'];
}
}
echo '<br/><b>id</b>--->' .$qid. '<br/><b>uid</b>--->' .$uid. '<br/><b>username</b>--->' .$uname. '<br/><b>subject</b>--->'.$subject. '<br/><b>story1</b>--->'.$story. '<br/><b>story2</b>--->'.$storyext. '<br/><b>postdate</b>--->'.$date. '<br/><b>categorie</b>--->'.$topic_title . '<br/><b>project start</b>--->'.$pdate. '<br/><b>materials</b>--->'.$material_name. '<br/><b>bid methode</b>--->'.$bidname. '<br/><b>project duration</b>--->'.$duration_alias.'<br /><br /><br/><b>image url</b>--->'.getmyimages($qid).'<br /><br />';
}
CloseTable();
The result outputs only the "last" file from the directories.
If I do an echo instead of a return $mytest;, it reads the whole directory but ruins the output.
You are always overwriting $mytest in your loop:
for($x=0; $x < $totimg; $x++)
{
$size = getimagesize($imgdir.'/'.$a_img[$x]);
// do whatever
$halfwidth = ceil($size[0]/2);
$halfheight = ceil($size[1]/2);
$mytest = 'name: '.$a_img[$x].' width: '.$size[0].' height: '.$size[1].'<br />'. $a_img[$x]. '';
}
return $mytest;
So after the loop, $mytest contains the last value that was generated in the loop. You might want to return an array instead:
$mytest = array();
for($x=0; $x < $totimg; $x++)
{
$size = getimagesize($imgdir.'/'.$a_img[$x]);
// do whatever
$halfwidth = ceil($size[0]/2);
$halfheight = ceil($size[1]/2);
$mytest[] = 'name: '.$a_img[$x].' width: '.$size[0].' height: '.$size[1].'<br />'. $a_img[$x]. '';
}
return $mytest;
But of course you also have to change your other function in order to deal with the returned array.

Categories