Connecting Tech Pros Worldwide Forums | Help | Site Map

how to read with php from .csv file?

Newbie
 
Join Date: Oct 2006
Posts: 4
#1: Oct 8 '06
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's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#2: Oct 9 '06

re: how to read with php from .csv file?


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

Ronald :cool:
Newbie
 
Join Date: Oct 2006
Posts: 4
#3: Oct 9 '06

re: how to read with php from .csv file?


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?
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#4: Oct 9 '06

re: how to read with php from .csv file?


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:
Newbie
 
Join Date: Oct 2006
Posts: 4
#5: Oct 9 '06

re: how to read with php from .csv file?


Quote:

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:

Expand|Select|Wrap|Line Numbers
  1. the code i used is:
  2. <?php
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU Library General Public License as published by
  5. # the Free Software Foundation either version 2 of the License, or
  6.  
  7. //=====================================================================================================================
  8. //classdef  CSVHandler 0.91 :: CSV Handling Wrapper
  9. //=====================================================================================================================
  10. #
  11. #
  12. # Copyright (C) 2005 by Andreas Müller
  13.  
  14. class CSVHandler {
  15.     var $Separator;        //
  16.     var $DataFile;
  17.     var $DataKey;
  18.     var $HeaderData;    //
  19.     var $ItemsList;    //
  20.     var $Items_Count;
  21.     var $color;
  22.  
  23. // Standard User functions
  24.     function CSVHandler($Filename, $Separator, $KeyFieldName) {        //Constructor
  25.         $this->DataFile=$Filename;
  26.         $this->DataKey=$KeyFieldName;
  27.         $this->Separator=$Separator;
  28.         $this->color="#FFFFFF";
  29.     }
  30.     function ReadCSV() {            //read data into this->ItemsList and return it in an array 
  31.         $this->Items_Count=0;
  32.         $this->ItemsList=array();
  33.         $Item=array();
  34.         $fp = fopen ($this->DataFile,"r");
  35.         $this->HeaderData = fgetcsv ($fp, 3000, $this->Separator);
  36.         while ($DataLine = fgetcsv ($fp, 3000, $this->Separator)) {
  37.             for($i=0;$i<count($this->HeaderData);$i++){
  38.                 $Item[$this->HeaderData[$i]]=$DataLine[$i];
  39.             }
  40.             array_push($this->ItemsList,$Item);
  41.             $this->Items_Count++;
  42.         }
  43.         fclose($fp);
  44.         return ($this->ItemsList);
  45.     }
  46.     function Select($needle,$column="all")    {            //get items in a sort of SQL Select query and return them in an array
  47.         $this->ReadCSV();
  48.         if($needle=="*") {
  49.             $result=$this->ItemsList;
  50.         } else {
  51.             $result=array();
  52.             if($column=="all") {
  53.                 while(list($key,$line)=each($this->ItemsList)) {
  54.                      if (stristr(implode("",$line),$needle)) array_push($result,$line);
  55.                 }
  56.             } else {
  57.                 while(list($key,$line)=each($this->ItemsList)) {
  58.                      if (stristr($line[$column],$needle)) array_push($result,$line);
  59.                 }
  60.             }
  61.         }
  62.         return ($result);
  63.     }
  64.     function ListAll() {                    //prints a list of all Data
  65.         $Data=$this->ReadCSV();
  66.         reset ($this->ItemsList);
  67.         reset ($this->HeaderData);
  68.         $HHeaders="";
  69.         $HData="";
  70.         while(list($HKey,$HVal)=each($this->HeaderData)) {            //Create Headers Line
  71.             $HHeaders.=$this->HTTD($HVal);
  72.         }
  73.         $HHeaders=$this->HTTR($HHeaders);
  74.         while(list($LineKey,$LineVal)=each($this->ItemsList)) {    //Read Data Lines
  75.             $HDataLine="";
  76.             while(list($DataKey,$DataVal)=each($LineVal)) {            //Dissect one Data Line
  77.                 $HDataLine.=$this->HTTD($DataVal);
  78.             }
  79.             $HData.=$this->HTTR($HDataLine);    //and add HTML to Data
  80.         }
  81.         print ($this->HTPage($this->HTTable($HHeaders.$HData)));
  82.     }
  83.     function GetValues($field) {        //Fetch all values of a specified field and return values in array
  84.         $Data=$this->ReadCSV();
  85.         $values=array();
  86.         while(list($key,$val)=each($this->ItemsList)) {
  87.             if(!in_array($val[$field],$values)) array_push($values,$val[$field]);
  88.         }
  89.         sort($values);
  90.         return $values;
  91.     }
  92.     function Edit() {                        //All edit function in one Table
  93.         $Data=$this->ReadCSV();
  94.         if(isset($_POST['commit'])) {
  95.             $this->Update($_POST[$this->DataKey],$_POST);
  96.             $Data=$this->ReadCSV();
  97.         }    
  98.         if(isset($_POST['add'])) {
  99.             $this->Add($_POST[$this->DataKey],$_POST);
  100.             $Data=$this->ReadCSV();
  101.         }    
  102.         if(isset($_POST['delete'])) {
  103.             $this->Delete($_POST[$this->DataKey]);
  104.             $Data=$this->ReadCSV();
  105.         }    
  106.         $PAGE=$this->EditList();
  107.         print $PAGE;    
  108.     }
  109.  
  110. //    Administration Area
  111.     function Update($key,$data) {        //Updating Item "key" with "data" named array
  112.         $this->ReadCSV();
  113.         for($i=0;$i<count($this->ItemsList);$i++) {
  114.             If($this->ItemsList[$i][$this->DataKey]==$key){
  115.                 while(list($key,$val)=each($this->HeaderData)) {
  116.                     if(isset($data[$val])) $this->ItemsList[$i][$val]=$data[$val];
  117.                 }
  118.             }    
  119.         }
  120.         $this->WriteData();
  121.         return($this->ItemsList);
  122.     }
  123.     function Add($key,$data) {            //add an Item "key" with "data" named array
  124.         $this->ReadCSV();
  125.         $NewLine=array();
  126.         $NewItem=array($this->DataKey=>$key);
  127.         while(list($key,$val)=each($this->HeaderData)) {
  128.             if(isset($data[$val])) {
  129.                 $NewItem[$val]=$data[$val];
  130.             } else {
  131.                 $NewItem[$val]=$data[$val]="";
  132.             }
  133.         }
  134.         array_push($this->ItemsList,$NewItem);
  135.         $this->WriteData();
  136.         return($this->ItemsList);
  137.     }
  138.     function EditList() {        //returns Editor's List of Items
  139.         reset ($this->ItemsList);
  140.         reset ($this->HeaderData);
  141.         $HHeaders=$this->HTTD(" ");
  142.         $HData="";
  143.         while(list($HKey,$HVal)=each($this->HeaderData)) {            //Create Headers Line
  144.             $HHeaders.=$this->HTTD($HVal);
  145.         }
  146.         $HHeaders=$this->HTTR($HHeaders);
  147.         while(list($LineKey,$LineVal)=each($this->ItemsList)) {    //Read Data Lines
  148.             $HDataLine="";
  149.             while(list($DataKey,$DataVal)=each($LineVal)) {            //Dissect one Data Line
  150.                 $HDataLine.=$this->HTInput($DataKey,$DataVal);
  151.             }
  152.             $HData.=$this->HTForm($LineVal[$this->DataKey],$this->HTTR($this->HTButton("commit").$HDataLine.$this->HTButton("delete")));    //and add HTML to Data
  153.         }
  154.         $HDataLine="";
  155.         reset($this->HeaderData);
  156.         while(list($DataKey,$DataVal)=each($this->HeaderData)) {            // Add an extra Line for Adding a record
  157.             $HDataLine.=$this->HTInput($DataVal,"");
  158.         }
  159.         $HData.=$this->HTForm($LineVal[$this->DataKey],$this->HTTR($this->HTButton("add").$HDataLine));    //and add HTML to Data
  160.         return($this->HTPage($this->HTTable($HHeaders.$HData)));
  161.     }
  162.     function Delete($DeleteKey) {        //Remove Item "Key" from the file
  163.         $inter=array();
  164.         while(list($key,$val)=each($this->ItemsList)) {
  165.             If($val[$this->DataKey]!=$DeleteKey)    array_push($inter,$val);
  166.         }
  167.         $this->ItemsList=$inter;
  168.         $this->WriteData();
  169.         return($this->ItemsList);
  170.     }
  171.     function WriteData() {        //writing contents of ItemList to Datafile
  172.         reset($this->ItemsList);
  173.         $Output=implode($this->Separator, $this->HeaderData)."\n";
  174.         while(list($key,$val)=each($this->ItemsList)) {
  175.             for($i=0;$i<count($this->HeaderData);$i++){
  176.                 $writekey=$this->HeaderData[$i];
  177.                 $writeitem[$writekey]=$val[$writekey];
  178.             }
  179.             $Output.=implode($this->Separator, $writeitem)."\n";
  180.         }
  181.         $fp = fopen ($this->DataFile,"w");
  182.         flock($fp,2);
  183.         fputs($fp,$Output);
  184.         flock($fp,3);
  185.         fclose($fp);
  186.     }
  187.  
  188. //    Accessory HTML output functions
  189.     function HTPage($value) {    // Places $value into BODY of HTML Page
  190.         $result = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
  191.         $result.="<html><head><title>".$this->DataFile." Editor</title>\n";
  192.         $result.="<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n";
  193.         $result.="<style type=\"text/css\">";
  194.         $result.="<!--  td { margin: 0px; padding: 0px; border: 1px solid #003399; } --></style></head>\n";
  195.         $result.="<body>\n".$value."</body>\n</html>";
  196.         return $result;
  197.     }
  198.     function HTForm($item,$data) {    //places $data into form $item
  199.         return "<form name=\"".$item."\" method=\"post\">\n".$data."</form>\n";
  200.     }
  201.     function HTTable($value) {        //places $value into TABLE
  202.         return "<table width=\"96%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n".$value."</table>\n";
  203.     }
  204.     function HTTR($value) {            //places $value into TR
  205.         $this->SRotate();
  206.         return "<tr>\n".$value."</tr>\n";
  207.     }
  208.     function HTTD($value) {    // places $value into TD
  209.         return "<td bgcolor=\"".$this->color."\">".$value."</td>\n";
  210.     }
  211.     function HTInput($field,$value) {    //returns TD input field
  212.         $Olen=strlen($value);
  213.         if($Olen<3) {
  214.             $Ilen=12;
  215.         } else {
  216.             $Ilen=$Olen;
  217.         }
  218.         return "<td bgcolor=\"".$this->color."\"><input name=\"".$field."\" type=\"text\" id=\"".$field."\" value=\"".$value."\" size=\"".$Ilen."\"></td>\n";
  219.     }    
  220.     function HTButton($value) {    // returns "$value" button
  221.         return "<td><input name=\"".$value."\" type=\"submit\" id=\"".$value."\" value=\"".$value."\"></td>\n";
  222.     }
  223.     function SRotate() {        //rotating colors for more readability of tables
  224.         if($this->color=="#FFFFFF") {
  225.             $this->color="#CCEEFF";
  226.         } else {
  227.             $this->color="#FFFFFF";
  228.         }
  229.     }
  230. }
  231. ?>
Newbie
 
Join Date: Oct 2006
Posts: 4
#6: Oct 9 '06

re: how to read with php from .csv file?


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
Newbie
 
Join Date: Jul 2008
Posts: 1
#7: Jul 6 '08

re: how to read with php from .csv file?


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.
Reply