1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to read data from this .csv file

Discussion in 'PHP' started by kmzeron, Oct 21, 2013.

  1. #1
    hello,

    I have a large csv file and I want to get data from there.

    I found that script :
    <?php
    $row = 1;
    if (($handle = fopen("test.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $num = count($data);
            echo "<p> $num fields in line $row: <br /></p>\n";
            $row++;
            for ($c=0; $c < $num; $c++) {
                echo $data[$c] . "<br />\n";
            }
        }
        fclose($handle);
    }
    ?>
    PHP:
    My csv file contain this : PRODUCT and AFFILIATE_CODE(url of affiliate). (ex . : Laptop Lenovo,http:// affiliatecode . com / abcd123)
    Now I want to make the script to print like this : PRODUCT

    Is that possible ?
    Thank you.
     
    kmzeron, Oct 21, 2013 IP
  2. thomasdgr

    thomasdgr Active Member

    Messages:
    167
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #2
    The lines in your csv file have the form: PRODUCT,AFFILIATE_CODE .
    So, on the script you posted, with the for loop you have for each line: $data[0] is the product and $data[1] is the url.
    In order to echo this as a link you have to add the following after for loop:
    echo '<a href="'.$data[1].'">'.$data[0].'</a>';
    Code (markup):
    You can also remove the $row counter and the line that echos the number of fields in each line if you don't need them.
     
    thomasdgr, Oct 21, 2013 IP
    kmzeron likes this.
  3. kmzeron

    kmzeron Well-Known Member

    Messages:
    734
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    128
    #3
    Yes, that is want I want, I eliminated the lines that I don't need them.
    THank you very much for the explanation.
     
    kmzeron, Oct 21, 2013 IP