6 Replies - 2096 Views - Last Post: 17 February 2012 - 12:24 PM Rate Topic: -----

#1 chris55   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-February 12

Read a csv file and input the relevant data

Posted 17 February 2012 - 07:52 AM

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.
Is This A Good Question/Topic? 0
  • +

Replies To: Read a csv file and input the relevant data

#2 Jstall   User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Read a csv file and input the relevant data

Posted 17 February 2012 - 08:00 AM

Hi,

Well, assuming that published content info is also stored in the CSV you could use fgetcsv() to parse the CSV file into an array and then do filtering based on whatever flag you use to mark something as published.
Was This Post Helpful? 0
  • +
  • -

#3 chris55   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-February 12

Re: Read a csv file and input the relevant data

Posted 17 February 2012 - 08:17 AM

View PostJstall, on 17 February 2012 - 08:00 AM, said:

Hi,

Well, assuming that published content info is also stored in the CSV you could use fgetcsv() to parse the CSV file into an array and then do filtering based on whatever flag you use to mark something as published.


Ok this is new to me so what code would i use to take a url from colum AB row 3 in my csv file only if row L said yes
Was This Post Helpful? 0
  • +
  • -

#4 Jstall   User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Read a csv file and input the relevant data

Posted 17 February 2012 - 08:45 AM

That would depend on the structure of your file. According to the manual fgetcsv() returns an indexed array representing one line in your CSV.

So lets say you had a CSV with the name "mycsv.csv" that had a person's first name,last name and street address with the following data:
Bob,Johnson,17 Fake St
John,Doe,5 Doe Lane



You can iterate through the file using a while loop. If you are looking for a particular value just check the appropriate index. Something like this:
//open the file
$handle = fopen("mycsv.csv", "r");
// while there are records to read
while($row = fgetcsv($handle))
{
  // if the first name is Bob
  if($row[0] == "Bob")
  {
     echo "The name is Bob!"; 
  }
}



Remember, the array that is returned by fgetcsv is numerically indexed so in the case of this csv index 0 would be the first name, 1 would be the last name and 2 would hold the street address. Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#5 chris55   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-February 12

Re: Read a csv file and input the relevant data

Posted 17 February 2012 - 09:09 AM

You are being incredibally helpful and i'm sorry for being thick but here is the example i want to use:

Product Image URL Available Link
Product 1 mysite.com/product1.jpg Yes mysite.com/product1
Product 2 mysite.com/product2.jpg No mysite.com/product2


Now i want to load the products that are available to automatically display in my Javascript Carosell from the image links however not show if it says no under the available colum. How would i go about this to link to the jpeg if the product is available?

I really appriciate you taking your time and helping me and am sorry! Just am new to csv file and getting info based on the information in the file!!!


My javascript looks like this incase it helps and i want to replace the flickr images with the urls from active content on the csv file!!!
<script type="text/javascript">

// Set thickbox loading image
tb_pathToImage = "images/loading-thickbox.gif";

var mycarousel_itemList = [
  
    {url: "http://static.flickr.com/75/199481072_b4a0d09597_s.jpg", title: "Flower2"},
    {url: "http://static.flickr.com/57/199481087_33ae73a8de_s.jpg", title: "Flower3"},
    {url: "http://static.flickr.com/77/199481108_4359e6b971_s.jpg", title: "Flower4"},
    {url: "http://static.flickr.com/58/199481143_3c148d9dd3_s.jpg", title: "Flower5"},
    {url: "http://static.flickr.com/72/199481203_ad4cdcf109_s.jpg", title: "Flower6"},
    {url: "http://static.flickr.com/58/199481218_264ce20da0_s.jpg", title: "Flower7"},
    {url: "http://static.flickr.com/69/199481255_fdfe885f87_s.jpg", title: "Flower8"},
    {url: "http://static.flickr.com/60/199480111_87d4cb3e38_s.jpg", title: "Flower9"},
    {url: "http://static.flickr.com/70/229228324_08223b70fa_s.jpg", title: "Flower10"}
];

function mycarousel_itemLoadCallback(carousel, state)
{
    for (var i = carousel.first; i <= carousel.last; i++) {
        if (carousel.has(i)) {
            continue;
        }

        if (i > mycarousel_itemList.length) {
            break;
        }

        // Create an object from HTML
        var item = jQuery(mycarousel_getItemHTML(mycarousel_itemList[i-1])).get(0);

        // Apply thickbox
        tb_init(item);

        carousel.add(i, item);
    }
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(item)
{
    var url_m = item.url.replace(/_s.jpg/g, '_m.jpg');
    return '<a href="' + url_m + '" title="' + item.title + '"><img src="' + item.url + '" width="150" height="150" border="0" alt="' + item.title + '" /></a>';
};

jQuery(document).ready(function() {
    jQuery('#mycarousel').jcarousel({
        size: mycarousel_itemList.length,
        itemLoadCallback: {onBeforeAnimation: mycarousel_itemLoadCallback}
    });
});

</script>


Was This Post Helpful? 0
  • +
  • -

#6 Duckington   User is offline

  • D.I.C Addict

Reputation: 172
  • View blog
  • Posts: 615
  • Joined: 12-October 09

Re: Read a csv file and input the relevant data

Posted 17 February 2012 - 09:21 AM

I would highly recommend using the PHPExcel library.
Was This Post Helpful? 0
  • +
  • -

#7 Jstall   User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Read a csv file and input the relevant data

Posted 17 February 2012 - 12:24 PM

Hi,

Well it looks like you have to problems here.

The first one being a way to parse the CSV, and storing the urls for available images. Then putting the information on the DOM when the page renders.

Your next problem is getting those urls from the DOM using Javascript and using them in your carousel plugin.

One way to store the urls in an array while iterating through your CSV. So, in your while loop do a check if available is "Yes" and if so store the url. Something like
$handle = fopen("images.csv", "r");
//array to hold your available image urls
$available_images = array();
while($row = fgetcsv($handle))
{
  // if available is yes,add the image to your array
  if($row[3] == "Yes")
  {
     $available_images[] = $row[1];
  }
}



Now you have an array with all the available images. Next you need put them on the DOM in a way that makes them accessible after the page renders.

One way you could do this is using hidden inputs that your Javascript would look for and then use with your plugin. I will leave it up to you to figure out the best way to do this since I'm not sure what sort of templating system you are using etc.

So you end up with something like this on your page:
<input type = "hidden" class = "carousel_image" value = "mysite.com/product1.jpg" />
<input type = "hidden" class = "carousel_image" value = "mysite.com/product2.jpg" />



Now you want to use Javascript to get those values. In the example you gave you use a hard coded array of objects. You would want to build that dynamically. I see you are using jQuery, so something like this may do it:
//each of your hidden inputs
$(".carousel_image").each(function(){
     //add a new object to your array with the url dynamically set.
     mycarousel_itemList.push({url:$(this).val()});
});



Something like that would do what you are looking for, I believe. That's the first thing I came up with but it's Friday and my brain is mushy so that is not saying much :P

Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1