Jump to content

Recommended Posts

I have been trying to find a way to display a csv table onto a web page and have found this example.

 

Have a look at http://www.rhinoracks.co.uk/data.php this is what I want  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 throws it all out of sync!

 

code here

 

 

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

Link to comment
https://forums.phpfreaks.com/topic/75660-solved-help-to-edit-this-code-please/
Share on other sites

Simplified/cleaner version:

<html>
<body>

<table border="1" cellspacing="0" cellpadding="5" width="440" style="border-collapse:collapse;">
  <tr style="background-color:gainsboro">
    <th>Product Code</th>
    <th>Description</th>
    <th>Price<br />(exc VAT)</th>
    <th>Price<br />(Inc. VAT)</th>
  </tr>
<?php

$odd  = 'whitesmoke';
$even = 'azure';

$products = file('info.txt');

foreach($products as $i => $product_info)
{
    list($product_code, $description, $price, $priceVAT) = explode('~', $product_info);

    $bg = ($i%2) ? $odd : $even;
    
    echo "  <tr style=\"background-color:$bg\">
    <td>$product_code</td>
    <td>$description</td>
    <td>\$$price</td>
    <td>\$$priceVAT</td>
  </tr>\n";

}

?>
</table>

</body>
</html>

 

info.txt:

001~Black Shoes~19.00~25.00
002~Light Blue T-Shirt~10.00~12.50
003~Item3 here~0.00~0.00
004~Item4 here~0.00~0.00

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.