Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    49
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Pasing a csv file in PHP and Hiding First Line

    Hello

    I'm hoping someone can point me in the right direction as I have a small issue I can't quite work out.

    I have a php page which reads a csv file and works fine; however I would like to do one of two things

    either

    Hide the first line of the csv file so it is not displayed in the php page

    or

    Display the first line in a different format (i.e. Bold and all centralised)

    The reason for this is so I can title each column in the csv file so someone else can edit and they would then know what information is required in each column

    The current code is (so the first row of the Table would be the titles)

    Code:
    <a name="dining"></a><b>LOCAL DINING</center></b>
    
    <table border="0" cellpadding="1" cellspacing="0" width="875">
    <tr>
    <td width="125" align="center" valign="top"><b>LOCATION</b></td>
    <td width="200" align="center" valign="top"><b>NAME</b></td>
    <td width="125" align="center" valign="top"><b>PHONE No</b></td>
    <td width="125" align="center" valign="top"><b>WEBSITE</b></td>
    <td width="300" align="center" valign="top"><b>DETAILS</b></td>
    </tr>
    
    <?PHP  
    
    $Dining = fopen("csv/Dining.csv", "r");  
    while (!feof($Dining) ) {  
    $line = fgetcsv($Dining, 1024);  
    
    echo '<tr>';
    echo '<td width="125" align="center" valign="top">' . $line[0] . '</td>';
    echo '<td width="200" align="center" valign="top">' . $line[1] . '</td>';
    echo '<td width="125" align="center" valign="top">' . $line[2] . '</td>';
    echo '<td width="125" align="center" valign="top"><a href=' . $line[3] . ' target=_new>' . $line[4] . '</a></td>';
    echo '<td width="300" align="left" valign="top">' . $line[5] . '</td>';
    echo '</tr>';
    
    }  
    fclose($Dining);  
    ?>
    
    </table>
    Can anyone advise

    Many Thanks

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    First I suggest updating from HTML 3.2 as that was superseded in 1997. Also move the open and read of the file to before the start of the table. You also need to keep track of which line of the CSV you have read. You can then produce the headings using the first line read in and process the rest of the table for the rest of the lines.

    So your HTML would look like this:

    Code:
    <?php
    $Dining = fopen("csv/Dining.csv", "r");  
    $count = 0;
    while (!feof($Dining) ) {  
    $line = fgetcsv($Dining, 1024);
    $count++;
    if ($count === 1) {  
    ?>
    <h1 id="dining">LOCAL DINING</h1>
    
    <table><thead>
    <tr>
    <th style="width:125px"><?php echo $line[0] ?></th>
    <th style="width:200px"><?php echo $line[1] ?></th>
    <th style="width:125px"><?php echo $line[2] ?></th>
    <th style="width:125px"><?php echo $line[4] ?></th>
    <th style="width:300px"><?php echo $line[5] ?></th>
    </tr></thead><tbody>
    
    <?PHP  
    } else {
    echo '<tr>';
    echo '<td>' . $line[0] . '</td>';
    echo '<td>' . $line[1] . '</td>';
    echo '<td>' . $line[2] . '</td>';
    echo '<td><a href=' . $line[3] . ' target=_new>' . $line[4] . '</a></td>';
    echo '<td style="text-align:left">' . $line[5] . '</td>';
    echo '</tr>';
    }
    }  
    fclose($Dining);  
    ?>
    
    </tbody></table>
    The rest of the table styling would be defined in the CSS file attached to the page.

    Note that <th> tags are bold and centered by default so you will not need to apply additional styles to get that effect.

    Try this CSS to apply the rest of the styles you need:

    Code:
    table {border:0; border-collapse:collapse;width:875px;}
    th, td {text-align:center;vertical-align:top;}
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    gazza3665 (02-09-2014)

  • #3
    New Coder
    Join Date
    Jan 2011
    Posts
    49
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Many thanks for the assistance, works well

    Just need to resolve a css issue as I want to apply different variables to different tables. Will try to figure this out


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •