MickDom MickDom - 2 months ago 6
HTML Question

combine echoes to echo a string as valid html

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

Answer

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