PHP search csv files

Hi,
I am using this PHP to search for CSV file. How do I add to script to generate another search form for another csv file.
http://www.scgstudio.com/ucsc/web/benefactor.csv
http://www.scgstudio.com/ucsc/web/Workbook3.csv

here is the actual site http://www.scgstudio.com/ucsc/web/

Here is the php code on the page

<?php include('csvtoservice.php'); $content = csvtoservice('http://www.scgstudio.com/ucsc/web/Workbook3.csv', array( 'filter' => array(fieldnames), 'rename'=> array( 'name'=>'Name', 'donortype'=>'Donor Type', 'year'=>'Year', 'amount'=>'Amount' ), 'uppercase'=>Boolean ) ); // if it could be loaded and parsed...
if($content){
// show the form
echo '<h2>Honor Roll of 2010-11 Contributors</h2>';
if($content['form']){
  echo $content['form'];
}

}
?>

<?php // if it could be loaded and parsed... if($content){
// show the table
if($content['table']){
  echo '<h2>Results</h2>';
  echo $content['table'];
}

}
?>

This is a csvtoservice.php to search csv and generating form fields base on CSV columns.

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 = ‘’;
foreach($columns as $k=>$c){
$csvform .= ‘

’.
($options[‘uppercase’] ?
ucfirst($displaycolumns[$k]) :
$displaycolumns[$k]).
‘’.
’;
}

$csvform .= ‘

<input type=“submit” name=“csvsend”’.
’ value=“search”>
’;

$csvform .= ‘’;

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 = ‘

’.$yql.’
’;
$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 .= ‘

No results found.’;
}
$csvtable .= ‘’;
}
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;
}

Sponsor our Newsletter | Privacy Policy | Terms of Service