SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot boognish's Avatar
    Join Date
    Sep 2005
    Location
    Leeds
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    csv to variables to html table?

    I need to to write a php script that reads a csv file, coverts the values to variables, and displays the appropriate values in an html table.

    So far I've only managed to convert the csv file direct to an html table, without converting them into variables so I can filter only the values needed. http://rippledesigns.co.uk/click/example.php from the csv file... http://rippledesigns.co.uk/click/cfp/xfile.txt

    Can anyone show me how to do this please?

    PHP Code:
    <?php

    $filename 
    "cfp/xfile.txt"

    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

    function viewlog($filename) {
    $fp fopen($filename,"r");
    $file fread($fp,65535);
    $replaced eregi_replace(",""<td>"$file);
    $replaced2 eregi_replace("\n""<tr><td>"$replaced);
    $replaced3 eregi_replace("\r""<tr><td>"$replaced2);
    fclose($fp);

    return 
    $replaced3;
    }
    echo 
    "<html><head><base href=\"./\"><title>CSV File Viewer</title></head><body bgcolor=silver>";
    // Start the table definition of your choice
    echo "<table border=0 bordercolor=black cellspacing=0 cellpadding=5 width=100% style='font-size:10pt'>";

    echo 
    viewlog($filename);
    echo 
    "</table></body></html>";
    exit;
    ?>

  2. #2
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Zealot boognish's Avatar
    Join Date
    Sep 2005
    Location
    Leeds
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thanks for the link. Still a bit unsure how to implement this. I'm still quite new at php. I used the example given for fgetcsv http://rippledesigns.co.uk/click/example2.php . If for example I wanted to only display the PropertyID, PropertyName and Price values in a table, with the table headers 'Image', 'Name of Property' and 'The Price', how would I achieve that?

  4. #4
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php $handle fopen("csv.txt""r"); ?>
    <table>
        <thead>
            <th>
                Image
            </th>
            <th>
                Name
            </th>
            <th>
                Price
            </th>
        </thead>
        <tbody>

    <?php while (($data fgetcsv($handle1000",")) !== FALSE): ?>
        <tr>
            <td><?= $data[1?></td>
            <td><?= $data[2?></td>
            <td>$<?= number_format($data[7], 2?></td>
        </tr>
    <?php endwhile; ?>

        </tbody>
    </table>
    fgetcsv returns the next line of the CSV in an array. So you loop through the CSV and print the fields you need

  5. #5
    SitePoint Zealot boognish's Avatar
    Join Date
    Sep 2005
    Location
    Leeds
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You star! Thanks for that, that's great, works a treat

  6. #6
    SitePoint Zealot Ambr0's Avatar
    Join Date
    Apr 2000
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Finally found an answer to one of my problems?

    Could anyone tell me how do I filter CSV rows so I could only display those whose data[1] column contains a certain word (as in "only display csv rows whose data[1] entry has at least one instance of the word "happy")?

    Thanks.

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if (false !== stripos($data[1], 'happy')) {



  8. #8
    SitePoint Zealot Ambr0's Avatar
    Join Date
    Apr 2000
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fantastic! I love you!

  9. #9
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ambr0 View Post
    Fantastic! I love you!
    Hi,

    I think this is nearly exact what i need, except i want to diplay a row if data[2] column is not empty.
    My knowledge of php is very poor, but with an working example i'll should be able to change it to my own needs.
    Can you post your complete script here ?

    Thnx anyway,
    Ronald

  10. #10
    SitePoint Zealot Ambr0's Avatar
    Join Date
    Apr 2000
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by galen View Post
    PHP Code:
    <?php $handle fopen("csv.txt""r"); ?>
    <table>
        <thead>
            <th>
                Image
            </th>
            <th>
                Name
            </th>
            <th>
                Price
            </th>
        </thead>
        <tbody>

    <?php while (($data fgetcsv($handle1000",")) !== FALSE): ?>
        <tr>
            <td><?= $data[1?></td>
            <td><?= $data[2?></td>
            <td>$<?= number_format($data[7], 2?></td>
        </tr>
    <?php endwhile; ?>

        </tbody>
    </table>
    fgetcsv returns the next line of the CSV in an array. So you loop through the CSV and print the fields you need
    Me again. I now have a csv feed inside a .zip file. What do I need to change inside this code in order to parse it?

    Thanks once more,
    David
    Soccer Directory | Travel Directory
    (free - reciprocal will give you featured status)

  11. #11
    SitePoint Zealot Ambr0's Avatar
    Join Date
    Apr 2000
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by galen View Post
    PHP Code:
    <?php $handle fopen("csv.txt""r"); ?>
    <table>
        <thead>
            <th>
                Image
            </th>
            <th>
                Name
            </th>
            <th>
                Price
            </th>
        </thead>
        <tbody>
     
    <?php while (($data fgetcsv($handle1000",")) !== FALSE): ?>
        <tr>
            <td><?= $data[1?></td>
            <td><?= $data[2?></td>
            <td>$<?= number_format($data[7], 2?></td>
        </tr>
    <?php endwhile; ?>
     
        </tbody>
    </table>
    fgetcsv returns the next line of the CSV in an array. So you loop through the CSV and print the fields you need
    Me once again. Feed works great but I got a complaint from the affiliate merchant that I'm getting their servers down. They want me to download the feed on my server once a day after midnight and use it from there. How on earth can I do that based on this script? I really need an answer otherwise the merchant will stop letting me use their feeds.

    Thanks once again,
    David
    Soccer Directory | Travel Directory
    (free - reciprocal will give you featured status)


Bookmarks

Posting Permissions

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