Using switch statements with Mysql and php PDO - php

So I can read two tables by using the following sql statement
$result = $dbc->query("SELECT table1.*, table2.* FROM table1 INNER JOIN table2 WHERE table1.column_name = 'variable' AND table2.column_name = 'variable2'")
Now I want to use a switch statement to read from each row determined by the variable in the columns. so for example
$q = $result->fetch();
switch ($q['column_name']) {
case "i":
echo ///results
break;
case "e":
echo // different results
break;
default:
//whatever code
}
I'm trying to echo two types of images from the two different tables based on the string under the column_name. But the switch method doesn't echo anything so I must not be calling be reading string from the column_name, any help?
Here is the var_dump that I'm getting:
array(46) { ["ad_id"]=> string(1) "1" [0]=> string(1) "1" ["user_id"]=> string(1) "0" [1]=> string(1) "0" ["global"]=> string(0) "" [2]=> string(0) "" ["title"]=> string(7) "Testing" [3]=> string(7) "Testing" ["categories"]=> string(15) "music altnindie" [4]=> string(4) "blog" ["location"]=> string(9) "australia" [5]=> string(7) "austria" ["description"]=> string(16) "New King in town" [6]=> string(8) "A new ad" ["link"]=> string(12) "www.link.com" [7]=> string(12) "www.link.com" ["link_clicks"]=> string(1) "0" [8]=> string(1) "0" ["image_file_path"]=> string(43) "******** 2015-03-07 00.48.33.png" [9]=> string(59) "adimages/11014891_660400880771405_6666691584554052971_n.jpg" ["thumb_file_path"]=> string(52) "********* 2015-03-07 00.48.33.png" [10]=> string(68) "*******-11014891_660400880771405_6666691584554052971_n.jpg" ["date_post"]=> string(19) "2015-05-15 11:14:32" [11]=> string(19) "2015-05-15 12:36:05" ["share_count"]=> string(1) "0" [12]=> string(1) "0" ["type"]=> string(5) "audio" [13]=> string(5) "image" [14]=> string(1) "1" [15]=> string(1) "0" [16]=> string(0) "" [17]=> string(7) "Testing" [18]=> string(15) "music altnindie" [19]=> string(9) "australia" [20]=> string(16) "New King in town" ["plays"]=> string(1) "0" [21]=> string(1) "0" [22]=> string(12) "www.link.com" [23]=> string(1) "0" ["audio_file_path"]=> string(50) "*******/audioI0n7Dr" [24]=> string(50) "*****/audioI0n7Dr" [25]=> string(43) "**** 2015-03-07 00.48.33.png" [26]=> string(52) "***** 2015-03-07 00.48.33.png" [27]=> string(19) "2015-05-15 11:14:32" [28]=> string(1) "0" [29]=> string(5) "audio" }

Related

csv header row as array key

I'm able to get the file into an array, but I'd like to get the key's to be the first row. Here's is the curl command:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://url.com/download");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "X-App-Key: xxxxx";
$headers[] = "X-User-Key: yyyy";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
$data = array_map("str_getcsv", preg_split('/\r*\n+|\r+/', $result));
return $data;
Here is the csv that is returned:
"Date,Campaign,Campaign Id,Impressions,Clicks,CTR,Platform Cost,Search Cost,Demographic Cost,Contextual Cost,Behavioral Cost,Geographic Cost,Segment Cost,Media Cost,CPM,Total Spend,ECPC,Click Sales,Click Leads,Click Signups,Click Other,View Sales,View Leads,View Signups,View Other,CPA,Weighted Actions 2017-05-16,Company,326606,,,,,,,,,,,,,,,0,0,0,0,0,0,0,2,,2.00 2017-05-18,Company,326606,,,,,,,,,,,,,,,0,0,0,0,0,0,0,2,,2.00 2017-05-19,Company,326606,,,,,,,,,,,,,,,0,0,0,0,0,0,0,2,,2.00 2017-05-20,Company,326606,,,,,,,,,,,,,,,0,0,0,0,0,0,0,2,,2.00 2017-05-28,Company,326606,,,,,,,,,,,,,,,0,0,0,0,0,0,0,1,,1.00 2017-06-02,Company,326606,,,,,,,,,,,,,,,0,0,0,0,0,0,0,1,,1.00 2017-05-10,Company,326606,16877,41,0.002429,0.770373,0.000000,0.0,0.000000,0.0,0.770373,0.000000,1.540785,3.081682,52.009548,1.268526,,,,,,,,,, 2017-05-11,Company,326606,17001,50,0.002941,0.764775,0.000000,0.0,0.000000,0.0,0.764775,0.000000,1.529592,3.059288,52.010956,1.040219,,,,,,,,,, 2017-05-12,Company,326606,16814,44,0.002617,0.773364,0.000000,0.0,0.000000,0.0,0.773364,0.000000,1.546763,3.093651,52.016652,1.182197,,,,,,,,,, 2017-05-13,Company,326606,16880,32,0.001896,0.770245,0.000000,0.0,0.000000,0.0,0.770245,0.000000,1.540520,3.081195,52.010570,1.625330,,,,,,,,,, 2017-05-14,Company,326606,17045,32,0.001877,0.762742,0.000000,0.0,0.000000,0.0,0.762742,0.000000,1.525543,3.051212,52.007907,1.625247,0,0,0,0,0,0,0,1,52.0079,1.00 2017-05-15,Company,326606,7294,10,0.001371,0.781126,0.000000,0.0,0.000000,0.0,0.781126,0.000000,1.562252,3.124683,22.791441,2.279144,,,,,,,,,, 2017-06-03,Company,559435,30186,42,0.001391,0.690422,0.690422,0.0,0.000000,0.0,0.000000,0.000000,1.380910,2.761773,83.366884,1.984926,0,0,0,1,0,0,0,1,41.6834,2.00 2017-06-04,Company,559435,29952,25,0.000835,0.695962,0.695962,0.0,0.000000,0.0,0.000000,0.000000,1.391983,2.783917,83.383894,3.335356,0,0,0,0,0,0,0,12,6.9487,12.00 2017-06-05,Company,559435,29863,39,0.001306,0.697854,0.697854,0.0,0.000000,0.0,0.000000,0.000000,1.395772,2.791494,83.362371,2.137497,0,0,0,4,2,1,0,15,3.7892,22.00 2017-06-06,Company,559435,29859,40,0.001340,0.698055,0.698055,0.0,0.000000,0.0,0.000000,0.000000,1.396160,2.792283,83.374777,2.084369,0,1,0,4,2,1,0,21,2.8750,29.00 2017-06-07,Company,559435,29968,22,0.000734,0.695653,0.695653,0.0,0.000000,0.0,0.000000,0.000000,1.391359,2.782683,83.391458,3.790521,0,0,0,1,0,0,0,25,3.2074,26.00 2017-06-08,Company,559435,29967,41,0.001368,0.695284,0.695284,0.0,0.000000,0.0,0.000000,0.000000,1.390634,2.781195,83.344075,2.032782,0,0,0,2,1,0,0,23,3.2055,26.00 2017-06-09,Company,559435,14689,8,0.000545,0.686755,0.686755,0.0,0.000000,0.0,0.000000,0.000000,1.373565,2.747041,40.351291,5.043911,0,0,0,0,0,2,0,21,1.7544,23.00 ,,,286395,426,0.001487,0.718754,0.471867,0.0,0.000000,0.0,0.246886,0.000000,1.437560,2.875126,823.421824,1.932915,0,1,0,12,5,4,0,129,5.4531,151.00
And the returned array is but I'd like "date" "clicks" to be used as the keys so I can simply call "$data[Click Lead]":
array(22) {
[0]=>
array(27) {
[0]=>
string(4) "Date"
[1]=>
string(8) "Campaign"
[2]=>
string(11) "Campaign Id"
[3]=>
string(11) "Impressions"
[4]=>
string(6) "Clicks"
[5]=>
string(3) "CTR"
[6]=>
string(13) "Platform Cost"
[7]=>
string(11) "Search Cost"
[8]=>
string(16) "Demographic Cost"
[9]=>
string(15) "Contextual Cost"
[10]=>
string(15) "Behavioral Cost"
[11]=>
string(15) "Geographic Cost"
[12]=>
string(12) "Segment Cost"
[13]=>
string(10) "Media Cost"
[14]=>
string(3) "CPM"
[15]=>
string(11) "Total Spend"
[16]=>
string(4) "ECPC"
[17]=>
string(11) "Click Sales"
[18]=>
string(11) "Click Leads"
[19]=>
string(13) "Click Signups"
[20]=>
string(11) "Click Other"
[21]=>
string(10) "View Sales"
[22]=>
string(10) "View Leads"
[23]=>
string(12) "View Signups"
[24]=>
string(10) "View Other"
[25]=>
string(3) "CPA"
[26]=>
string(16) "Weighted Actions"
}
[1]=>
array(27) {
[0]=>
string(10) "2017-05-16"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
[6]=>
string(0) ""
[7]=>
string(0) ""
[8]=>
string(0) ""
[9]=>
string(0) ""
[10]=>
string(0) ""
[11]=>
string(0) ""
[12]=>
string(0) ""
[13]=>
string(0) ""
[14]=>
string(0) ""
[15]=>
string(0) ""
[16]=>
string(0) ""
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "2"
[25]=>
string(0) ""
[26]=>
string(4) "2.00"
}
[2]=>
array(27) {
[0]=>
string(10) "2017-05-18"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
[6]=>
string(0) ""
[7]=>
string(0) ""
[8]=>
string(0) ""
[9]=>
string(0) ""
[10]=>
string(0) ""
[11]=>
string(0) ""
[12]=>
string(0) ""
[13]=>
string(0) ""
[14]=>
string(0) ""
[15]=>
string(0) ""
[16]=>
string(0) ""
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "2"
[25]=>
string(0) ""
[26]=>
string(4) "2.00"
}
[3]=>
array(27) {
[0]=>
string(10) "2017-05-19"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
[6]=>
string(0) ""
[7]=>
string(0) ""
[8]=>
string(0) ""
[9]=>
string(0) ""
[10]=>
string(0) ""
[11]=>
string(0) ""
[12]=>
string(0) ""
[13]=>
string(0) ""
[14]=>
string(0) ""
[15]=>
string(0) ""
[16]=>
string(0) ""
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "2"
[25]=>
string(0) ""
[26]=>
string(4) "2.00"
}
[4]=>
array(27) {
[0]=>
string(10) "2017-05-20"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
[6]=>
string(0) ""
[7]=>
string(0) ""
[8]=>
string(0) ""
[9]=>
string(0) ""
[10]=>
string(0) ""
[11]=>
string(0) ""
[12]=>
string(0) ""
[13]=>
string(0) ""
[14]=>
string(0) ""
[15]=>
string(0) ""
[16]=>
string(0) ""
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "2"
[25]=>
string(0) ""
[26]=>
string(4) "2.00"
}
[5]=>
array(27) {
[0]=>
string(10) "2017-05-28"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
[6]=>
string(0) ""
[7]=>
string(0) ""
[8]=>
string(0) ""
[9]=>
string(0) ""
[10]=>
string(0) ""
[11]=>
string(0) ""
[12]=>
string(0) ""
[13]=>
string(0) ""
[14]=>
string(0) ""
[15]=>
string(0) ""
[16]=>
string(0) ""
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "1"
[25]=>
string(0) ""
[26]=>
string(4) "1.00"
}
[6]=>
array(27) {
[0]=>
string(10) "2017-06-02"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
[6]=>
string(0) ""
[7]=>
string(0) ""
[8]=>
string(0) ""
[9]=>
string(0) ""
[10]=>
string(0) ""
[11]=>
string(0) ""
[12]=>
string(0) ""
[13]=>
string(0) ""
[14]=>
string(0) ""
[15]=>
string(0) ""
[16]=>
string(0) ""
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "1"
[25]=>
string(0) ""
[26]=>
string(4) "1.00"
}
[7]=>
array(27) {
[0]=>
string(10) "2017-05-10"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(5) "16877"
[4]=>
string(2) "41"
[5]=>
string(8) "0.002429"
[6]=>
string(8) "0.770373"
[7]=>
string(8) "0.000000"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.770373"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.540785"
[14]=>
string(8) "3.081682"
[15]=>
string(9) "52.009548"
[16]=>
string(8) "1.268526"
[17]=>
string(0) ""
[18]=>
string(0) ""
[19]=>
string(0) ""
[20]=>
string(0) ""
[21]=>
string(0) ""
[22]=>
string(0) ""
[23]=>
string(0) ""
[24]=>
string(0) ""
[25]=>
string(0) ""
[26]=>
string(0) ""
}
[8]=>
array(27) {
[0]=>
string(10) "2017-05-11"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(5) "17001"
[4]=>
string(2) "50"
[5]=>
string(8) "0.002941"
[6]=>
string(8) "0.764775"
[7]=>
string(8) "0.000000"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.764775"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.529592"
[14]=>
string(8) "3.059288"
[15]=>
string(9) "52.010956"
[16]=>
string(8) "1.040219"
[17]=>
string(0) ""
[18]=>
string(0) ""
[19]=>
string(0) ""
[20]=>
string(0) ""
[21]=>
string(0) ""
[22]=>
string(0) ""
[23]=>
string(0) ""
[24]=>
string(0) ""
[25]=>
string(0) ""
[26]=>
string(0) ""
}
[9]=>
array(27) {
[0]=>
string(10) "2017-05-12"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(5) "16814"
[4]=>
string(2) "44"
[5]=>
string(8) "0.002617"
[6]=>
string(8) "0.773364"
[7]=>
string(8) "0.000000"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.773364"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.546763"
[14]=>
string(8) "3.093651"
[15]=>
string(9) "52.016652"
[16]=>
string(8) "1.182197"
[17]=>
string(0) ""
[18]=>
string(0) ""
[19]=>
string(0) ""
[20]=>
string(0) ""
[21]=>
string(0) ""
[22]=>
string(0) ""
[23]=>
string(0) ""
[24]=>
string(0) ""
[25]=>
string(0) ""
[26]=>
string(0) ""
}
[10]=>
array(27) {
[0]=>
string(10) "2017-05-13"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(5) "16880"
[4]=>
string(2) "32"
[5]=>
string(8) "0.001896"
[6]=>
string(8) "0.770245"
[7]=>
string(8) "0.000000"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.770245"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.540520"
[14]=>
string(8) "3.081195"
[15]=>
string(9) "52.010570"
[16]=>
string(8) "1.625330"
[17]=>
string(0) ""
[18]=>
string(0) ""
[19]=>
string(0) ""
[20]=>
string(0) ""
[21]=>
string(0) ""
[22]=>
string(0) ""
[23]=>
string(0) ""
[24]=>
string(0) ""
[25]=>
string(0) ""
[26]=>
string(0) ""
}
[11]=>
array(27) {
[0]=>
string(10) "2017-05-14"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(5) "17045"
[4]=>
string(2) "32"
[5]=>
string(8) "0.001877"
[6]=>
string(8) "0.762742"
[7]=>
string(8) "0.000000"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.762742"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.525543"
[14]=>
string(8) "3.051212"
[15]=>
string(9) "52.007907"
[16]=>
string(8) "1.625247"
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "1"
[25]=>
string(7) "52.0079"
[26]=>
string(4) "1.00"
}
[12]=>
array(27) {
[0]=>
string(10) "2017-05-15"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(4) "7294"
[4]=>
string(2) "10"
[5]=>
string(8) "0.001371"
[6]=>
string(8) "0.781126"
[7]=>
string(8) "0.000000"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.781126"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.562252"
[14]=>
string(8) "3.124683"
[15]=>
string(9) "22.791441"
[16]=>
string(8) "2.279144"
[17]=>
string(0) ""
[18]=>
string(0) ""
[19]=>
string(0) ""
[20]=>
string(0) ""
[21]=>
string(0) ""
[22]=>
string(0) ""
[23]=>
string(0) ""
[24]=>
string(0) ""
[25]=>
string(0) ""
[26]=>
string(0) ""
}
[13]=>
array(27) {
[0]=>
string(10) "2017-06-03"
[1]=>
string(25) "Company"
[2]=>
string(6) "559435"
[3]=>
string(5) "30186"
[4]=>
string(2) "42"
[5]=>
string(8) "0.001391"
[6]=>
string(8) "0.690422"
[7]=>
string(8) "0.690422"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.000000"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.380910"
[14]=>
string(8) "2.761773"
[15]=>
string(9) "83.366884"
[16]=>
string(8) "1.984926"
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "1"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "1"
[25]=>
string(7) "41.6834"
[26]=>
string(4) "2.00"
}
[14]=>
array(27) {
[0]=>
string(10) "2017-06-04"
[1]=>
string(25) "Company"
[2]=>
string(6) "559435"
[3]=>
string(5) "29952"
[4]=>
string(2) "25"
[5]=>
string(8) "0.000835"
[6]=>
string(8) "0.695962"
[7]=>
string(8) "0.695962"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.000000"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.391983"
[14]=>
string(8) "2.783917"
[15]=>
string(9) "83.383894"
[16]=>
string(8) "3.335356"
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(2) "12"
[25]=>
string(6) "6.9487"
[26]=>
string(5) "12.00"
}
[15]=>
array(27) {
[0]=>
string(10) "2017-06-05"
[1]=>
string(25) "Company"
[2]=>
string(6) "559435"
[3]=>
string(5) "29863"
[4]=>
string(2) "39"
[5]=>
string(8) "0.001306"
[6]=>
string(8) "0.697854"
[7]=>
string(8) "0.697854"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.000000"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.395772"
[14]=>
string(8) "2.791494"
[15]=>
string(9) "83.362371"
[16]=>
string(8) "2.137497"
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "4"
[21]=>
string(1) "2"
[22]=>
string(1) "1"
[23]=>
string(1) "0"
[24]=>
string(2) "15"
[25]=>
string(6) "3.7892"
[26]=>
string(5) "22.00"
}
[16]=>
array(27) {
[0]=>
string(10) "2017-06-06"
[1]=>
string(25) "Company"
[2]=>
string(6) "559435"
[3]=>
string(5) "29859"
[4]=>
string(2) "40"
[5]=>
string(8) "0.001340"
[6]=>
string(8) "0.698055"
[7]=>
string(8) "0.698055"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.000000"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.396160"
[14]=>
string(8) "2.792283"
[15]=>
string(9) "83.374777"
[16]=>
string(8) "2.084369"
[17]=>
string(1) "0"
[18]=>
string(1) "1"
[19]=>
string(1) "0"
[20]=>
string(1) "4"
[21]=>
string(1) "2"
[22]=>
string(1) "1"
[23]=>
string(1) "0"
[24]=>
string(2) "21"
[25]=>
string(6) "2.8750"
[26]=>
string(5) "29.00"
}
[17]=>
array(27) {
[0]=>
string(10) "2017-06-07"
[1]=>
string(25) "Company"
[2]=>
string(6) "559435"
[3]=>
string(5) "29968"
[4]=>
string(2) "22"
[5]=>
string(8) "0.000734"
[6]=>
string(8) "0.695653"
[7]=>
string(8) "0.695653"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.000000"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.391359"
[14]=>
string(8) "2.782683"
[15]=>
string(9) "83.391458"
[16]=>
string(8) "3.790521"
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "1"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(2) "25"
[25]=>
string(6) "3.2074"
[26]=>
string(5) "26.00"
}
[18]=>
array(27) {
[0]=>
string(10) "2017-06-08"
[1]=>
string(25) "Company"
[2]=>
string(6) "559435"
[3]=>
string(5) "29967"
[4]=>
string(2) "41"
[5]=>
string(8) "0.001368"
[6]=>
string(8) "0.695284"
[7]=>
string(8) "0.695284"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.000000"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.390634"
[14]=>
string(8) "2.781195"
[15]=>
string(9) "83.344075"
[16]=>
string(8) "2.032782"
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "2"
[21]=>
string(1) "1"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(2) "23"
[25]=>
string(6) "3.2055"
[26]=>
string(5) "26.00"
}
[19]=>
array(27) {
[0]=>
string(10) "2017-06-09"
[1]=>
string(25) "Company"
[2]=>
string(6) "559435"
[3]=>
string(5) "14689"
[4]=>
string(1) "8"
[5]=>
string(8) "0.000545"
[6]=>
string(8) "0.686755"
[7]=>
string(8) "0.686755"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.000000"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.373565"
[14]=>
string(8) "2.747041"
[15]=>
string(9) "40.351291"
[16]=>
string(8) "5.043911"
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "2"
[23]=>
string(1) "0"
[24]=>
string(2) "21"
[25]=>
string(6) "1.7544"
[26]=>
string(5) "23.00"
}
[20]=>
array(27) {
[0]=>
string(0) ""
[1]=>
string(0) ""
[2]=>
string(0) ""
[3]=>
string(6) "286395"
[4]=>
string(3) "426"
[5]=>
string(8) "0.001487"
[6]=>
string(8) "0.718754"
[7]=>
string(8) "0.471867"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.246886"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.437560"
[14]=>
string(8) "2.875126"
[15]=>
string(10) "823.421824"
[16]=>
string(8) "1.932915"
[17]=>
string(1) "0"
[18]=>
string(1) "1"
[19]=>
string(1) "0"
[20]=>
string(2) "12"
[21]=>
string(1) "5"
[22]=>
string(1) "4"
[23]=>
string(1) "0"
[24]=>
string(3) "129"
[25]=>
string(6) "5.4531"
[26]=>
string(6) "151.00"
}
[21]=>
array(1) {
[0]=>
NULL
}
}
Use array_combine and extend your code to:
$data = array_map("str_getcsv", preg_split('/\r*\n+|\r+/', $result));
$new_data = [];
// shift first element of $data, we will use it as keys.
$keys = array_shift($data);
foreach ($data as $item) {
// create key => value array
$new_data[] = array_combine($keys, $item);
}
Thanks to #Kazz - in case of warnings like
array_combine(): Both parameters should have an equal number of elements
you need to check if both elements are arrays and theirs sizes equals (with count(), for example).

Json_decode shows empty value

I got a joomla database with a field that has multiple image paths. I want to seperate those paths and read them using json_decode. But when I try my code the image source is empty (where I want the path to be shown)
How I get the data and use json_decode:
$content = "SELECT * FROM `snm_content` WHERE catid = 13";
$contentcon = $conn->query($content);
$contentcr = array();
while ($contentcr[] = $contentcon->fetch_array());
$image = json_decode($content['images'], true);
I got the following line inside a foreach:
<img src="'.$image['image_intro'].'" alt="" class="company_logo">
I don't get any output in the img src. Why is that?
This is how I start the foreach if this is relevant:
foreach($contentcr as $content)
{
Output of $contentcr:
" [6]=> string(1) "1" ["state"]=> string(1) "1" [7]=> string(2) "13" ["catid"]=> string(2) "13" [8]=> string(19) "2015-11-20 08:56:29" ["created"]=> string(19) "2015-11-20 08:56:29" [9]=> string(3) "361" ["created_by"]=> string(3) "361" [10]=> string(0) "" ["created_by_alias"]=> string(0) "" [11]=> string(19) "2015-11-30 11:00:10" ["modified"]=> string(19) "2015-11-30 11:00:10" [12]=> string(3) "361" ["modified_by"]=> string(3) "361" [13]=> string(3) "361" ["checked_out"]=> string(3) "361" [14]=> string(19) "2015-12-02 10:49:59" ["checked_out_time"]=> string(19) "2015-12-02 10:49:59" [15]=> string(19) "2015-11-20 08:56:29" ["publish_up"]=> string(19) "2015-11-20 08:56:29" [16]=> string(19) "0000-00-00 00:00:00" ["publish_down"]=> string(19) "0000-00-00 00:00:00" [17]=> string(209) "{"image_intro":"images\/mobieleairco\/Airco_blog.jpg","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}" ["images"]=> string(209) "{"image_intro":"images\/mobieleairco\/Airco_blog.jpg","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}" [18]=> string(121) "{"urla":false,"urlatext":"","targeta":"","urlb":false,"urlbtext":"","targetb":"","urlc":false,"urlctext":"","targetc":""}" ["urls"]=> string(121) "{"urla":false,"urlatext":"","targeta":"","urlb":false,"urlbtext":"","targetb":"","urlc":false,"urlctext":"","targetc":""}" [19]=> string(593) "{"show_title":"","link_titles":"","show_tags":"","show_intro":"","info_block_position":"","show_category":"","link_category":"","show_parent_category":"","link_parent_category":"","show_author":"","link_author":"","show_create_date":"","show_modify_date":"","show_publish_date":"","show_item_navigation":"","show_icons":"","show_print_icon":"","show_email_icon":"","show_vote":"","show_hits":"","show_noauth":"","urls_position":"","alternative_readmore":"","article_layout":"","show_publishing_options":"","show_article_options":"","show_urls_images_backend":"","show_urls_images_frontend":""}" ["attribs"]=> string(593) "{"show_title":"","link_titles":"","show_tags":"","show_intro":"","info_block_position":"","show_category":"","link_category":"","show_parent_category":"","link_parent_category":"","show_author":"","link_author":"","show_create_date":"","show_modify_date":"","show_publish_date":"","show_item_navigation":"","show_icons":"","show_print_icon":"","show_email_icon":"","show_vote":"","show_hits":"","show_noauth":"","urls_position":"","alternative_readmore":"","article_layout":"","show_publishing_options":"","show_article_options":"","show_urls_images_backend":"","show_urls_images_frontend":""}" [20]=> string(2) "14" ["version"]=> string(2) "14" [21]=> string(1) "3" ["ordering"]=> string(1) "3" [22]=> string(22) "€ 50 (vanaf 2 weken)" ["metakey"]=> string(22) "€ 50 (vanaf 2 weken)" [23]=> string(5) "Slang" ["metadesc"]=> string(5) "Slang" [24]=> string(1) "1" ["access"]=> string(1) "1" [25]=> string(1) "0" ["hits"]=> string(1) "0" [26]=> string(53) "{"robots":"","author":"","rights":"","xreference":""}" ["metadata"]=> string(53) "{"robots":"","author":"","rights":"","xreference":""}" [27]=> string(1) "0" ["featured"]=> string(1) "0" [28]=> string(1) "*" ["language"]=> string(1) "*" [29]=> string(0) "" ["xreference"]=> string(0) "" } [1]=> array(60) { [0]=> string(2) "10" ["id"]=> string(2) "10" [1]=> string(2) "69" ["asset_id"]=> string(2) "69" [2]=> string(16) "Aelia 12 (AL-12)" ["title"]=> string(16) "Aelia 12 (AL-12)" [3]=> string(14) "aelia-12-al-12" ["alias"]=> string(14) "aelia-12-al-12" [4]=> string(297) "
Your variable names where confusing matters, but the answer that you where calling json_decode on the sql string, not the row:
$sql = "SELECT * FROM `snm_content` WHERE catid = 13";
$result = $conn->query($content);
while ($row = $result->fetch_array()){
$image = json_decode($row['images'], true);
echo '<img src="'.$image['image_intro'].'" alt="" class="company_logo">';
}

PHP session saving array with POST values

First sorry for my English.
I have a weird problem using the php $_SESSION Object.
I spent 2 days without find the solution.
I am triying to save a multidimensional array that stores some post values.
I create an array with this values and then i create a multidimensional array containing this arrays.
Page 1 (setvalues in multi array ans save in session)-> GET -> Page 2 read the session.
My code Page 1:
First, save the post values into a object property and validate them.
public function validateData(){
$this->nombres=$_POST["nombre"];
$this->imp_nombres=$_POST["imp_nombre"];
$this->numeros=$_POST["numero"];
$this->imp_numeros=$_POST["imp_numero"];
$this->tallas=$_POST["talla"];
$this->cantidades=$_POST["cantidad"];
$this->productos=$_POST["products"];
$this->equipos=$_POST["equipo"];
if(WSI_Funtions::compareSizes($this->nombres,$this->imp_nombres,$this->numeros,$this->imp_numeros,$this->tallas,$this->cantidades,$this->productos,$this->equipos))
{
$this->isValidModel=true;
$this->saveProductsValues();
}
else{
$this->isValidModel=false;
$this->errorMessage="Los datos no son correctos. Los parametros no coinciden";
}
}
If data is ok, I save those values:
public function saveProductsValues()
{
$this->productsValues=array();
$this->productsValues["names"]=$this->nombres;
$this->productsValues["imp_nombres"]=$this->imp_nombres;
$this->productsValues["numeros"]=$this->numeros;
$this->productsValues["imp_numeros"]=$this->imp_numeros;
$this->productsValues["tallas"]=$this->tallas;
$this->productsValues["cantidades"]=$this->cantidades;
$this->productsValues["productos"]=$this->productos;
$this->productsValues["equipos"]=$this->equipos;
}
Then I save it in a session :
public function saveSessionValues()
{
$_SESSION['customer'] = $this->customerObject;
$_SESSION['productsValues'] =$this->productsValues;
echo var_dump($_SESSION['productsValues']);
}
The saveSessionValues echo print this:
array(8) { ["names"]=> array(12) { [0]=> string(0) "" [1]=> string(12) "ÁNGEL HDEZ." [2]=> string(11) "VUJASINOVIC" [3]=> string(4) "ABIA" [4]=> string(10) "MUTAKABBIR" [5]=> string(8) "PETROVIC" [6]=> string(5) "DOBOS" [7]=> string(4) "HOMS" [8]=> string(6) "CASTRO" [9]=> string(0) "" [10]=> string(0) "" [11]=> string(0) "" } ["imp_nombres"]=> array(12) { [0]=> string(0) "" [1]=> string(1) "1" [2]=> string(1) "1" [3]=> string(1) "1" [4]=> string(1) "1" [5]=> string(1) "1" [6]=> string(1) "1" [7]=> string(1) "1" [8]=> string(1) "1" [9]=> string(0) "" [10]=> string(0) "" [11]=> string(0) "" } ["numeros"]=> array(12) { [0]=> string(1) "7" [1]=> string(1) "8" [2]=> string(1) "9" [3]=> string(0) "" [4]=> string(2) "11" [5]=> string(2) "12" [6]=> string(2) "18" [7]=> string(2) "19" [8]=> string(2) "22" [9]=> string(1) "5" [10]=> string(2) "33" [11]=> string(0) "" } ["imp_numeros"]=> array(12) { [0]=> string(1) "1" [1]=> string(1) "1" [2]=> string(1) "1" [3]=> string(0) "" [4]=> string(1) "1" [5]=> string(1) "1" [6]=> string(1) "1" [7]=> string(1) "1" [8]=> string(1) "1" [9]=> string(1) "1" [10]=> string(1) "1" [11]=> string(0) "" } ["tallas"]=> array(12) { [0]=> string(4) "XXXL" [1]=> string(3) "XXL" [2]=> string(2) "XL" [3]=> string(3) "XXL" [4]=> string(3) "XXL" [5]=> string(3) "XXL" [6]=> string(4) "XXXL" [7]=> string(3) "XXL" [8]=> string(2) "XL" [9]=> string(4) "XXXL" [10]=> string(2) "XL" [11]=> string(3) "XXL" } ["cantidades"]=> array(12) { [0]=> string(1) "1" [1]=> string(1) "1" [2]=> string(1) "1" [3]=> string(1) "1" [4]=> string(3) "145" [5]=> string(1) "1" [6]=> string(1) "1" [7]=> string(1) "1" [8]=> string(1) "1" [9]=> string(1) "1" [10]=> string(1) "1" [11]=> string(1) "1" } ["productos"]=> array(12) { [0]=> string(3) "109" [1]=> string(3) "109" [2]=> string(3) "109" [3]=> string(3) "109" [4]=> string(3) "109" [5]=> string(3) "109" [6]=> string(3) "109" [7]=> string(3) "109" [8]=> string(3) "109" [9]=> string(3) "109" [10]=> string(3) "109" [11]=> string(3) "109" } ["equipos"]=> array(12) { [0]=> string(7) "LEB ORO" [1]=> string(7) "LEB ORO" [2]=> string(7) "LEB ORO" [3]=> string(7) "LEB ORO" [4]=> string(7) "LEB ORO" [5]=> string(7) "LEB ORO" [6]=> string(12) "ES TALLA 4XL" [7]=> string(7) "LEB ORO" [8]=> string(7) "LEB ORO" [9]=> string(12) "ES TALLA 4XL" [10]=> string(7) "LEB ORO" [11]=> string(7) "LEB ORO" } }
My code Page 2 (php tags ommitted):
if (!isset($_SESSION)) { session_start(); }
echo var_dump($_SESSION['productsValues']);
This echo print the next value:
array(8) { ["names"]=> NULL ["imp_nombres"]=> NULL ["numeros"]=> NULL ["imp_numeros"]=> NULL ["tallas"]=> NULL ["cantidades"]=> NULL ["productos"]=> NULL ["equipos"]=> NULL }
The first level array exists, because the subarrays keys are printed, but all the second level arrays are NULL..
Maybe is for use the $_POST value??
I had try to encode the values of the $_POST object, saving only a json string instead of saving a object with the same result, the first nodes in the JSON are the keys of the arrays but the values are "NULL"
Any help please??
Thanks!!
You are over writing the values. Try with -
$_SESSION['productsValues'][] = $this->productsValues;

how to get result from codeigniter model as object

i'm using codeigniter for create a web site. and i need to take result from mysql table as
this
object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(4) ["lengths"]=> NULL ["num_rows"]=> int(12) ["type"]=> int(0) }
but always i got as a result something efferent from this.
when i'm using result_object() in codeigniter i'm getting something like this
array(12) { [0]=> object(stdClass)#24 (4) { ["id"]=> string(1) "1" ["label"]=> string(15) "Web Development" ["link_url"]=> string(0) "" ["parent_id"]=> string(1) "0" } [1]=> object(stdClass)#25 (4) { ["id"]=> string(2) "10" ["label"]=> string(19) "Sales and Marketing" ["link_url"]=> string(0) "" ["parent_id"]=> string(1) "0" } [2]=> object(stdClass)#26 (4) { ["id"]=> string(1) "7" ["label"]=> string(18) "Design and Artwork" ["link_url"]=> string(0) "" ["parent_id"]=> string(1) "0" } [3]=> object(stdClass)#27 (4) { ["id"]=> string(1) "2" ["label"]=> string(16) "Content Creation" ["link_url"]=> string(0) "" ["parent_id"]=> string(1) "0" } [4]=> object(stdClass)#28 (4) { ["id"]=> string(1) "4" ["label"]=> string(19) "OSCommerce projects" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "1" } [5]=> object(stdClass)#29 (4) { ["id"]=> string(1) "3" ["label"]=> string(8) "PHP Jobs" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "1" } [6]=> object(stdClass)#30 (4) { ["id"]=> string(1) "5" ["label"]=> string(22) "Technical Writing Jobs" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "2" } [7]=> object(stdClass)#31 (4) { ["id"]=> string(1) "6" ["label"]=> string(13) "Forum Posting" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "2" } [8]=> object(stdClass)#32 (4) { ["id"]=> string(1) "8" ["label"]=> string(20) "Blog Design Projects" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "7" } [9]=> object(stdClass)#33 (4) { ["id"]=> string(1) "9" ["label"]=> string(24) "Freelance Website Design" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "7" } [10]=> object(stdClass)#34 (4) { ["id"]=> string(2) "11" ["label"]=> string(29) "Internet Marketing Consulting" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(2) "10" } [11]=> object(stdClass)#35 (4) { ["id"]=> string(2) "12" ["label"]=> string(25) "Leads Generation Services" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(2) "10" } }
$this->db->order_by('parent_id','id','ASC');
$query = $this->db->get('dyn_menu');
if ($query->num_rows() > 0) {
$data = $query->result_object();
return $data;
}
how can i get the result as i want?
try using just result(), like:
...
$data = $query->result();
return $data;
...
try this if single record you want, make $is_single = true
if($is_single)
return $query ->row_array();
else
return $query ->result_array();

how do you insert associative array to an oracle table in php

I have this array called lines. When I do var_dump($lines), I get this result back:
array(3) {
[0]=>
array(21) {
[0]=>
string(1) "3"
[1]=>
string(1) "1"
[2]=>
string(1) "0"
[3]=>
string(8) "49992904"
[4]=>
string(6) "283699"
[5]=>
string(1) "9"
[6]=>
string(3) "610"
[7]=>
string(1) "0"
[8]=>
string(1) "0"
[9]=>
string(1) "0"
[10]=>
string(1) "0"
[11]=>
string(4) "1142"
[12]=>
string(5) "61219"
[13]=>
string(5) "21873"
[14]=>
string(1) "6"
[15]=>
string(1) "2"
[16]=>
string(2) "91"
[17]=>
string(1) "1"
[18]=>
string(4) "1.90"
[19]=>
string(4) "11.9"
[20]=>
string(8) "15:03:46"
}
[1]=>
array(21) {
[0]=>
string(1) "3"
[1]=>
string(1) "1"
[2]=>
string(1) "0"
[3]=>
string(8) "49986779"
[4]=>
string(6) "291340"
[5]=>
string(2) "11"
[6]=>
string(3) "667"
[7]=>
string(1) "0"
[8]=>
string(1) "0"
[9]=>
string(1) "0"
[10]=>
string(1) "0"
[11]=>
string(4) "1286"
[12]=>
string(5) "34464"
[13]=>
string(5) "21778"
[14]=>
string(1) "4"
[15]=>
string(1) "2"
[16]=>
string(2) "93"
[17]=>
string(1) "1"
[18]=>
string(4) "1.28"
[19]=>
string(3) "8.0"
[20]=>
string(8) "15:04:16"
}
[2]=>
array(21) {
[0]=>
string(1) "6"
[1]=>
string(1) "1"
[2]=>
string(1) "0"
[3]=>
string(8) "49986826"
[4]=>
string(6) "292415"
[5]=>
string(2) "10"
[6]=>
string(3) "622"
[7]=>
string(1) "0"
[8]=>
string(1) "0"
[9]=>
string(1) "0"
[10]=>
string(1) "0"
[11]=>
string(4) "1226"
[12]=>
string(5) "31334"
[13]=>
string(5) "20273"
[14]=>
string(1) "4"
[15]=>
string(1) "1"
[16]=>
string(2) "94"
[17]=>
string(1) "1"
[18]=>
string(4) "1.21"
[19]=>
string(3) "7.5"
[20]=>
string(8) "15:04:46"
}
}
I need to append today's date to each of the 20th elements in each array (need to make it date and time)
$now = new DateTime(null, new DateTimeZone('America/New_York'));
$today=$now->format('Y-m-d');
I tried this:
foreach ($lines as $key => $value){
$lines[$key][20]=$today . ' ' . $lines[$key][20];
echo $lines[$key][20];
}
looks like it worked.
2 . I need to insert this into an oracle db. I was curious to see if there is already a method or function to insert each array within lines array into the table.
In order to serialize an array for insertion into a database, one would either use json_encode(), or serialize(); I personally like json_encode(), as it is easier to read and modify.
Both of these functions convert an array or the like into an encoded string, which you can then either insert into a column for text-based data.
To do the actual action of insertion.. that actually depends upon the version of the db you are using, because the following code only works for 11g, 10g, 9i and 8i.
Note the following isn't exactly real code, and you will have to adapt it to suit your needs, following the documentation.
<?php
$str = json_encode($lines); //convert to string. You should have properly escaped the data prior to this/ran it through some function to escape the data. We don't want injection attacks to occur
$conn = oci_connect('username', 'password', 'localhost'); //connect to database
$stmt = oci_parse($conn, "insert into tablename ('column')
values('$str')"); // construct statement
oci_execute($stmt, OCI_DEFAULT); // statement executed/data inserted
?>

Categories