Forum Moderators: coopster

Message Too Old, No Replies

How do I skip the first line

How do I skip the first line in a CSV file

         

fredfletcher

12:54 am on Feb 6, 2011 (gmt 0)

10+ Year Member



Hello,

Someone posted this code a while ago in order to show random lines from within a CSV file, the original URL being on this site at [webmasterworld.com...]

Here is the code:

<?php
$filename = $_SERVER["DOCUMENT_ROOT"].'/inc/pub/pub_array.inc';
$how_many_to_show = 4;
if ($fileContents = file($filename)) {
shuffle($fileContents);
for ($i = 0; $i < $how_many_to_show; $i++) {
print($fileContents[$i]. '<br />');
}
} else {
die('Could not get contents of: ' . $filename);
}
?>

The first row of my CSV file which is delimited by a semi-colon has headers for columns showing what type of fields the info is; for example: First name; Last Name; City; Province; Country

Then the 2nd row has their info.

I would like to be able to generate random info and skip the first line, because when I execute the script, it will sometimes show the headers which I do not want, any way of modifying the script in order to do that?

Thank you :)

rainborick

1:38 am on Feb 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Once you retrieve the file contents with file(), you could use array_shift($fileContents) to pop the first element out of the array, and then proceed.

fredfletcher

2:10 am on Feb 6, 2011 (gmt 0)

10+ Year Member



Thanks. But how would I go about to do that? I'm not a PHP programmer, just able to get by on modifying code once in a while, can you show me how I can do that please?

astupidname

2:50 am on Feb 6, 2011 (gmt 0)

10+ Year Member



I personally would prefer not to use file() in this instance, as once you have that and you want to deal with $fileContents[$i] you still need to accurately parse that into csv columns in order to use it with the headings if desired. I'd rather let php's in-built fgetcsv() function parse the columns apart. I guess I would go about it like this:

<?php
$filename = 'someCsvFile.csv';
$how_many_to_show = 4;
$csv = @fopen($filename, 'r');
if ($csv) {
$csvData =array();
$csvHeadings = fgetcsv($csv, 4000); //assuming the first line of the csv file contains the headings
if (!$csvHeadings) {
die('Unable to parse csv file headings!');
}
$numHeadings = count($csvHeadings);
while (!feof($csv)) { //read the csv file into our $csvData array
$tmp = fgetcsv($csv, 4000);
if ($tmp) {
array_push($csvData, $tmp);
}
}
shuffle($csvData); //randomize the order of $csvData items
for ($i = 0; $i < $how_many_to_show; $i++) {
if (count($csvData[$i]) == $numHeadings) { //array_combine() requires both arrays to be same length
$csvItem = array_combine($csvHeadings, $csvData[$i]);
//$csvHeadings items become keys in $csvItem, $csvData[$i] items become the values in $csvItem
$s = '<h3>$csvItem '.$i."</h3>\r\n<p>";
foreach ($csvItem as $key => $value) { //now you have access to the keys (heading names) if desired
$s .= $key.' => '.$value."<br>\r\n";
}
echo preg_replace('/<br>\\r\\n$/', "\r\n</p>\r\n", $s);
}
}
} else {
die('Could not get contents of: ' . $filename);
}
?>

If anybody's wondering, how to post formatted code on webmasterworld [webmasterworld.com]
Do not copy formatted code on webmasterworld from IE, use other browser such as Firefox.


GO PACK GO!

astupidname

2:58 am on Feb 6, 2011 (gmt 0)

10+ Year Member



my CSV file which is delimited by a semi-colon

Oh, missed that about the semi-colon delimiter. So in the code I posted you may need to change the fgetcsv($csv, 4000) to fgetcsv($csv, 4000, ';') in two places.

Also:
//read the csv file into our $csvData array

should have been
//read the REST of the csv file into our $csvData array

as the first line of the csv has already been pulled as $csvHeadings

fredfletcher

3:16 am on Feb 6, 2011 (gmt 0)

10+ Year Member



I have 2 words... WOW and THANKS!

I love the way you made the output come out, it's very well laid-out!

Thanks again, I appreciate it very much :) it works beautifully!

Cheers!

Fred

fredfletcher

5:12 am on Feb 6, 2011 (gmt 0)

10+ Year Member



I have another question; any way of writing the results output of the random info generated to another/new file?

astupidname

8:43 am on Feb 6, 2011 (gmt 0)

10+ Year Member



Sure, open a file using fopen(), then use fputcsv() with the current array of csv line items. Just a few minor additions to the code I posted earlier will be needed:

.....
shuffle($csvData); //randomize the order of $csvData items
$resultsFile = fopen('randomResults.csv', 'w'); //will write the results to this file
for ($i = 0; $i < $how_many_to_show; $i++) {
fputcsv($resultsFile, $csvData[$i], ';'); //write the current array from $csvData into $resultsFile as csv fields
if (count($csvData[$i]) == $numHeadings) { //array_combine requires both arrays to be same length
$csvItem = array_combine($csvHeadings, $csvData[$i]);
//$csvHeadings items become keys in $csvItem, $csvData[$i] items become the values in $csvItem
$s = '<h3>$csvItem '.$i."</h3>\r\n<p>";
foreach ($csvItem as $key => $value) { //now you have access to the keys (heading names) if desired
$s .= $key.' => '.$value."<br>\r\n";
}
echo preg_replace('/<br>\\r\\n$/', "\r\n</p>\r\n", $s);
}
}
fclose($resultsFile);
} else {
.....

fredfletcher

12:27 am on Feb 8, 2011 (gmt 0)

10+ Year Member



Sweet! thanks, works beautifully! Gawd, I knew I should've kept up with programming! I was studying Turbo Pascal in the early 90s and along came Windows Paintbrush! That led me to animation and graphic arts after LOL! Took a left turn sometime back, and now it seems I'm heading back towards it! Not "Turbo Pascal" anyway haha. I appreciate it very much, thanks again :)