Displaying CSV file in PHP

This is a discussion on "Displaying CSV file in PHP" within the PHP Forum section. This forum, and the thread "Displaying CSV file in PHP are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Aug 15th, 2006, 11:57
Junior Member
Join Date: Jul 2005
Location: Reading
Age: 44
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Displaying CSV file in PHP

I'm looking to display the contents of a CSV file on my PHP page.

You can view what I've done so far at: http://www.readingspeedway.co.uk/table06.php

I'm fairly happy with that, but would now like to do the follow:

1. What I would also like to do is to have a second page which only displays certain columns (a smaller version of the table). The columns I wish to have displayed on that page is: M, PTS, BP, TOT

2. Also if possible I would like 'READING'S' row to be displayed in a different colour. So for example if Reading are currently top of the league, or mid table, then it will be highlighted in yellow text.

What code do I need to add/ edit, to be able to do this?

Please find below a print out of the current code on that page:

<?php

//
$filename = "http://www.readingspeedway.co.uk/files/tables/spreadsheets/table06.csv"; // File to open. quote out this variable if you are using a form to link to this script.


/*
No cache!!
*/
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// always modified
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
/*
End of No cache
*/



function viewlog($filename) {

//formatting options...
$alignCentre = 'align="center" ';
$frmt_embolden = 'style="font-weight:bold; font-size:11px" ';


//start the table...
$returnStr = "<table border=0 bordercolor=white cellspacing=1 cellpadding=1 width=95% font-family: Verdana, Arial, Helvetica, sans-serif; align='center' style='font-size:11pt'>";


$handle = fopen($filename,"r");


while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data); // <--- this is the number of columns in the line read in....
$row++;

//each line represents a row, so open the row...
$returnStr .= "<tr>";

for ($i=0; $i < $num; $i++) {
//now insert the column details...

//say you want to align cols centre then
if($i == 0 || $i == 2 || $i == 3 || $i == 4 || $i == 5 || $i == 6 || $i == 7 || $i == 8 || $i == 9 || $i == 10 || $i == 11 || $i == 12 || $i == 13){
$returnStr .= "<td " . $alignCentre;
}else{
$returnStr .= "<td ";
}//end if-else




//say you want to embolden the heading row...
if($row == 3){
$returnStr .= $frmt_embolden;
}




$returnStr .= "> " . $data[$i] . "</td>";

}//end for

//close the row...
$returnStr .= "</tr>";


}//end while

//end the table...
$returnStr .= "</table>";

//gets the last mod date of the file...
$fileModTime = filemtime($filename);
//now format the date...
$fileModTime = date("D d M Y", $fileModTime);



fclose($handle);
return $returnStr;

}//end function


echo "<html><head><base href=\"./\"><title>ELITE LEAGUE TABLE - TEST</title>
<style type='text/css'>
<!--
body,td,th {
font-family: arial, verdana, tahoma, sans-serif;
font-size: 11px;
color: #FFFFFF;
}
-->
</style>


</head><body bgcolor=#b2181a>";
// Start the table definition of your choice
echo "<p>";

echo viewlog($filename);
echo "</p></body></html>";
exit;
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Aug 15th, 2006, 17:48
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Displaying CSV file in PHP

You can do what you want using css.

Give each of the columns you don't want to appear on a certain page a class of say hide. Only have that class available on that page and use 'display: none;' to hid them.

Similarly, give Reading and id and define a css style to suit your needs.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Aug 15th, 2006, 18:44
Junior Member
Join Date: Jul 2005
Location: Reading
Age: 44
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Displaying CSV file in PHP

Quote:
Originally Posted by ukgeoff View Post
You can do what you want using css.



Give each of the columns you don't want to appear on a certain page a class of say hide. Only have that class available on that page and use 'display: none;' to hid them.

Similarly, give Reading and id and define a css style to suit your needs.
Thanks for your help, but I'm going to need a bit more help here.

Can you please take a look at my code I've pasted above and tell me how I can assign the READING class to the appropriate row. Please bear in mind Reading's position in the table may and will change.

I have now managed to work out how to hide the appropriate columns thanks to your help.



Thanks very much.

Last edited by AndyP; Aug 15th, 2006 at 19:38.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Aug 15th, 2006, 20:16
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Displaying CSV file in PHP

In the code that reads your csv file, each time you read a name in, check to see if itis Reading and if it is, assign that cell the 'id' for the Reading style.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Aug 15th, 2006, 20:47
Junior Member
Join Date: Jul 2005
Location: Reading
Age: 44
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Displaying CSV file in PHP

Quote:
Originally Posted by ukgeoff View Post
In the code that reads your csv file, each time you read a name in, check to see if itis Reading and if it is, assign that cell the 'id' for the Reading style.
So how do I read a row to see if contains a cell with Reading in?

I really need some help with this.

Would I add something like this?

$frmt_TeamName = 'style="color:#FFFF00; font-size:11px; " '; //make text yellow

//say you want to READING row yellow text....
if($row == "Reading"){
$returnStr .= $frmt_TeamName;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
displaying, csv, file, php

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
how do I use a:hover within a html file (without the css file) heyo Web Page Design 2 Mar 5th, 2007 22:50
Creating a log file (text file) and an XML file using XSL kdelacruz Other Programming Languages 1 Nov 4th, 2006 21:12
PHP file displaying wrongly ??? j4mes_bond25 PHP Forum 6 May 19th, 2006 17:35
Need help displaying XML Zhan Web Page Design 5 Aug 19th, 2005 18:47
How make Word file same as html file in ASP humair Classic ASP 5 Sep 24th, 2003 14:35


All times are GMT. The time now is 02:20.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42