对CSV数据进行Fire查询

时间:2013-04-09 10:17:56

标签: php

我有一个公开的Google电子表格。我从远程网址获取了CSV数据作为数组。然后我将信息显示为HTML列表,引用相应的数组项。 现在我想在它上面激活一个php select查询,这样只显示特定actor的八卦。怎么做? (我想使用变量$ Name.wen我提供Name.I应该得到那个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>';
}

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:-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>';
       }
   }
?>