Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 10-31-2007, 09:00 AM Formating fgetcsv()
rolda hayes's Avatar
Wannabe Adventurer...

Posts: 961
Name: Darren
Location: England
Trades: 0
Hi I am trying to use the fgetcsv function to display a csv file in a "results" page.

The page is here http://www.rhinoracks.co.uk/test.php

Is it possible to format the output of this file, so the the data is displayed in a tabular form with colum and row shading?? like in the example table below the out put?

code is here... (also my pound signs are showing as squares so i guess ive got to add aline to the top of the page to sort this??)

Code:
<?PHP

$file_handle = fopen("./csv/berlingo.csv", "r");

while (!feof($file_handle) ) {


$line_of_text = fgetcsv($file_handle, 1024);

print $line_of_text[0] . $line_of_text[1]. $line_of_text[2] . "<BR>";

}

fclose($file_handle);

?>
__________________
I Just a test to see what happens...
Please login or register to view this content. Registration is FREE

"Let us be thankful for the fools. But for them the rest of us could not succeed..."

Last edited by rolda hayes; 10-31-2007 at 09:06 AM..
rolda hayes is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-31-2007, 10:59 AM Re: Formating fgetcsv()
Novice Talker

Posts: 5
Name: Alex
Trades: 0
Hi,

your code should looks something like:

PHP Code:
echo '<style type="text/css">
<!--
.style5 {font-family: Arial, Helvetica, sans-serif; font-size: 11px; color: #FFFFFF; }
.style6 {color: #CCCCCC}
-->
</style>'
;

echo 
'<table width="612" border="0" cellspacing="2" cellpadding="0">
  <tr>
    <td width="68" bgcolor="#000033"><div align="center"><span class="style5">Make</span></div></td>
    <td width="170" bgcolor="#000033"><div align="center"><span class="style5">Product Code </span></div></td>
    <td width="130" bgcolor="#000033"><div align="center"><span class="style5">Description</span></div></td>
    <td width="58" bgcolor="#000033"><div align="center"><span class="style5">Price</span></div></td>
    <td width="186" bgcolor="#000033"><div align="center"><span class="style5">Price inc VAT </span></div></td>
  </tr>'
;
  
$file_handle fopen("./csv/berlingo.csv""r");

$odd true;


$colors = array('#E0E0E0''#FFFFFF');

while (!
feof($file_handle) ) {

    
$line_of_text fgetcsv($file_handle1024);
    
$odd true;
    while (
$line_of_text)
    {   
        if (
$odd
            {
                
$clr $colors[0];
                
$odd false;                
            }
        else
            {
                
$clr $colors[1];
                
$odd true;                                
            }            
    echo 
'
    <tr>
            <td bgcolor="'
.$clr.'">'.$line_of_text[0].'</td>
            <td bgcolor="'
.$clr.'">'.$line_of_text[1].'</td>
            <td bgcolor="'
.$clr.'">'.$line_of_text[2].'</td>
            <td bgcolor="'
.$clr.'">'.htmlentities($line_of_text[3]).'</td>
            <td bgcolor="'
.$clr.'">&nbsp;</td>
    </tr>'
;
    
$line_of_text fgetcsv($file_handle1024);
    }
  
}

fclose($file_handle);                        
echo 
'</table>'
__________________
The Best
Please login or register to view this content. Registration is FREE
.
Neocoder is offline
Reply With Quote
View Public Profile
 
Old 10-31-2007, 11:16 AM Re: Formating fgetcsv()
rolda hayes's Avatar
Wannabe Adventurer...

Posts: 961
Name: Darren
Location: England
Trades: 0
Hmmm, that just produced loads of code at the top of the page...

I've changed the code slightly now to this, so it displays in a table but still cant figure out how to format it...

Code:
<?
//Define what you want the seperator to be, this could be new line, (\n) a tab (\t) or any other char, for obvious reasons avoid using chars that will be present in the string.  Id suggest a comma, or semicolon.
$sep = ",";

//define file to read
$file = "./csv/berlingo.csv";

//read the file into an array
$lines = file($file);

//count the array
$numlines = count($lines);

//explode the first (0) line which will be the header line
$headers = explode($sep, $lines[0]);

//count the number of headers
$numheaders = count($headers);

$i = 0;

//start formatting output
echo "<table border = 1 cellpadding = 2><tr>";

//loop through the headers outputting them into their own <TD> cells
while($i<$numheaders){
        $headers = str_replace("\"", "", $headers);
        echo "<td>".$headers[$i]."</td>";
        $i++;
        }

echo "</tr>";

$y = 1;

//Output the data, looping through the number of lines of data and also looping through the number of cells in each line, as this is a dynamic number the header length has to be reread.
while($y<$numlines){
        $x=0;
        echo "<TR>";
                while($x<$numheaders){
                $fields = explode($sep, $lines[$y]);
                $fields = str_replace("\"", "", $fields);
                echo "<TD>&nbsp;".$fields[$x]." </TD>";
                $x++;
                        }
        $y++;
        echo "</TR>";
        }

//close the table.
echo "</table>";
?>
__________________
I Just a test to see what happens...
Please login or register to view this content. Registration is FREE

"Let us be thankful for the fools. But for them the rest of us could not succeed..."
rolda hayes is offline
Reply With Quote
View Public Profile
 
Old 10-31-2007, 11:45 AM Re: Formating fgetcsv()
Novice Talker

Posts: 5
Name: Alex
Trades: 0
What did you mean by word "format"? Do you need to apply style to the table to make odd and even row different color? If so, why don't you just use CSS?
__________________
The Best
Please login or register to view this content. Registration is FREE
.
Neocoder is offline
Reply With Quote
View Public Profile
 
Old 10-31-2007, 11:50 AM Re: Formating fgetcsv()
rolda hayes's Avatar
Wannabe Adventurer...

Posts: 961
Name: Darren
Location: England
Trades: 0
thats exactly what im trying to do...!

but thats the part im struggling on where and how to put the css...

example results canbe found here http://www.autorack.co.uk/results.ph...here+to+search.

basically, we have the csv that runs that site and need a simplified version.
__________________
I Just a test to see what happens...
Please login or register to view this content. Registration is FREE

"Let us be thankful for the fools. But for them the rest of us could not succeed..."
rolda hayes is offline
Reply With Quote
View Public Profile
 
Old 10-31-2007, 06:07 PM Re: Formating fgetcsv()
solomongaby's Avatar
Webmaster Talker

Latest Blog Post:
How Do You Find Music Online ?
Posts: 522
Name: Gabe Solomon
Location: Romania
Trades: 1
then you might want think about building a code that separate the actual data from the html in the cvs
__________________
If you like my posts ... TK is appreciated:)

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
solomongaby is offline
Reply With Quote
View Public Profile Visit solomongaby's homepage!
 
Old 10-31-2007, 10:48 PM Re: Formating fgetcsv()
SmartBomb's Avatar
Average Talker

Posts: 27
Name: Dave
Trades: 0
I'd suggest using fread instead of using fgetcsv and formatting the data from there by using explode() to split the values in the CSV file.
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
SmartBomb is offline
Reply With Quote
View Public Profile
 
Old 11-01-2007, 06:01 AM Re: Formating fgetcsv()
rolda hayes's Avatar
Wannabe Adventurer...

Posts: 961
Name: Darren
Location: England
Trades: 0
Thanks, I'll have a read up on those commands today.... (and I thought this was going to be easy...!)
__________________
I Just a test to see what happens...
Please login or register to view this content. Registration is FREE

"Let us be thankful for the fools. But for them the rest of us could not succeed..."
rolda hayes is offline
Reply With Quote
View Public Profile
 
Old 11-01-2007, 07:51 AM Re: Formating fgetcsv()
rolda hayes's Avatar
Wannabe Adventurer...

Posts: 961
Name: Darren
Location: England
Trades: 0
Ok hit a wall here...

Can anyone give an example code of how to go about this???

Ive read up on fread and explode() but its not making sense to what im trying to do...
__________________
I Just a test to see what happens...
Please login or register to view this content. Registration is FREE

"Let us be thankful for the fools. But for them the rest of us could not succeed..."
rolda hayes is offline
Reply With Quote
View Public Profile
 
Old 11-01-2007, 12:04 PM Re: Formating fgetcsv()
rolda hayes's Avatar
Wannabe Adventurer...

Posts: 961
Name: Darren
Location: England
Trades: 0
right think im getting somewhere...

Have a look at http://www.rhinoracks.co.uk/data.php this is ok and I can customise the look and fit it into my pages fine...

Can someone help with adjusting the code to my fields though... Im getting nowhere with it!

I need 1 less colum so that it would be

"product code" "description" "price(ex vat)" "price inc vat"

When I try and edit fields it throughs it all out!!

code here

Code:
<html><body>
<table border='1' cellspacing='0' cellpadding='5'
       width="440" style="border-collapse:collapse;
       font-family:sans-serif;">
<tr style="background-color:gainsboro"><th colspan="2">
Employee</th><th>Sex</th>
<th>Hired</th><th>Salary</th></tr>
<colgroup>
<col span="2" align="left" />
<col span="1" align="center" />
<col span="2" align="right" />
<?PHP
$oddColor='whitesmoke';
$evenColor='azure';
$ID=0; $FirstName=1; $LastName=2; $HireDate=3;
$ReviewDate=4; $Salary=5; $Sex=6; $IsSelected=6;
$TheFile = fopen ("info.txt", "r");
if($TheFile){
    $row = 0;
    $Employee = strval(fgets($TheFile, 4096));
    while (!feof ($TheFile)) {
        $rec = explode("~",$Employee);
        if($row % 2 == 0) {
            $rowStart = "<tr style='background-color:".$evenColor."'><td>"; }
        else {
            $rowStart = "<tr style='background-color:".$oddColor."'><td>"; }
        print $rowStart;
        print $rec[$FirstName]. "</td><td>" ;
        print $rec[$LastName]. "</td><td>" ;
        print $rec[$Sex]. "</td><td>" ;
        print $rec[$HireDate]. "</td><td>" ;
        print "$".$rec[$Salary]. "</td></tr>";
        $row++;
        $Employee = strval(fgets($TheFile, 4096));
    }
    fclose ($TheFile);
}
?>
</table></body></html>
__________________
I Just a test to see what happens...
Please login or register to view this content. Registration is FREE

"Let us be thankful for the fools. But for them the rest of us could not succeed..."
rolda hayes is offline
Reply With Quote
View Public Profile
 
Old 11-01-2007, 05:51 PM Re: Formating fgetcsv()
rolda hayes's Avatar
Wannabe Adventurer...

Posts: 961
Name: Darren
Location: England
Trades: 0
Solved it...

http://www.rhinoracks.co.uk/berlingo.php

thanks for your help guys!
__________________
I Just a test to see what happens...
Please login or register to view this content. Registration is FREE

"Let us be thankful for the fools. But for them the rest of us could not succeed..."
rolda hayes is offline
Reply With Quote
View Public Profile
 
Old 11-02-2007, 10:12 AM Re: Formating fgetcsv()
Novice Talker

Posts: 5
Name: Alex
Trades: 0
Check your code, your pound sign not displayed. Add semicolon after '&pound' in code.
__________________
The Best
Please login or register to view this content. Registration is FREE
.
Neocoder is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Formating fgetcsv()
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.60623 seconds with 12 queries