0
我正在使用此PHP腳本來搜索Christian Heilmann的CSV文本文件。但是,目前,當你第一次訪問該網站時,沒有任何結果,直到你進行搜索。如何使其顯示標準數據列表直到用戶搜索基本數據。PHP腳本搜索CSV文本文件
function csvtoservice($url,$options){
$csv = get($url);
$lines = preg_split('/\r?\n/msi',$csv);
$columns = split(',',
strtoLower(
preg_replace('/\s/','',$lines[0])
)
);
$colstring = join(',',$columns);
if($options['preset']){
$pres = $options['preset'];
foreach(array_keys($pres) as $p){
$presetstring .= ' and '.$p.' like "%'.$pres[$p].'%"';
}
$columns = array_diff($columns,array_keys($pres));
}
if($options['filter']){
$columns = array_diff($columns,$options['filter']);
}
if($options['prefill']){
foreach(array_keys($options['prefill']) as $p){
if(!isset($_GET[$p])){
$_GET[$p] = $options['prefill'][$p];
}
}
}
if($options['rename']){
$renames = array_keys($options['rename']);
foreach($columns as $k=>$c){
foreach($renames as $r){
if(!in_array($c,$renames)){
$displaycolumns[$k] = $c;
} else {
if($c == $r){
$displaycolumns[$k] = $options['rename'][$r];
}
}
}
}
} else {
$displaycolumns = $columns;
}
foreach($columns as $c){
filter_input(INPUT_GET, $c, FILTER_SANITIZE_SPECIAL_CHARS);
$fromget[$c] = $_GET[$c];
}
$current = preg_replace('/.*\/+/','',"#section4",$_SERVER['PHP_SELF']);
$csvform = '<form action="'.$current.'">';
foreach($columns as $k=>$c){
$csvform .= '<div><label for="'.$c.'">'.
($options['uppercase'] ?
ucfirst($displaycolumns[$k]) :
$displaycolumns[$k]).
'</label></br>'.
'<input type="text" id="'.$c.'" name="'.$c.
'" value="'.$fromget[$c].'"></div>';
}
$csvform .= '<div id="bar"><input type="submit" name="csvsend"'.
' value="search"></div>';
$csvform .= '</form>';
if(isset($_GET['csvsend'])){
$yql = 'select * from csv where url="'.$url.'" '.
'and columns="'.$colstring.'"';
foreach($columns as $c){
if(isset($_GET[$c]) && $_GET[$c]!=''){
$yql .= ' and '.$c.' like "%'.$_GET[$c].'%"';
}
}
$yql .= $presetstring;
$yqlquery = '<div id="yql">'.$yql.'</div>';
$yqlendpoint = 'http://query.yahooapis.com/v1/public/yql?format=json';
$query = $yqlendpoint.'&q='.urlencode($yql);
$data = get($query);
$datadecoded = json_decode($data);
$csvtable = '<table><thead><tr>';
foreach($columns as $k=>$c){
$csvtable .= '<th scope="col">'.
($options['uppercase'] ?
ucfirst($displaycolumns[$k]) :
$displaycolumns[$k]).
'</th>';
}
$csvtable .= '</tr></thead><tbody>';
if($datadecoded->query->results->row){
foreach ($datadecoded->query->results->row as $r){
$csvtable .= '<tr>';
foreach($columns as $c){
$csvtable .= '<td>'.$r->$c.'</td>';
}
$csvtable .= '</tr>';
}
} else {
$csvtable .= '<tr><td class="error" colspan="'.sizeof($columns).
'">No results found.</td></tr>';
}
$csvtable .= '</tbody></table>';
}
return array(
'table'=>$csvtable,
'form'=>$csvform,
'query'=>$yqlquery,
'json'=>$data
);
}
function get($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
這應該是一個評論,而不是一個答案 – Hamish