how to read with php from .csv file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chriscross
    New Member
    • Oct 2006
    • 4

    how to read with php from .csv file?

    I am trying to make three php pages:
    one showing the list of categories
    another showing a lost of the products of the category selected in the first page
    and the second showing the product selected in the second page

    I am using a .csv for a database, with the coloumns:
    Category, Products Information, Product Description, photo_file

    (the photo_file is the name of the file of the image which i need to view in the all pages for all products)

    what script can I use to do this?

    Please help for I am in a hurry,
    Thank-you all
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Look at the tutorial Handling data with comma delimited strings
    or the php class CSV Handler

    Ronald :cool:

    Comment

    • chriscross
      New Member
      • Oct 2006
      • 4

      #3
      Thanks but how can I make every line of data with a link to a template showing the data of the item clicked on from the same csv file?

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        You can use a [HTML]<a href="photo file name.typ">Click here to see</a>[/HTML]
        html tag and show that at the position on the screen (table cell or otherwise) where you want the user to click. If that is not clear, show the code you currently use/have developed that displays the file contents on the screen. That way we can all have a look at it and arrive at a solution.

        Ronald :cool:

        Comment

        • chriscross
          New Member
          • Oct 2006
          • 4

          #5
          Originally posted by ronverdonk
          You can use a [HTML]<a href="photo file name.typ">Click here to see</a>[/HTML]
          html tag and show that at the position on the screen (table cell or otherwise) where you want the user to click. If that is not clear, show the code you currently use/have developed that displays the file contents on the screen. That way we can all have a look at it and arrive at a solution.

          Ronald :cool:
          [code=php]
          the code i used is:
          <?php
          # This program is free software; you can redistribute it and/or modify
          # it under the terms of the GNU Library General Public License as published by
          # the Free Software Foundation either version 2 of the License, or

          //=============== =============== =============== =============== =============== =============== =============== ============
          //classdef CSVHandler 0.91 :: CSV Handling Wrapper
          //=============== =============== =============== =============== =============== =============== =============== ============
          #
          #
          # Copyright (C) 2005 by Andreas Müller

          class CSVHandler {
          var $Separator; //
          var $DataFile;
          var $DataKey;
          var $HeaderData; //
          var $ItemsList; //
          var $Items_Count;
          var $color;

          // Standard User functions
          function CSVHandler($Fil ename, $Separator, $KeyFieldName) { //Constructor
          $this->DataFile=$File name;
          $this->DataKey=$KeyFi eldName;
          $this->Separator=$Sep arator;
          $this->color="#FFFFFF ";
          }
          function ReadCSV() { //read data into this->ItemsList and return it in an array
          $this->Items_Count= 0;
          $this->ItemsList=arra y();
          $Item=array();
          $fp = fopen ($this->DataFile,"r" );
          $this->HeaderData = fgetcsv ($fp, 3000, $this->Separator);
          while ($DataLine = fgetcsv ($fp, 3000, $this->Separator)) {
          for($i=0;$i<cou nt($this->HeaderData);$i ++){
          $Item[$this->HeaderData[$i]]=$DataLine[$i];
          }
          array_push($thi s->ItemsList,$Ite m);
          $this->Items_Count+ +;
          }
          fclose($fp);
          return ($this->ItemsList);
          }
          function Select($needle, $column="all") { //get items in a sort of SQL Select query and return them in an array
          $this->ReadCSV();
          if($needle=="*" ) {
          $result=$this->ItemsList;
          } else {
          $result=array() ;
          if($column=="al l") {
          while(list($key ,$line)=each($t his->ItemsList)) {
          if (stristr(implod e("",$line),$ne edle)) array_push($res ult,$line);
          }
          } else {
          while(list($key ,$line)=each($t his->ItemsList)) {
          if (stristr($line[$column],$needle)) array_push($res ult,$line);
          }
          }
          }
          return ($result);
          }
          function ListAll() { //prints a list of all Data
          $Data=$this->ReadCSV();
          reset ($this->ItemsList);
          reset ($this->HeaderData);
          $HHeaders="";
          $HData="";
          while(list($HKe y,$HVal)=each($ this->HeaderData)) { //Create Headers Line
          $HHeaders.=$thi s->HTTD($HVal);
          }
          $HHeaders=$this->HTTR($HHeaders );
          while(list($Lin eKey,$LineVal)= each($this->ItemsList)) { //Read Data Lines
          $HDataLine="";
          while(list($Dat aKey,$DataVal)= each($LineVal)) { //Dissect one Data Line
          $HDataLine.=$th is->HTTD($DataVal) ;
          }
          $HData.=$this->HTTR($HDataLin e); //and add HTML to Data
          }
          print ($this->HTPage($this->HTTable($HHead ers.$HData)));
          }
          function GetValues($fiel d) { //Fetch all values of a specified field and return values in array
          $Data=$this->ReadCSV();
          $values=array() ;
          while(list($key ,$val)=each($th is->ItemsList)) {
          if(!in_array($v al[$field],$values)) array_push($val ues,$val[$field]);
          }
          sort($values);
          return $values;
          }
          function Edit() { //All edit function in one Table
          $Data=$this->ReadCSV();
          if(isset($_POST['commit'])) {
          $this->Update($_POS T[$this->DataKey],$_POST);
          $Data=$this->ReadCSV();
          }
          if(isset($_POST['add'])) {
          $this->Add($_POST[$this->DataKey],$_POST);
          $Data=$this->ReadCSV();
          }
          if(isset($_POST['delete'])) {
          $this->Delete($_POS T[$this->DataKey]);
          $Data=$this->ReadCSV();
          }
          $PAGE=$this->EditList();
          print $PAGE;
          }

          // Administration Area
          function Update($key,$da ta) { //Updating Item "key" with "data" named array
          $this->ReadCSV();
          for($i=0;$i<cou nt($this->ItemsList);$i+ +) {
          If($this->ItemsList[$i][$this->DataKey]==$key){
          while(list($key ,$val)=each($th is->HeaderData)) {
          if(isset($data[$val])) $this->ItemsList[$i][$val]=$data[$val];
          }
          }
          }
          $this->WriteData();
          return($this->ItemsList);
          }
          function Add($key,$data) { //add an Item "key" with "data" named array
          $this->ReadCSV();
          $NewLine=array( );
          $NewItem=array( $this->DataKey=>$key) ;
          while(list($key ,$val)=each($th is->HeaderData)) {
          if(isset($data[$val])) {
          $NewItem[$val]=$data[$val];
          } else {
          $NewItem[$val]=$data[$val]="";
          }
          }
          array_push($thi s->ItemsList,$New Item);
          $this->WriteData();
          return($this->ItemsList);
          }
          function EditList() { //returns Editor's List of Items
          reset ($this->ItemsList);
          reset ($this->HeaderData);
          $HHeaders=$this->HTTD(" ");
          $HData="";
          while(list($HKe y,$HVal)=each($ this->HeaderData)) { //Create Headers Line
          $HHeaders.=$thi s->HTTD($HVal);
          }
          $HHeaders=$this->HTTR($HHeaders );
          while(list($Lin eKey,$LineVal)= each($this->ItemsList)) { //Read Data Lines
          $HDataLine="";
          while(list($Dat aKey,$DataVal)= each($LineVal)) { //Dissect one Data Line
          $HDataLine.=$th is->HTInput($DataK ey,$DataVal);
          }
          $HData.=$this->HTForm($LineVa l[$this->DataKey],$this->HTTR($this->HTButton("comm it").$HDataLine .$this->HTButton("dele te"))); //and add HTML to Data
          }
          $HDataLine="";
          reset($this->HeaderData);
          while(list($Dat aKey,$DataVal)= each($this->HeaderData)) { // Add an extra Line for Adding a record
          $HDataLine.=$th is->HTInput($DataV al,"");
          }
          $HData.=$this->HTForm($LineVa l[$this->DataKey],$this->HTTR($this->HTButton("add" ).$HDataLine)); //and add HTML to Data
          return($this->HTPage($this->HTTable($HHead ers.$HData)));
          }
          function Delete($DeleteK ey) { //Remove Item "Key" from the file
          $inter=array();
          while(list($key ,$val)=each($th is->ItemsList)) {
          If($val[$this->DataKey]!=$DeleteKey) array_push($int er,$val);
          }
          $this->ItemsList=$int er;
          $this->WriteData();
          return($this->ItemsList);
          }
          function WriteData() { //writing contents of ItemList to Datafile
          reset($this->ItemsList);
          $Output=implode ($this->Separator, $this->HeaderData)."\ n";
          while(list($key ,$val)=each($th is->ItemsList)) {
          for($i=0;$i<cou nt($this->HeaderData);$i ++){
          $writekey=$this->HeaderData[$i];
          $writeitem[$writekey]=$val[$writekey];
          }
          $Output.=implod e($this->Separator, $writeitem)."\n ";
          }
          $fp = fopen ($this->DataFile,"w" );
          flock($fp,2);
          fputs($fp,$Outp ut);
          flock($fp,3);
          fclose($fp);
          }

          // Accessory HTML output functions
          function HTPage($value) { // Places $value into BODY of HTML Page
          $result = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
          $result.="<html ><head><title>" .$this->DataFile." Editor</title>\n";
          $result.="<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n";
          $result.="<styl e type=\"text/css\">";
          $result.="<!-- td { margin: 0px; padding: 0px; border: 1px solid #003399; } --></style></head>\n";
          $result.="<body >\n".$value." </body>\n</html>";
          return $result;
          }
          function HTForm($item,$d ata) { //places $data into form $item
          return "<form name=\"".$item. "\" method=\"post\" >\n".$data."</form>\n";
          }
          function HTTable($value) { //places $value into TABLE
          return "<table width=\"96%\" border=\"0\" cellspacing=\"0 \" cellpadding=\"0 \">\n".$value." </table>\n";
          }
          function HTTR($value) { //places $value into TR
          $this->SRotate();
          return "<tr>\n".$value ."</tr>\n";
          }
          function HTTD($value) { // places $value into TD
          return "<td bgcolor=\"".$th is->color."\">".$v alue."</td>\n";
          }
          function HTInput($field, $value) { //returns TD input field
          $Olen=strlen($v alue);
          if($Olen<3) {
          $Ilen=12;
          } else {
          $Ilen=$Olen;
          }
          return "<td bgcolor=\"".$th is->color."\"><inp ut name=\"".$field ."\" type=\"text\" id=\"".$field." \" value=\"".$valu e."\" size=\"".$Ilen. "\"></td>\n";
          }
          function HTButton($value ) { // returns "$value" button
          return "<td><input name=\"".$value ."\" type=\"submit\" id=\"".$value." \" value=\"".$valu e."\"></td>\n";
          }
          function SRotate() { //rotating colors for more readability of tables
          if($this->color=="#FFFFF F") {
          $this->color="#CCEEFF ";
          } else {
          $this->color="#FFFFFF ";
          }
          }
          }
          ?>[/code]
          Last edited by pbmods; Jul 7 '08, 01:39 AM. Reason: Added CODE tags.

          Comment

          • chriscross
            New Member
            • Oct 2006
            • 4

            #6
            also what code do i have to do to make a picture at the side of every item which is also viewed in the template when clicked?
            Thanks

            Comment

            • dragonsire08
              New Member
              • Jul 2008
              • 1

              #7
              Pardon my ignorance.....I 'm an old HTML with a little ASP user and I haven't quite gotten my head around php yet. Where in this script are you locating/calling the database (CSV) file??

              Thanks for any help you can offer.

              Bob A.
              Last edited by pbmods; Jul 7 '08, 01:40 AM. Reason: Removed excessive quoting.

              Comment

              Working...