I am having troubles converting a string which contains tab delimited CSV output
into an array. While using the following code :
$data = stream_get_contents($request->getShipmentReport());
I get back data as following:
Date Shipped Comments Feedback Arrived on Time
5/11/15 2 comment response Yes
Now I would like to process this data into an array with the returned headers(line 1) as the index containing the value for each line which follows after that.
I am trying to end up with something like this :
$line['Date'] = 5/11/15
$line['Shipped'] = 2
$line['Feedback'] = response
$line['Arrived on Time'] yes
What would be the best way to achieve this ?
I recommend using a library for handling CSV for example the CSV-Package by The League of Extraordinary Packages
You can pass your data and start working with array structures right away:
$csvString = stream_get_contents($request->getShipmentReport());
$csvReader = \League\Csv\Reader::createFromString($csvString);
$data = $csvReader->fetchAssoc(
array('Date', 'Shipped', 'Comments', 'Feedback', 'Arrived on Time')
);
// Or since line 0 contains header
$data = $csvReader->fetchAssoc(0); // 0 is offset of dataset containing keys
Now your data should be in a structure like:
$data = array(
array(
'Date' => '5/11/15',
'Shipped' => '2',
...
),
array(
...
)
);
You can even filter out specific datasets and all kinds of fancy stuff. Just check the documentation I linked to.
League CSV reader gives me a bad result. I use this instead:
$csvReportString = stream_get_contents($request->getReport());
$csvReportRows = explode("\n", $csvReportString);
$report = [];
foreach ($csvReportRows as $c) {
var_dump($c);
if ($c) { $report[] = str_getcsv($c, "\t");}
}
var_dump($report);
Related
how can optimization function this code in PHP smarty?
now I've a code to confuse me, there is a simple code.
$sql_set_land = "select * from set_new_land where id_rent_house =".Tools::getValue("id_rent_house");
//print_r($sql_set_land);
$n_land = Db::rowSQL($sql_set_land,true);
$landTitle1 = $n_land['landTitle1'];
$landTitle2 = $n_land['landTitle2'];
$landBuilderNumber = $n_land['landBuilderNumber'];
$landLandMark = $n_land['landLandMark'];
$land_1 = $n_land['land_1'];
$land_2 = $n_land['land_2'];
$land_3 = $n_land['land_3'];
...
...
...
$land_30 = $n_land['land_30'];
$land_31 = $n_land['land_31'];
$land_32 = $n_land['land_32'];
when I search the code that I need to push the value.
$this->context->smarty->assign([
'park_space' =>$park_space,
'recording_data' =>$recording_data,
'clode_number' => $clode_number,
'ad_choose_top' => $ad_choose_top,
'ad_choose_type' => $ad_choose_type,
'ad_choose_payment_type' => $ad_choose_payment_type,
'ad_choose_payment_type1' => $ad_choose_payment_type1,
'landTitle1' => $landTitle1,
'landTitle2' => $landTitle2,
'landBuilderNumber' => $landBuilderNumber,
'landLandMark' => $landLandMark,
'land_1' => $land_1,
'land_2' => $land_2,
'land_3' => $land_3,
...
...
...
'land_30' => $land_30,
'land_31' => $land_31,
'land_32' => $land_32,
how can optimization function? Can I write to array? if It's can write into array, how can I do?
Your problem is not in the PHP code, it is in the design of your database. Any time you need to have numbers in column names is a sign that you've failed to normalize your data.
If you expanded the *, which is generally good practice to avoid surprises when you make changes to your database, you would have to write this:
select
landTitle1,
landTitle2,
landBuilderNumber,
landLandMark,
land_1,
land_2,
land_3,
land_4,
land_5,
land_6,
land_7,
land_8,
land_9,
land_10,
land_11,
land_12,
land_13,
land_14,
land_15,
land_16,
land_17,
land_18,
land_19,
land_20,
land_21,
land_22,
land_23,
land_24,
land_25,
land_26,
land_27,
land_28,
land_29,
land_30,
land_31,
land_32
from set_new_land
where id_rent_house = :id
With a properly normalised database, you would instead write something like this:
select
SND.landTitle1,
SND.landTitle2,
SND.landBuilderNumber,
SND.landLandMark,
L.landNumber,
L.land
from set_new_land as SND
join lands as L
On L.set_new_land_id = SND.set_new_land_id
where SND.id_rent_house = :id
Then in PHP, you can use the array_column function:
$lands = array_column($dbResults, 'land');
// or, if the land numbers are important
$lands = array_column($dbResults, 'land', 'landNumber');
If you can't fix your data, though, you can transform it into a more sensible form in PHP, with a loop that counts from 1 to 32:
$lands = [];
for ( $landNumber=1; $landNumber<=32; $landNumber++ ) {
$columnName = 'land_' . $landNumber;
$lands[] = $messyDbResults[ $columnName ];
}
Hey I am building a chatbot using dialogflow and I am generating the responses by using a customized Webhook (I am programming in php). I am extracting data from my database and storing it in an array but when I send the array as a response to dialogflow it only shows the first row.
Here is my code:
<?php
header('Content-Type: text/html; charset=utf-8');
date_default_timezone_set("Asia/Bangkok");
$date = date("Y-m-d");
$time = date("H:i:s");
$json = file_get_contents('php://input');
$request = json_decode($json, true);
$input = fopen("log_json.txt", "w") or die("Unable to open file!");
fwrite($input,$json);
fclose($input);
function processMessage($update) {
if($update["queryResult"]["action"] == "ques"){
$bdd= new PDO('mysql:host=localhost;dbname=****', '****', '***', array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")) ;
$data = array();
$nom= $update["queryResult"]["parameters"]["nom_aliment"];
$info=$update["queryResult"]["parameters"]["Information"];
$quantite=$update["queryResult"]["parameters"]["Quantite"];
$req=$bdd->prepare("SELECT * FROM TableCiqual WHERE alim_nom LIKE ? ");
$req->execute(array("%$nom%"));
while($resultat=$req->fetch()){
$variab=$resultat[$info]*$quantite/100;
$ppp =$resultat['alim_nom'].' '.$info.' : '.$variab;
$data=$ppp;
}
sendMessage(array(
"source" => $update["responseId"],
"fulfillmentText"=>$data,
"payload" => array(
"items"=>[
array(
"simpleResponse"=>
array(
"textToSpeech"=>"Bad request"
)
)
],
),
));
}
}
function sendMessage($parameters) {
echo json_encode($parameters);
}
I know that my query returns multiple results all these results are stored in the array $data that I send as a response in dialogflow. The problem is that dialogflow only shows me the first row of the array $data instead of the whole array with all the rows.
My question is : Is it possible to send an array as a response in dialogflow and if yes how so.
I think there are two issues here.
The first is that $data is not actually containing a list of your results. The line
$data = $ppp;
is assigning $ppp, which is a string, to $data rather than adding on to the end of the array. I think, for that line, you want something more like
$data[] = $ppp;
However, this doesn't solve your problem completely, since the fulfillmentText attribute in JSON isn't expecting an array - it is expecting a string. So you probably want to concatenate all of those entries with something like
"filfillmentText" => implode( "\n", $data );
However, this assumes that you both want a new line in between each answer and that the chat system you're using supports the feature this way - not all do. (And you haven't indicated which one you're using.)
I'm trying to get PHP returned array from Ajax.
Here is my Ajax call:
And my PHP code is this:
Query is running perfectly.
I have tried to get values like alert(data[0].program_title) in ajax success. But it also returns Undefined.
How can I fix this issue?
In your PHP code:
//remove
return $data
//change with
echo json_encode($data);
Just before flushing the data back to the stream (returning), convert your data ($data variable in this case) to JSON string using json_encode and add the proper content-type application/json using header function.
However, the best practice is to provide some metadata to your data included in your transfer, like the size of data and count of elements in data and if paginated, which page the data refers to and what the maximum available pages and maximum element size of a page are.
Here's a sample body structure for a more robust data transfer:
$response = [
'page' => 0, // e.g.
'count' => count($data),
'data' => $data,
'max_page' => 3, // e.g.
'item_per_page' => 15, // e.g.
'status_code' => 200, // e.g.
];
header ( "Content-Type: application\/json", true , 200);
return json_encode(
$response
, JSON_INVALID_UTF8_SUBSTITUTE
| JSON_NUMERIC_CHECK
| JSON_PRESERVE_ZERO_FRACTION
| JSON_UNESCAPED_LINE_TERMINATORS
| JSON_UNESCAPED_SLASHES
| JSON_UNESCAPED_UNICODE
);
Try this:
$data = [];
if ($numRows>0) {
while($row=$result->fetch_assoc()) {
$data[] = $row;
}
}
Replace return with echo and add json_encode
echo json_encode($data);
I am collecting ADSB data on a Raspberry Pi via a JSON file; I am using someones PHP code that is producing a SQL data base from the JSON file.However I need a csv file to feed into an analsis program (SPSS) that I have developed.
Here is the PHP code that creates the sql database
// generate sql insert statement per aircraft in range of user set alt_geom/latitude/longitude and optionally according only to hex or flight numbers in hex_code_array.txt and flight_code_array.txt
#var_dump($hex_code_array); var_dump($flight_code_array); // show arrays for debug
if ($user_set_array['filter_mode_database'] && $user_set_array['filter_mode_database_limited']) {
if (($ac_alt_geom != '' && $ac_alt_geom < $user_set_array['max_alt'] && $ac_lat < $user_set_array['max_lat'] && $ac_lat > $user_set_array['min_lat'] && $ac_lon < $user_set_array['max_lon'] && $ac_lon > $user_set_array['min_lon']) && (func_wildcard_search($ac_hex, $hex_code_array, $user_set_array['filter_mode_wildcard']) || ($ac_flight != '' && func_wildcard_search($ac_flight, $flight_code_array, $user_set_array['filter_mode_wildcard'])))) {
$sql .= "INSERT INTO aircrafts VALUES (NULL, '" . date("Y-m-d G:i:s l", $ac_now) . "', '$ac_now', '$ac_hex', '$ac_flight', '$ac_dist', ";
$sql .= "'$ac_alt_geom', '$ac_lat', '$ac_lon', '$ac_track', '$ac_gs', '$ac_baro_rate', '$ac_seen_pos', '$ac_seen', ";
$sql .= "'$ac_rssi', '$ac_messages', '$ac_category', '$ac_squawk', '$ac_alt_baro', '$ac_mlat', '$ac_tisb', '$message_rate');";
$sql .= PHP_EOL;
As I am going up a steep learning curve with PHP I have adapted some code that I found else where on Stack but its not producing a csv file. My objective was to take the variables that create the sql arrays and continually update the csv file .
$data= "id,now,hex,flight,distance,altitude,lat,long,track,gs,baro-rate,seen-pos,seen,rasi,messages,category,squark,alt-baro,mlat,tisb,rec-mag-sec\n";
$i = 1;
#echo $data;
$data = array(
array( '$i' ),
array( '$ac_id' ),
array( '$ac_now' ),
array( '$ac_hex' ),
array( '$ac_flight' ),
array( '$ac_distance' ),
array( '$ac_alt_geom' ),
array( '$ac_lat' ),
array( '$ac_long' ),
array( '$ac_track' ),
array( '$ac_gs' ),
array( '$ac_baro_rate' ),
array( '$ac_seen_pos' ),
array( '$ac_seen' ),
array( '$ac_rssi' ),
array( '$ac_messages' ),
array( '$ac_category' ),
array( '$ac_squark' ),
array( '$ac_alt_baro' ),
array( '$ac_mlat' ),
array( '$ac_tisb' ),
array( '$ac_rec_mag_sec' ),
);
}
function outputCSV($data) {
$outputBuffer = fopen("php://output", 'w');
foreach($data as $val) {
fputcsv($outputBuffer, $val);
}
fclose($outputBuffer);
}
$filename = 'aircrafts';
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=($filename).csv");
header("Pragama: no-cache");
hwader("Expires: 0");
outputCSV($data);
First of all, it looks like you are mixing and matching 2 separate methods of working with an array. If you are working with data that starts off as comma separated string like this:
$data= "1,'2019-2-15 12:00:00','#f6F0C1',1040,345,12500,32.1465,80.1263, ...";
Then you can convert $data into an array like this:
$data = explode(",", $data);
Note that when doing this, you are not using variable names but actual raw data that you want in the csv.
However, if your data is coming from a bunch of variables, then you declare your array like this:
$data= array($i, $ac_id, $ac_now, $ac_hex, $ac_flight, $ac_distance, $ac_alt_geom, $ac_lat ...);
Of note, since each element of your $data array is only one value, you don't need to make arrays out of them. Also, because you want to call these values as variables, you don't want to put them in quotes. If you put them in quotes, they will be evaluated as strings instead of variables.
Once you have used the appropriate method above to make an array called $data, you need to push it to your CSV. Another thing to note here is that you are only trying to put one line into the CSV at a time; so, you do not need to loop it at all. Looping is used for when you have an array of arrays where each subarray represents a single line in the CSV file. Since you are just doing a single line at a time, you can simplify your code as shown below.
function outputCSV($data) {
$outputBuffer = fopen("php://output", 'w');
fputcsv($outputBuffer,$data);
fclose($outputBuffer);
}
I want to save xml document in mysql database as below:
$_docId= $this->getRequest()->getParam('docId', 0);
$_slmData = '<SLM_form_v2 id="slmform"><group_1_1><section1_1/><name1_1>sdfds</name1_1>
enter code here<localname1_2/><selectcountry_1_3>Algeria</selectcountry_1_3></group_1_1>
enter code here<group_1_2><main_doc/><name_doc1>gdgf gfh f</name_doc1><sex_doc1>Male
</sex_doc1><name_institution_1>fsdgdfg</name_institution_1><address_institution_1>gdgfdgfd
</address_institution_1>....';
$_docMapper = new Model_Mapper_XMLDoc();
$_docModel = new Model_XMLDoc();
$_docModel ->doc_data = Zend_Json::encode($_docData);
if ($_docId != 0) {
$_docModel->id = $_docId;
$_docMapper->update($_docModel->toArray(), 'id = ' . $_docId);
$_action = 'update';
} else {
$_docMapper->insert($_docModel->toArray());
$_lastId = $_docMapper->getAdapter()->lastInsertId(); //gets the id of the last inserted record
$_action = 'add';
}
$this->_helper->json->sendJson(array(
'message' => 'success',
'id' => $_lastId,
'action' => $_action
));
It is stored in the db:
INSERT INTO crpcoreix.tbl_xml_doc (slm_data, web_gis_fk) VALUES ('"<SLM_form_v2 id=\\"slmform\\"><group_1_1><section1_1\\/><name1_1>sdfds<\\/name1_1><localname1_2\\/><selectcountry_1_3>Algeria<\\/selectcountry_1_3><\\/group_1_1><group_1_2><main_doc\\/><name_doc1>gdgf gfh f<\\/name_doc1><sex_doc1>Male<\\/sex_doc1><name_institution_1>fsdgdfg<\\/name_institution_1><address_institution_1>gdgfdgfd gdgf<\\/address_institution_1>...', null);
When I'm trying to read the data from the database and display the encoded xml tags the output miss the first part ("<SLM_form_v2 id=\\"slmform\\"><group_1_1><section1_1\\/><name1_1>...) the first part
Array
(
[id] => 1
[xml_data] => "sdfds<\/name1_1>Algeria<\/selectcountry_1_3>...'
[web_gis_fk] =>
)
Please advice how to fix and if there is a better way to store xml documents in database.
Thanx,
first is first you need to double check your given xml syntax, whether its correct or not.
Zend_Json includes a static function called Zend_Json::fromXml(). so
you would better manage to use it instead of Zend_Json::encode when
you encode XML
This function will generate JSON from a given XML input.
This function takes any arbitrary XML string as an input parameter. It also takes an optional Boolean input parameter to instruct the conversion logic to ignore or not ignore the XML attributes during the conversion process.
If this optional input parameter is not given, then the default behavior is to ignore the XML attributes. This function call is made as shown below:
$jsonContents = Zend_Json::fromXml($xmlStringContents, true);
Give it a try but again check your XML syntax