2

我有一个公共的 google 电子表格CSV。我从远程 URL 获取数据作为数组。然后我将信息显示为 HTML 列表,引用适当的数组项。现在我想在它上面触发一个 php 选择查询,以便只显示特定演员的八卦。怎么做?(我想使用一个变量 $Name.wen 我提供了 Name。我应该得到那个 Actor 的八卦。)

我的 excel 表包含以下列

> 1.Sr No.
> 2.Name
> 3.Gossip

这是我的 php 页面的代码,它以列表的形式检索数据:

$lines = file('https://docs.google.com/spreadsheet/pub?key=0AgzUC4MxdChJdFIyMHFUZ21PS053b3Z1OHFnbHJwYVE&single=true&gid=0&output=csv');


$headers = array_shift($lines);


foreach ($lines as $line)
{

    $ldata =  explode(',', trim($line)); 

    if ($ldata[0] == '') break;
    echo '<li>Sr No.  <strong>' . $ldata[0] . '</strong></li><li>Name  <strong>' . $ldata[1] . '</strong></li><li>Gossip <strong>' . $ldata[2] . '</strong></li>';
}
4

2 回答 2

0

试一试:

<?php    
   // get the CSV data as an array from the remote URL
define('GOOGLE_DOC','https://docs.google.com/spreadsheet/pub?key=0AgzUC4MxdChJdFIyMHFUZ21PS053b3Z1OHFnbHJwYVE&single=true&gid=0&output=csv');

if(isset($_GET['filterColumn'])){define('FILTER_COLUMN',$_GET['filterColumn']);}
if(isset($_GET['filterValue'])){define('FILTER_VALUE',$_GET['filterValue']);}

function readCSVIntoArray($fileName)
{
    $rows=array();
    if (($handle = fopen($fileName, "r")) !== FALSE)
    {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE){$rows[]=$data;}
        fclose($handle);
    }
    $h=array_shift($rows);
    return array($h,$rows);
}



list($head,$rows)=readCSVIntoArray(GOOGLE_DOC);
header('Content-Type: text/html; charset=utf-8');
?><!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Ultimater's Example</title>
<style type="text/css">
html,body
{
    margin:0;
    padding:0;
    font-family:'Source Sans Pro',sans-serif;
    font-size: 13px;
    color:black;
    background:#e2e2ec;
    line-height:15px;
}
table{border-collapse:collapse;}
table thead tr th{font-weight:bold;padding:2px;margin:1px;border:1px solid black;background-color:blue;color:white;}
table tbody tr td{margin:1px;padding:2px;border:1px solid black;}
</style>
</head>
<body>
<div>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">
<select name="filterColumn">
<?php foreach($head as $i=>$h){echo sprintf('<option value="%s">%s</option>',$i,htmlentities($h));} ?>
</select>
<input type="text" name="filterValue" place-holder="filter" value="" />
<input type="submit" value="Filter" />
</form>
</div>
<hr />
<?php

echo '<table>';
echo '<thead><tr><th>'.implode('</th><th>',$head).'</th></tr></thead>';
echo '<tbody>';
foreach($rows as $row)
{
    if(defined('FILTER_COLUMN')&&defined('FILTER_VALUE'))
    {
        if(strpos($row[FILTER_COLUMN],FILTER_VALUE)===false)continue;
    }
echo '<tr><td>'.implode('</td><td>',$row).'</td></tr>';
}
echo '</tbody>';
echo '</table>';
?>
</body>
</html>
于 2013-04-09T11:12:15.963 回答
-1

如果名称不是必需的,则简单地不显示一行:

<?php    
   // get the CSV data as an array from the remote URL
   $lines = file('https://docs.google.com/spreadsheet/pub?key=0AgzUC4MxdChJdFIyMHFUZ21PS053b3Z1OHFnbHJwYVE&single=true&gid=0&output=csv');

   // get rid of header row    
   $headers = array_shift($lines);

   // Loop through data- therer is only one line hear
   foreach ($lines as $line) {    
       $ldata =  explode(',', trim($line)); // split row to its own array of elements
       if ($ldata[0] == '') break; // an empty line means we are done, so exit the foreach loop

       if($ldata[1] == $var_with_required_name) {

           // now we can just output the information as an HTML list, referencing the appropriate array items    
           echo '<li>Sr No.  <strong>' . $ldata[0] . '</strong></li><li>Name  <strong>' . $ldata[1] . '</strong></li><li>Gossip <strong>' . $ldata[2] . '</strong></li>';
       }
   }
?>
于 2013-04-09T10:26:06.127 回答