组合回声将字符串作为有效的html回显

时间:2016-10-08 01:55:28

标签: php 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);
}
?>

所以我有这个代码从谷歌工作表文件中获取每个单元格,但它一次回应一个。我需要将它们连接成一个字符串并将该字符串传递给js脚本,所以我需要立即回显它们。我不太懂php,所以我很难解读回声线。

1 个答案:

答案 0 :(得分:1)

您可以定义变量并为其附加字符串:

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