combine echoes to echo a string as valid html - php

<?php
// link to Google Docs spreadsheet
$url='https://docs.google.com/spreadsheets/d/1_4d-MPxTUOVZbxTOlhjXv1ebTlBPQ-_JrYUlS1rqX5Q/pub?output=csv';
// open file for reading
if (($handle = fopen($url, "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$totalrows = count($data);
for ($row=0; $row<=$totalrows; $row++)
{
if ( (strlen($data[$row])>0))
{
echo '<dt><span class="icon"></span>'.$data[$row].'</dt><dd>'.$data[$answer].'</dd>';
}
}
}
fclose($handle);
}
?>
So I have this code that takes each cell from a google sheets file but It echoes them one at a time. I need to concatenate them into one string and pass that string to a js script so I need to echo them all at once. I don't know much php so I'm having a hard time deciphering the echo line.

You can define a variable and append strings to it:
<?php
// link to Google Docs spreadsheet
$url='https://docs.google.com/spreadsheets/d/1_4d-MPxTUOVZbxTOlhjXv1ebTlBPQ-_JrYUlS1rqX5Q/pub?output=csv';
// Define variable which will hold your data.
$html = "";
// open file for reading
if (($handle = fopen($url, "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$totalrows = count($data);
for ($row=0; $row<=$totalrows; $row++)
{
if ( (strlen($data[$row])>0))
{
// Append your data to $html variable
$html .= '<dt><span class="icon"></span>'.$data[$row].'</dt><dd>'.$data[$answer].'</dd>';
}
}
}
fclose($handle);
}
//Do whatever you want with $html variable.
?>

Related

PHP: Remove a row from a CSV if ID occurs multiple times and a column has a specific value

I have data as below in a CSV file. The green lines are what I'd like to keep.
Basically if someone has a single line, I want to keep it.
If they have multiple lines, I want to remove any where the 3rd column is A - Fully Fit.
After running through the whole file, I then want to save it over the original.
I tried writing the code but not sure if it's a Friday but the logic is escaping me at the moment.
There could be just a single line or there could be dozens. So if an ID has 1000 rows, I would just want to delete the rows for that ID where the 4th column is "A - Fully Fit"
UPDATE
This code works but not sure if it's the most optimal
// DECLARE ARRAYS
$a = Array();
$b = Array();
$c = Array();
// LOOP THROUGH FILE AND COLLECT DUPLICATES
if (($handle = fopen($filename, "r")) !== FALSE) {
while (($line = fgetcsv($handle, 4096, ",")) !== FALSE) {
$a[$line[1]][] = 'SHAKKA';
}
foreach ($a as $k=>$v) {
if (count($v)>1) {
$b[] = $k;
}
}
}
// IF A DUPLICATE THEN REMOVE ROWS THAT ARE 'A - Fully Fit'
if (($handle = fopen($filename, "r")) !== FALSE) {
while (($golly = fgetcsv($handle, 4096, ",")) !== FALSE) {
if (in_array($golly[1],$b) && $golly[3] == 'A - Fully Fit') {
}
else {
$c[] = $golly;
}
}
}
fclose($handle);
// WRITE THE FILE
$fp = fopen($filename, 'wa+');
foreach ($c as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
<?php
if (($handle = fopen($filename, "r")) !== FALSE) {
$new_rows = [];
$hash_ids = [];
while (($line = fgetcsv($handle, 4096, ",")) !== FALSE) {
if($line[3] === 'A - Fully Fit'){
if(isset($hash_ids[$line[1]])) continue;
$hash_ids[$line[1]] = true;
}
$new_rows[] = $line;
}
fclose($handle);
// WRITE TO THE FILE
$fp = fopen($filename, 'wa+');
foreach ($new_rows as $current_row) {
fputcsv($fp,$current_row);
}
fclose($fp);
}
In the above code, we maintain a hash(associative array) for each ID. If we have already come across with an ID with A - Fully Fit, then we skip the row, else we add it to the list.

How to do category mapping from csv?

I have a csv form which I have to make the category mapping. I had let this csv read from controller.And, I am trying to establish a hierarchy .So, that I can map . But , I am not able to do that. Please help!
[link:https://pricefalls.zendesk.com/hc/en-us/articles/204287645-How-do-I-create-a-category-map- “Pricefalls Category Taxonomy”].My csv has the same pattern as shown in the given link.
public function actionReadcsv()
{
$row = 1;
$csvFile = Yii::getAlias('#frontend') . '/modules/pricefalls /csv/Pricefalls_Categories1.csv';
if (($handle = fopen($csvFile, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "|")) !== FALSE) {
$row++;
$create_category[] =$data;
}
Pricefallscategories::categorytree($create_category);
echo "<pre>";
print_r($create_category);
echo"</pre>";
fclose($handle);
}
}

PHP changing the index of multidimentional array to value of variable

Pretty new to PHP. I have a csv feed that i am trying to display every line of in the right place. The feed is loaded into a multidimentional array and I can echo out specific places like this.
echo $readcsv[0][2]
But I am trying to use the value of the variable $row in stead of the first number while doing a while loop.
Something like:
echo $readcsv[$row][2]
I have tried next(), str_replace() and strtr() but none of them seems to work while the loop is running.
$row = 1;
$readcsv = array();
if (($handle = fopen("file.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
echo $readcsv[$row][2];
$row++;
for ($c=0; $c < $num; $c++) {
}
$mycsvfile[] = $data;
}
fclose($handle);
}
CSV file:
title;image_link;link;empty;price;
1;back.gif;http://link.com;;19.95;
2;back.gif;http://link.com;;19.95;
3;back.gif;http://link.com;;19.95;
4;back.gif;http://link.com;;19.95;
5;back.gif;http://link.com;;19.95;
I am not quite sure from your code and question exactly what you are trying to do, but maybe its something like this
if (($handle = fopen("file.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
// you load the line into $data and its a one dimentional array
// occurance 2 is the `link` (http://link.com) if that what you want to echo
echo $data[2];
// as you have a loop, maybe you were trying to
// echo each fields from the csv so you can do that like this
foreach ($data as $idx => $value) {
echo "Column $idx = $value";
}
$mycsvfile[] = $data;
}
fclose($handle);
}

Display lines in reverse order fgetcsv

How do I display lines in reverse order?
CSV file:
column1;column2;column3
cell1;cell1;cell1
cell2;cell2;cell2
cell3;cell3;cell3
It should appear like this:
column1;column2;column3
cell3;cell3;cell3
cell2;cell2;cell2
cell1;cell1;cell1
Code:
if (($handle = fopen($path, 'r')) !== FALSE)
{
echo '<table class="table table-striped table-bordered"><thead>';
// Get headers
if (($data = fgetcsv($handle, 1000, ';')) !== FALSE)
{
echo '<tr><th>'.implode('</th><th>', $data).'</th></tr>';
}
echo '</thead><tbody>';
// Get the rest
while (($data = fgetcsv($handle, 1000, ';')) !== FALSE)
{
echo '<tr><td>'.implode('</td><td>', $data).'</td></tr>';
}
fclose($handle);
echo '</tbody></table>';
}
Thanks in advance.
Collect first
$collect = array();
while (($data = fgetcsv($handle, 1000, ';')) !== FALSE)
{
$collect[]= '<tr><td>'.implode('</td><td>', $data).'</td></tr>';
}
echo implode(PHP_EOL,array_reverse($collect));
Reverse the array at the end.
Here what you need to do: instead of direct echoing row - store it to a variable:
// Get the rest
$rest = '';
while (($data = fgetcsv($handle, 1000, ';')) !== FALSE)
{
// main trick here - add every new row BEFORE old ones
$rest = '<tr><td>'.implode('</td><td>', $data).'</td></tr>' . $rest;
}
// echo gathered data
echo $rest;

get a column data into a text file one by one?

the csv data as the following:
(first line) price url
(second line) $10 src="http://www.test.com/1455/"||src="http://www.test.com/image/1456/"||
(third line) $20 src="http://www.test.com/2260/"||src="http://www.test.com/2261/"||
.... ........
now i want to put all the data from the url column into a text file. and shows the data one by one. namely:
http://www.test.com/1455/
http://www.test.com/image/1456/
http://www.test.com/2260/
http://www.test.com/2261/
....
now,i am stucked.
a, i don't know how to prevent outputting the first line.
b,i don't know how to get the url, and put them i a text file one by one.
first i using the following code to output the data to the screen.
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
echo $data[1] . "<br />\n";
}
fclose($handle);
}
the above code also output the first line (url). i don't want to output it. how should i do.
thank you.
You're very close! I have modified your code just slightly to do what you're trying to do. Essentially you need a second file handler for the output. You can open that as append mode so that write append to the end of the file. As for not echoing the header, you just make sure that row is greater than the first row. Hope it helps!
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE && ($handle2 = fopen("output.txt", 'a')) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($row > 1) {
fwrite($handle2, "{$data[1]}\n");
}
$row++;
}
fclose($handle);
fclose($handle2);
}
// Skips the first line of the CSV - outputting the second "column" of data.
$row = 0;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
if ($row > 0 ) {
echo $data[1] . "<br />\n";
}
$row++;
}
fclose($handle);
}

Categories