Computer Programming web Web programming Tips

How to display a record from a csv datafeed file - PHP code example

By Sergey Skudaev

In this tutorial you will learn how to read csv file and display its field values on a web page. Complete PHP source code is provided.

Download filetoread.csv file with tab delemited fields. For simplicity the file has only six fields and five records. Create datafeed derictory in your web site root directory and save filetoread.csv file in datafeed directory. Look at the file records.

the PHP file with source code, which reads the csv file is explained below


//create table with header and column names print('<table align="center" width="800">'); print('<tr><td colspan="6">Computer electronics</td></tr>'); print('<tr> <td>Part</td> <td>number</td> <td>Product</td> <td>Company</td> <td>Link</td> <td>Price</td> </tr>'); //declare variable to count records $rowcount = 0; //get path to file assumming that file located in datafeed folder $path_to_file="datafeed/filetoread.csv"; //open file for reading "r" $handle = fopen($path_to_file, "r"); //read file line by line while (($record = fgetcsv($handle, 1000, " ")) !== FALSE) { // read number of fields in one record $numfields = count($record); //Display all fields for debugging, to know which data is in which field. //In real scv datafeed file of an affiliated program a lot of fields are stored //data, that you do not need. Using output below you can see what fields to select //to build your advertisement on your web page. <!---comment code below in production version--->      for($i=0; $i<$numfields; $i++)      {      echo $i.":".$record[$i]."<br>";      } <!---end debugging part---> //read field values in variable      $part=$record[0];      $number=$record[1];      $product=$record[2];      $company=$record[3];      $clicklink=$record[4];      $price=$record[5]; //skip the first record since it has field headers only    if($rowcount > 0)    {
   //if record has link to item display it in the table row      if($record[3] !="")      { print('<tr><td>'.$part.'</td> <td>'.$number.'</td> <td>'.$product.'</td> <td>'.$company.'</td> <td> <a href="'$clicklink.'"> '.$product.'</a> </td>'.$price.'</td> </tr>');      }

   } $rowcount++; } fclose($handle); print('</table>'); ?>

Download readcsv.txt file and save it as .php file in your web site root directory.

Download filetoread.csv file with tab delemited fields.

Remove txt extension after downloading files.

In a web page you want to display records from csv file type:


We are done.

My eBooks on


No comments yet.

Add Comment

* Required information
(never displayed)
Smile Sad Huh Laugh Mad Tongue Crying Grin Wink Scared Cool Sleep Blush Unsure Shocked
Type the numbers for four hundred seventy-two.
Enter answer:
Enter code:
Notify me of new comments via email.
Remember my form inputs on this computer.
I have read and understand the privacy policy. *
I have read and agree to the terms and conditions. *
Powered by Commentics