PHP to return set number of icons.

Hello,

I have some code that I require assistance with

I have a csv file. If I would like to be able to retrieve only 10 items at a time. (i.e. 1 – 10, then 11 – 20 etc) or a pre-defined number of records (hard coded), how do I go about doing this? Their is only one column in the csv file, but potentially 1000’s of rows Code would be helpful. Here is what a line in the csv looks like:

testinmg

<target=’_blank’>A-src=  jpg

Satin wedding gown features overall soft lace. a>

<a href=‘http://www.bestdeals247.com/weddings/go.php?proddb=7&l=1 target=’_blank’><img style=‘WIDTH: 102px; HEIGHT: 42px’ title=‘Buy Now’ border=‘0’ alt=src=‘images/_02.png’>

<span style=‘FONT-SIZE: 14pt’ US$271.98


So, if their was 100 records (rows), I may want to be able to only get 1-20, or 21-30 etc.

Any help much appreciated.

[php]

<?PHP $file_handle = fopen("widgets.csv", "r"); while (!feof($file_handle) ) { $line_of_text = fgetcsv($file_handle, 1024); print $line_of_text[0] . $line_of_text[1]. $line_of_text[2] . "
"; } fclose($file_handle); ?>

[/php]

that was my original idea, is their anyway to perhaps say instead of getting the output line by line, to perhaps say “get only the first 10 lines” etc? rather than have to enter each line manually?

No, because it is a file, not a database where you have sql scripts etc.

Create variable $i=0;
Inside while $i++;
Then if i == 15, break out of loop.

Play around,…

Thanks for your help. So far I have this that returns manually specified lines from the csv:

<?php $myFile = "abc.csv"; $fh = fopen($myFile, 'r'); $lines = file($myFile); echo $lines[1]; echo $lines[2]; echo $lines[3]; echo $lines[4]; echo $lines[5]; echo $lines[6]; echo $lines[7]; echo $lines[8]; echo $lines[9]; echo $lines[10]; ?>

However, I am wanting to be able to select say 1-10, or 11-21 of the values at a time. How do I go about doing this with a for loop? Any assistance or code would be much appreciated.

[php]

<?PHP $myFile = "abc.csv"; $lines = file($myFile); //First 10 for($i=0; $i<10; $i++) { echo $lines[$i]; } //Lines 10-20 for($i=10; $i<21; $i++) { echo $lines[$i]; } //Lines 100-200 for($i=100; $i<201; $i++) { echo $lines[$i]; } ?>

[/php]

Get the idea?

Yes, thank-you for that, much appreciated.

Sponsor our Newsletter | Privacy Policy | Terms of Service