English 中文(简体)
PHP 书本搜索
原标题:PHP script searches CSV text file
  • 时间:2011-11-23 23:25:42
  •  标签:
  • php
  • csv

我正在使用这份PHP书,搜寻基督教海尔曼的CSV文本。 但是,在你进行搜查之前,你先到现场就没有结果。 我如何显示标准数据清单,让用户查询基本数据。

function csvtoservice($url,$options){
  $csv = get($url);
  $lines = preg_split( /
?
/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;
}
问题回答

why not to set a default value if there is no search term passed by the form?





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签