Jump to content

[SOLVED] Help to edit this code please:


roldahayes

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.