Parsing user input via AJAX and PHP - php

Now, this isn't a "debug my codez for me plz" question. I've spent... I don't even want to think how long on trying to fix this issue.
My problem is that I am executing this php page that is, ideally, examining a string and converting it to a more secure format since this page is to store passwords for accounts.
What I'm aiming at with this code below is to slice the string repeatedly at each character and then evaluate it to my key. It does this for the whole string length. What's been being returned to me is '0'. I don't know how the system is even getting this value.
Maybe I'm using the substr() function wrong? Also, I'm open to a completely different method of parsing the string, such as using a RegExp. Thank you for your help, guys!
Code:
<?php
error_reporting(0);
#region Initial
$accstr = "apple";
$accn = "scourge";
//Key
$a[0] = "#"; //These variables are for conversion; indexes of this array correspond to those
$a[1] = "!"; //of the other array ($ind)
$a[2] = "#";
$a[3] = "%$";
$a[4] = "%";
$a[5] = "!#";
$a[6] = "&*";
$a[7] = "^";
$a[8] = "##";
$a[9] = "&^";
$a[10] = "&";
$a[11] = "%~";
$a[12] = "!%";
$a[13] = "!$";
$a[14] = "*#";
$a[15] = "#*";
$a[16] = "~";
$a[17] = "~&";
$a[18] = "``";
$a[19] = "/^";
$a[20] = "%`";
$a[21] = "~~";
$a[22] = "`~";
$a[23] = "%%";
$a[24] = "~!";
$a[25] = "~#";
$a[26] = "``#";
$a[27] = "``!";
$a[28] = "``#";
$a[29] = "``%$";
$a[30] = "``%";
$a[31] = "``!#";
$a[32] = "``&*";
$a[33] = "``^";
$a[34] = "``##";
$a[35] = "``&^";
$a[36] = "&&^#";
$a[37] = "~#!";
$a[38] = "!#&#";
$a[39] = "%~~$";
$a[40] = "%`%";
$a[41] = "!^~#";
$a[42] = "&#$*";
$a[43] = "^**&";
$a[44] = "#%#`";
$a[45] = "&``!#^";
$a[46] = "&**~&";
$a[47] = "%|~";
$a[48] = "!-|~%";
$a[49] = "!$~";
$a[50] = "*/#";
$a[51] = "#%*";
$a[52] = "|~";
$ind[0] = "a";//These are used to tell what's being looked at in the string
$ind[1] = "b";
$ind[2] = "c";
$ind[3] = "d";
$ind[4] = "e";
$ind[5] = "f";
$ind[6] = "g";
$ind[7] = "h";
$ind[8] = "i";
$ind[9] = "j";
$ind[10] = "k";
$ind[11] = "l";
$ind[12] = "m";
$ind[13] = "n";
$ind[14] = "o";
$ind[15] = "p";
$ind[16] = "q";
$ind[17] = "r";
$ind[18] = "s";
$ind[19] = "t";
$ind[20] = "u";
$ind[21] = "v";
$ind[22] = "w";
$ind[23] = "x";
$ind[24] = "y";
$ind[25] = "z";
$ind[26] = "0";
$ind[27] = "1";
$ind[28] = "2";
$ind[29] = "3";
$ind[30] = "4";
$ind[31] = "5";
$ind[32] = "6";
$ind[33] = "7";
$ind[34] = "8";
$ind[35] = "9";
$ind[36] = "~";
$ind[37] = "!";
$ind[38] = "#";
$ind[39] = "#";
$ind[40] = "$";
$ind[41] = "%";
$ind[42] = "^";
$ind[43] = "&";
$ind[44] = "*";
$ind[45] = "(";
$ind[46] = ")";
$ind[47] = "_";
$ind[48] = "+";
$ind[49] = "`";
$ind[50] = "-";
$ind[51] = "=";
$ind[52] = "?";
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('pwDB.xml');
$finln = "";
#endregion
#region Create coded password
$pwlen = strlen($accstr);
for($cnter=1;$cnter<=$pwlen;$cnter++)
{
$a1 = substr($accstr,$cnter,1);
for($cnter2=1;$cnter2<=52;$cnter2++)
{
if($a1==$ind[$cnter2])
{
$finln += $a[$cnter2];
}
}
}
#endregion
#region Send finln
$newpw = $xml->createElement($accn);
$newpw->appendChild($xml->createElement('password', $finln));
$xml->getElementsByTagName('cache')->item(0)->appendChild($newpw);
file_put_contents("pwDB.xml",$xml->saveXML());
print $finln;
#endregion
?>

So typical password hashing is one way - if you need two way then youre talking about encryption which is different.
Normally for hashing youd do something like the following, though id urge not to take this verbatim but to research some on your own so oyu understand what youre doing and the concepts involved:
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('pwDB.xml');
$account = 'someuser';
$password = 'passw0rd';
// your salt can be a constant that you never change, or can be user specific
// if you make it user specific then you need to store it as well as the password
$salt = "1j0i90#$t%";
$hash = hash('sha256', $password . $salt);
$acct = $xml->createElement($account);
$pw = $xml->createElement('password', $salt);
$acct->appendChild($pw);
$xml->appendChild($acct);
file_put_contents("pwDB.xml",$xml->saveXML());
And then to compare credentials like for a login you would do:
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('pwDB.xml');
$account = 'someuser';
$password = 'passw0rd';
$salt = "1j0i90#$t%";
$hash = hash('sha256', $password . $salt);
$xpath = new DOMXPath($xml);
// look up by account name - assuming these are unique
$accountNodes = $xpath->query('//'.$account);
if($accountNodes->length) {
$accountNode = $accountNodes->item(0);
$pwNodes = $xpath->query('//password', $accountNode);
if($pwNodes->length) {
$pwNode = $pwNodes->item(0);
if($hash === (string) $pwNode) {
// authentication OK!
}
}
}

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

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];

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...

PHP - HEX to EBCDIC Conversion

How do i convert an Hex value to EBCDIC in PHP?
What is the most practical way to do it?
What i did so far is hex to Ascii:
Hex : 313233343536373832313332333435
Ascii: 123456782132345
By using:
hex2bin($foo);
strtr should do the job, if you provide ebcdic to ascii mapping.
An example mapping for 3-letters alphabet:
$ebcdicSet = "\x81\x84\xa2";
$asciiSet = "ads";
$ebcdic = "\x81\xa2\x84\x81\xa2\x84"; // 6 bytes
$ascii = strtr($ebcdic, $ebcdicSet, $asciiSet);
echo $ascii; // outputs "asdasd"
If your input is not binary hex, but a sting with hex representation, you need to hex2bin it first:
$ebcdic = "81a28481a284"; // 12 bytes
$ascii = strtr(hex2bin($ebcdic), $ebcdicSet, $asciiSet);
echo $ascii; // outputs "asdasd"
If using built in function from EBCDIC to ASCII
function ebcdic_to_ascii($ebcdic_hexstring /*expecting something like F0F1....*/)
{
//need to delcare it to avoid warning
$ebcd_ascii= null;
// here come all the conversion
$ebcd_ascii["4A"] = "¢";
$ebcd_ascii["4B"] = ".";
$ebcd_ascii["4C"] = "<";
$ebcd_ascii["4D"] = "(";
$ebcd_ascii["4E"] = "+";
$ebcd_ascii["4F"] = "|";
$ebcd_ascii["5A"] = "!";
$ebcd_ascii["5B"] = "$";
$ebcd_ascii["5C"] = "*";
$ebcd_ascii["5D"] = ")";
$ebcd_ascii["5E"] = ";";
$ebcd_ascii["5F"] = "¬";
$ebcd_ascii["60"] = "-";
$ebcd_ascii["61"] = "/";
$ebcd_ascii["6A"] = "¦";
$ebcd_ascii["6B"] = ",";
$ebcd_ascii["6C"] = "%";
$ebcd_ascii["6D"] = "_";
$ebcd_ascii["6E"] = ">";
$ebcd_ascii["6F"] = "?";
$ebcd_ascii["79"] = "`";
$ebcd_ascii["7A"] = ":";
$ebcd_ascii["7B"] = "#";
$ebcd_ascii["7C"] = "#";
$ebcd_ascii["7D"] = "'";
$ebcd_ascii["7E"] = "=";
$ebcd_ascii["7F"] = " '' ";
$ebcd_ascii["81"] = "a";
$ebcd_ascii["82"] = "b";
$ebcd_ascii["83"] = "c";
$ebcd_ascii["84"] = "d";
$ebcd_ascii["85"] = "e";
$ebcd_ascii["86"] = "f";
$ebcd_ascii["87"] = "g";
$ebcd_ascii["88"] = "h";
$ebcd_ascii["89"] = "i";
$ebcd_ascii["91"] = "j";
$ebcd_ascii["92"] = "k";
$ebcd_ascii["93"] = "l";
$ebcd_ascii["94"] = "m";
$ebcd_ascii["95"] = "n";
$ebcd_ascii["96"] = "o";
$ebcd_ascii["97"] = "p";
$ebcd_ascii["98"] = "q";
$ebcd_ascii["99"] = "r";
$ebcd_ascii["A1"] = "~";
$ebcd_ascii["A2"] = "s";
$ebcd_ascii["A3"] = "t";
$ebcd_ascii["A4"] = "u";
$ebcd_ascii["A5"] = "v";
$ebcd_ascii["A6"] = "w";
$ebcd_ascii["A7"] = "x";
$ebcd_ascii["A8"] = "y";
$ebcd_ascii["A9"] = "z";
$ebcd_ascii["C0"] = "{";
$ebcd_ascii["C1"] = "A";
$ebcd_ascii["C2"] = "B";
$ebcd_ascii["C3"] = "C";
$ebcd_ascii["C4"] = "D";
$ebcd_ascii["C5"] = "E";
$ebcd_ascii["C6"] = "F";
$ebcd_ascii["C7"] = "G";
$ebcd_ascii["C7"] = "H";
$ebcd_ascii["C9"] = "I";
$ebcd_ascii["D0"] = "}";
$ebcd_ascii["D1"] = "J";
$ebcd_ascii["D2"] = "K";
$ebcd_ascii["D3"] = "L";
$ebcd_ascii["D4"] = "M";
$ebcd_ascii["D5"] = "N";
$ebcd_ascii["D6"] = "O";
$ebcd_ascii["D7"] = "P";
$ebcd_ascii["D8"] = "Q";
$ebcd_ascii["D9"] = "R";
$ebcd_ascii["E0"] = "\\";
$ebcd_ascii["E2"] = "S";
$ebcd_ascii["E3"] = "T";
$ebcd_ascii["E4"] = "U";
$ebcd_ascii["E5"] = "V";
$ebcd_ascii["E6"] = "W";
$ebcd_ascii["E7"] = "X";
$ebcd_ascii["E8"] = "Y";
$ebcd_ascii["E9"] = "Z";
$ebcd_ascii["F0"] = "0";
$ebcd_ascii["F1"] = "1";
$ebcd_ascii["F2"] = "2";
$ebcd_ascii["F3"] = "3";
$ebcd_ascii["F4"] = "4";
$ebcd_ascii["F5"] = "5";
$ebcd_ascii["F6"] = "6";
$ebcd_ascii["F7"] = "7";
$ebcd_ascii["F8"] = "8";
$ebcd_ascii["F9"] = "9";
$ebcd_ascii["FF"] = "E0";
//end of conversion
// loop until there is no more conversion.
$asciiOut = "";
while(strlen($ebcdic_hexstring)>1)//F0F1F2F3F -> F1F2F3F
{
$thisEbcdic = substr($ebcdic_hexstring, 0, 2);//F0->F1
//if(!is_null($ebcd_ascii[$thisEbcdic]))
$asciiOut = $asciiOut.$ebcd_ascii[$thisEbcdic];//0->01
$ebcdic_hexstring = substr($ebcdic_hexstring, 2);//F1F2F3F -> F2F3F
}
return $asciiOut;
}
?>

How I can do this PHP If Statement in right way?

I have 50 variables in php. I want to check each of them and if they true then add 2 points in a variable called $point. I am new so I write a few lines but I think I am doing wrong way.
$strenght_point = 0;
if($f_name){$strenght_point++;}
if($l_name){$strenght_point + 2;}
if($full_name){$strenght_point + 2;}
How can I do it right way.Thanks
Update my full function is here...
It's Codeigniter Controller Function
Hope you guys understand well now
function strength_scale() {
$user_id = $this->uri->segment(2);
$user_name = $this->uri->segment(3);
$query = $this->db->get_where('aoa_user', array('id' => $user_id, 'username' => $user_name));
foreach ($query->result() as $row){
$f_name = $row->f_name;
$l_name = $row->l_name;
$full_name = $row->full_name;
$username = $row->username;
$alias_name = $row->alias_name;
$gender = $row->gender;
$country = $row->country;
$avatar = $row->avatar;
$cover_photo = $row->cover_photo;
$email = $row->email;
$skill = $row->skill;
$other_skills = $row->other_skills;
$ex_time = $row->ex_time;
$about = $row->about;
$company = $row->company;
$company_position = $row->company_position;
$phone = $row->phone;
$facebook = $row->facebook;
$facebook_page = $row->facebook_page;
$google_plus = $row->google_plus;
$twitter = $row->twitter;
$youtube = $row->youtube;
$skype = $row->skype;
$linkedin = $row->linkedin;
$website = $row->website;
$latitude = $row->latitude;
$longitude = $row->longitude;
$verification = $row->verification;
}
$strength_point = 0;
if($f_name){$strength_point++;}
if($l_name){$strength_point + 2;}
if($full_name){$strength_point + 2;}
}
Create an array with the variables instead.
https://3v4l.org/FYTtG
$arr = array("f_name" => true, "l_name" => true, "full_name" => true);
$strength=0;
Foreach($arr as $var){
if($var) $strength = $strength+2;
}
Echo $strength;
As Rizier123 said, you need to increment your strength variable correcly.
You could write a simple function that would accept one of your 50 variables and return the strength increment:
function defineStrength($param)
{
if ($param) {
return 2;
}
return 0;
}
$strength = 0;
$f_name = true;
$l_name = false;
$full_name = false;
$strength += defineStrength($f_name);
$strength += defineStrength($l_name);
$strength += defineStrength($full_name);
However, an array would be a better way to go, as Andreas mentionned.
In your question update you said you use CodeIgniter. As the documentation states you can return the query result as a pure array.
So you could further develop like that :
function defineStrengthFromArray(array $row)
{
$strength = 0;
foreach ($row as $param) {
$strength += defineStrength($param);
}
return $strength;
}
foreach ($query->result_array() as $row){
$strength = defineStrengthFromArray($row);
}

Categories