HTML form to CSV and insert into database - php
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];
Related
XML data works well on local machine but do not work online
I have code like this: $ares_ico_fin = ""; $ares_dic_fin = ""; $ares_firma_fin = ""; $ares_ulice_fin = ""; $ares_cp1_fin = ""; $ares_cp2_fin = ""; $ares_mesto_fin = ""; $ares_psc_fin = ""; $ares_stav_fin = ""; $file = #file_get_contents("http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_bas.cgi?ico=".$_POST["ico_ajax_send"]); if($file) { $xml = #simplexml_load_string($file); } if($xml) { $ns = $xml->getDocNamespaces(); $data = $xml->children($ns['are']); $el = $data->children($ns['D'])->VBAS; if (strval($el->ICO) == $_POST["ico_ajax_send"]) { $ares_ico_fin = strval($el->ICO); $ares_dic_fin = strval($el->DIC); $ares_firma_fin = strval($el->OF); $ares_ulice_fin = strval($el->AA->NU); $ares_cp1_fin = strval($el->AA->CD); $ares_cp2_fin = strval($el->AA->CO); if($ares_cp2_fin != ""){ $ares_cp_fin = $ares_cp1_fin."/".$ares_cp2_fin; }else{ $ares_cp_fin = $ares_cp1_fin; } $ares_mesto_fin = strval($el->AA->N); $ares_psc_fin = strval($el->AA->PSC); $ares_stav_fin = 1; } It works fine on my local machine, but I do not get any data if tries it on remoteserver... I read help like: Fixing error with file_get_contents permission denied with no success, thanks for ideas how to solve it, have a nice day subii
How to find and make sum of different numbers from array?
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...
i am trying to make a quiz in moodle externally via webservice
I am trying to make a webservice in moodle that can be called externally. The webservice makes an entry in the database table mdl_quiz. But the quiz does not show up on the front-end. Here is my code of the externallib file: global $CFG, $DB; $params = self::validate_parameters( self::create_quiz_parameters(), ['quiz' => $quiz] ); foreach ($params['quiz'] as $quiz) { $courseid = $quiz['courseid']; $quizname = $quiz['quizname']; $intro = $quiz['intro']; $attempts = $quiz['attempts']; $timeopen = $quiz['timeopen']; $timeclose = $quiz['timeclose']; $quiz = new stdClass(); $quiz->course = $courseid; $quiz->name = $quizname; $quiz->timeopen = $timeopen; $quiz->timeclose = $timeclose; $quiz->attempts = $attempts; $quiz->intro = $intro; $rqa = $DB->insert_record('quiz', $quiz); if (isset($rqa)) { $moduleid = $DB->get_field('modules', 'id', ['name' => 'quiz'], MUST_EXIST); $instanceid = 50; $sectionid = 1; $newcm = new stdClass(); $newcm->course = $courseid; $newcm->module = $moduleid; $newcm->section = $sectionid; $newcm->added = time(); $newcm->instance = $instanceid; $newcm->visible = 1; $newcm->groupmode = 0; $newcm->groupingid = 0; $newcm->groupmembersonly = 0; $newcm->showdescription = 0; $cmid = $DB->insert_record('course_modules', $newcm); } }
You should use add_moduleinfo() instead of inserting records manually. This will do most of the work for you. For an example see Create Moodle activities programmatically
mySQL cannot INSERT into one of the tables
I inherited this code and I can ot get it to INSERT into the first table "property_tbl" The upload, resize and INSERT of the image part works fine. But I am not getting any errors. And I have been staring at it for days now. <?php session_start(); if(empty($_SESSION[ss_user_id])){ $header("Location: login.php"); } include_once("includes/config.inc.php"); include_once("includes/function.inc.php"); $pro_type_id = $_REQUEST["pro_type_id"]; $pro_sid = $_REQUEST["pro_sid"]; $pro_name = $_REQUEST["pro_name"]; $pro_name_jp = $_REQUEST["pro_name_jp"]; $pro_title = $_REQUEST["pro_title"]; $pro_title_jp = $_REQUEST["pro_title_jp"]; $pro_desc = htmlentities($_REQUEST["pro_desc"],ENT_QUOTES); $pro_desc_jp = htmlentities($_REQUEST["pro_desc_jp"],ENT_QUOTES); $pro_notes = $_REQUEST["pro_notes"]; $pro_notes_jp = $_REQUEST["pro_notes_jp"]; $pro_address = $_REQUEST["pro_address"]; $pro_address_jp = $_REQUEST["pro_address_jp"]; $pro_email = $_REQUEST["pro_email"]; $pro_email_jp = $_REQUEST["pro_email_jp"]; $pro_contact = $_REQUEST["pro_contact"]; $pro_contact_jp = $_REQUEST["pro_contact_jp"]; $pro_price_low = $_REQUEST["pro_price_low"]; $pro_price_rent_high = $_REQUEST["pro_price_rent_high"]; $pro_price_rent_long = $_REQUEST["pro_price_rent_long"]; $pro_size_ext = $_REQUEST["pro_size_ext"]; $pro_ownership = $_REQUEST["pro_ownership"]; $pro_ownership_jp = $_REQUEST["pro_ownership_jp"]; $pro_house_stands = $_REQUEST["pro_house_stands"]; $pro_house_stands_jp = $_REQUEST["pro_house_stands_jp"]; $pro_years_left = $_REQUEST["pro_years_left"]; $pro_house = $_REQUEST["pro_house"]; $pro_house_jp = $_REQUEST["pro_house_jp"]; $pro_years_left_house = $_REQUEST["pro_years_left_house"]; $pro_common = $_REQUEST["pro_common"]; $pro_common_jp = $_REQUEST["pro_common_jp"]; $pro_price = $_REQUEST["pro_price"]; $pro_price_rent = $_REQUEST["pro_price_rent"]; $pro_price_rai = $_REQUEST["pro_price_rai"]; $size_land_rai = $_REQUEST["size_land_rai"]; $size_land_ngan = $_REQUEST["size_land_ngan"]; $size_land_wah = $_REQUEST["size_land_wah"]; $pro_bed = $_REQUEST["pro_bed"]; $pro_bath = $_REQUEST["pro_bath"]; $pro_land = $_REQUEST["pro_land"]; $pro_size = $_REQUEST["pro_size"]; $pro_view = $_REQUEST["pro_view"]; $pro_view_jp = $_REQUEST["pro_view_jp"]; $pro_furnished = $_REQUEST["pro_furnished"]; $pro_furnished_jp = $_REQUEST["pro_furnished_jp"]; $pro_furniture = $_REQUEST["pro_furniture"]; $pro_furniture_jp = $_REQUEST["pro_furniture_jp"]; $pro_kitchen = $_REQUEST["pro_kitchen"]; $pro_kitchen_jp = $_REQUEST["pro_kitchen_jp"]; $pro_internet = $_REQUEST["pro_internet"]; $pro_internet_jp = $_REQUEST["pro_internet_jp"]; $pro_aircon = $_REQUEST["pro_aircon"]; $pro_aircon_jp = $_REQUEST["pro_aircon_jp"]; $pro_pool = $_REQUEST["pro_pool"]; $pro_pool_jp = $_REQUEST["pro_pool_jp"]; $pro_parking = $_REQUEST["pro_parking"]; $pro_parking_jp = $_REQUEST["pro_parking_jp"]; $pro_distancetown = $_REQUEST["pro_distancetown"]; $pro_distancebeach = $_REQUEST["pro_distancebeach"]; $pro_features = $_REQUEST["pro_features"]; $pro_features_jp = $_REQUEST["pro_features_jp"]; $pro_loc = $_REQUEST["pro_loc"]; $pro_loc_jp = $_REQUEST["pro_loc_jp"]; $pro_status = $_REQUEST["pro_status"]; $pro_reccom = $_REQUEST["pro_reccom"]; $query = "INSERT INTO property_tbl SET pro_type_id = '$pro_type_id', pro_sid = '$pro_sid', pro_name = '$pro_name', pro_name_jp = '$pro_name_jp', pro_title = '$pro_title', pro_title_jp = '$pro_title_jp', pro_desc = '$pro_desc', pro_desc_jp = '$pro_desc_jp', pro_notes = '$pro_notes', pro_notes_jp = '$pro_notes_jp', pro_address = '$pro_address', pro_address_jp = '$pro_address_jp', pro_email = '$pro_email', pro_email_jp = '$pro_email_jp', pro_contact = '$pro_contact', pro_contact_jp = '$pro_contact_jp', pro_price_low = '$pro_price_low', pro_price_rent_high = '$pro_price_rent_high', pro_price_rent_long = '$pro_price_rent_long', pro_size_ext = '$pro_size_ext', pro_ownership = '$pro_ownership', pro_ownership_jp = '$pro_ownership_jp', pro_house_stands = '$pro_house_stands', pro_house_stands_jp = '$pro_house_stands_jp', pro_years_left = '$pro_years_left', pro_house = '$pro_house', pro_house_jp = '$pro_house_jp', pro_years_left_house = '$pro_years_left_house', pro_common = '$pro_common', pro_common_jp = '$pro_common_jp', pro_price = '$pro_price', pro_price_rent = '$pro_price_rent', pro_price_rai = '$pro_price_rai', size_land_rai = '$size_land_rai', size_land_ngan = '$size_land_ngan', size_land_wah = '$size_land_wah', pro_bed = '$pro_bed', pro_bath = '$pro_bath', pro_land = '$pro_land', pro_size = '$pro_size', pro_view = '$pro_view', pro_view_jp = '$pro_view_jp', pro_furnished = '$pro_furnished', pro_furnished_jp = '$pro_furnished_jp', pro_furniture = '$pro_furniture', pro_furniture_jp = '$pro_furniture_jp', pro_kitchen = '$pro_kitchen', pro_kitchen_jp = '$pro_kitchen_jp', pro_internet = '$pro_internet', pro_internet_jp = '$pro_internet_jp' pro_aircon = '$pro_aircon', pro_aircon_jp = '$pro_aircon_jp', pro_pool = '$pro_pool', pro_pool_jp = '$pro_pool_jp', pro_parking = '$pro_parking', pro_parking_jp = '$pro_parking_jp', pro_distancetown = '$pro_distancetown', pro_distancebeach = '$pro_distancebeach', pro_features = '$pro_features', pro_features_jp = '$pro_features_jp', pro_loc = '$pro_loc', pro_loc_jp = '$pro_loc_jp', pro_status = '$pro_status', pro_reccom = '$pro_reccom', pro_adddate = '$date_now', pro_addby = '$_SESSION[ss_user_id]'"; mysql_query($query); echo "<br /><br />".$query; $sql = "SELECT * FROM property_tbl ORDER BY pro_id DESC LIMIT 1"; $result = mysql_query($sql); $line = mysql_fetch_array($result, MYSQL_ASSOC); $NewId = $line["pro_id"]; foreach ($_FILES["pictures"]["error"] as $key => $error){ if($error == UPLOAD_ERR_OK){ $uploads_normal = '../images/pro-photo/normals'; $uploads_thumb = '../images/pro-photo/thumbnails'; $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; $pic_name = $_FILES["pictures"]["name"][$key]; $ext = strtolower(end(explode('.',$pic_name))); $imgname = $nextId.md5(rand() * time()).".".$ext; /* resize normal photo*/ $width1=800; $size1=GetimageSize($tmp_name); $height1=round($width1*$size1[1]/$size1[0]); if($ext == "jpg" or $ext=="jpeg"){ $images_orig1 = ImageCreateFromJPEG($tmp_name); }elseif($ext =="png"){ $images_orig1 = ImageCreateFromPNG($tmp_name); }elseif($ext =="gif"){ $images_orig1 = ImageCreateFromGIF($tmp_name); } $photoX1 = ImagesX($images_orig1); $photoY1 = ImagesY($images_orig1); $images_fin1 = ImageCreateTrueColor($width1, $height1); ImageCopyResampled($images_fin1, $images_orig1, 0, 0, 0, 0, $width1+1, $height1+1, $photoX1, $photoY1); if ($ext == "jpg" or $ext == "jpeg"){ ImageJPEG($images_fin1,"$uploads_normal/".$imgname); }elseif($ext == "png"){ ImagePNG($images_fin1,"$uploads_normal/".$imgname); }elseif($ext == "gif"){ ImageGIF($images_fin1,"$uploads_normal/".$imgname); } ImageDestroy($images_orig1); ImageDestroy($images_fin1); $width=303; $size=GetimageSize($tmp_name); $height=round($width*$size[1]/$size[0]); if($ext == "jpg" or $ext=="jpeg"){ $images_orig = ImageCreateFromJPEG($tmp_name); }elseif($ext =="png"){ $images_orig = ImageCreateFromPNG($tmp_name); }elseif($ext =="gif"){ $images_orig = ImageCreateFromGIF($tmp_name); } $photoX = ImagesX($images_orig); $photoY = ImagesY($images_orig); $images_fin = ImageCreateTrueColor($width, $height); ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY); if ($ext == "jpg" or $ext == "jpeg"){ ImageJPEG($images_fin,"$uploads_thumb/".$imgname); }elseif($ext == "png"){ ImagePNG($images_fin,"$uploads_thumb/".$imgname); }elseif($ext == "gif"){ ImageGIF($images_fin,"$uploads_thumb/".$imgname); } ImageDestroy($images_orig); ImageDestroy($images_fin); $sql_img = "INSERT INTO pro_img (img_id, pro_id, img_name) VALUES ('', '$NewId', '$imgname')"; if(mysql_query($sql_img)){ // print "<script language='javascript'>alert('Add Property complete.');</script>"; // print "<meta http-equiv='refresh' content='0;URL=index.php'>"; } } } print "<script language='javascript'>alert('Add Property complete.');</script>"; print "<meta http-equiv='refresh' content='0;URL=index.php'>"; ?> config.inc.php is just the database connection, which works fine. Any help would be great. Thank you
I would echo the query and try it in sequel pro, you probably have misspelt some table names. Also I would never type all this code, instead foreach over $REQUEST and append it to the query $query = "INSERT INTO property_tbl SET"; foreach($REQUEST as $key => $value) { $query .= $key . "= '". $value."',"; } and delete the comma after the loop :)
Thanks for your ideas. But it turns out that staring at it did work eventually. The problem was a missing comma after $pro_internet_jp pro_internet = '$pro_internet', pro_internet_jp = '$pro_internet_jp' pro_aircon = '$pro_aircon', All working fine now. Thanks
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.