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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How do I "read" data in a csv file using PHP?

    How would I go about setting up a PHP script to link to a image link contained in a csv file? Basically I have set up a Javascript carosel displaying product images. The difficulty i am having is i want it to only display published content as the products change frequently. How would a set up a php script to read my .csv file and link to an image on my sever only if the published tab says yes? Sorry if i havn't explained this very well but any help would be much appriciated.

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    This has nothing at all to do with HTML/CSS fixes, but here you go...

    To parse a CSV file in PHP you first need to open and read the file contents. Then you go line-by-line and pull the data into an array that you can use to run your intended process. Overall it looks something like this:

    PHP Code:
    $csv_file_path "path/to/file.csv";
    if(
    is_file($csv_file_path)){
        
    $my_csv_array=array();
        
    $file fopen($csv_file_path'r');
        while((
    $line fgetcsv($file)) !== FALSE){
            
    //each loop gives us a variable $line which is an array of the csv elements in the current row. So we push the row into the csv array...
            
    array_push($my_csv_array,$line);
        }
        
    fclose($file);

        
    //now we take a look at what is inside to make sure we have what we expected:
        
    print_r($my_csv_array);

        
    //and then we go on to do whatever it is we want to do with this information
        
    print "<p>This is where we would do something worthwhile after having deleted the print_r() bit just above this part.</p>";
    }
    else{
        print 
    "<h1>CSV FILE NOT FOUND.</h1>\n";

    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #3
    Senior Coder
    Join Date
    Aug 2010
    Location
    High Point, NC
    Posts
    3,338
    Thanks
    5
    Thanked 363 Times in 360 Posts
    Wouldn't it be easier to maintain in a MySQL database though? The code would virtually work the same way, but maintainence would be a lot easier.
    Teed

  • #4
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by teedoff View Post
    Wouldn't it be easier to maintain in a MySQL database though? The code would virtually work the same way, but maintainence would be a lot easier.
    Absolutely, for web use. OP might have a strong need to use a desktop application to manage things locally, though, and then upload to the server (or any number of other possibilities for why they might prefer csv data). You never know!
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #5
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,740
    Thanks
    41
    Thanked 191 Times in 190 Posts
    && (just to add ) some programs only allow to save off as csv files (*thinking in particular a lot of network traffic analysis tools that I've used in the past for packet capturing)

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #6
    Senior Coder
    Join Date
    Aug 2010
    Location
    High Point, NC
    Posts
    3,338
    Thanks
    5
    Thanked 363 Times in 360 Posts
    Quote Originally Posted by Rowsdower! View Post
    Absolutely, for web use. OP might have a strong need to use a desktop application to manage things locally, though, and then upload to the server (or any number of other possibilities for why they might prefer csv data). You never know!
    lol you are correct. I just always think in terms of dynamic page content..lol
    Teed


  •  

    Tags for this Thread

    Posting Permissions

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