Jump to content

Minor tweak on CSV parser needed


baccarak

Recommended Posts

This is simple, I am sure, for a programmer, so dont laugh

 

Problem

I have an admin section that writes latest offers to a csv file that is automatically given an ID adds new record to the file

 

CSV Setup (bbs.txt)

ID|Offer|SubHeading|Description|PromoCode|Notes

 

I then parse it using this

 

<?php

$fp = fopen('bbs.txt','r');

if (!$fp) {echo 'ERROR: Unable to open file.</body></html>'; exit;}

$row_count = 0;

while (!feof($fp)) {

// this is to exclude the first row

if($row_count==0){

fgets($fp, 2048); //use 2048 if very long lines

$row_count++;

continue;

 

}

$line = fgets($fp, 2048); //use 2048 if very long lines

if (!empty($line)){

list ($id, $offer, $subh, $desc, $promo, $notes ) = split ('\|', $line);

 

//template start

print"$id

<center><h3>$offer</h3>

<h4>$subh</h4></center>

<p align='left'>$desc</p>

Please quote Promo Code:<div align center> <b>$promo</b></div><hr>";

//template end

}

$fp++;

}

fclose($fp);

 

?>

 

Works great but needs to show in reverse so newest added first (id) 4, 3, 2, 1 from bottom to top instead of 1, 2, 3, 4  - still ignoring the first line : ID|Offer|SubHeading|Description|PromoCode|Notes

 

Graphics/Photoshop help offered in exchange if needed - this is my final task to finish so I can then get paid ;-) so any help appreciated greatly.

 

Link to comment
https://forums.phpfreaks.com/topic/242529-minor-tweak-on-csv-parser-needed/
Share on other sites

do you mean?

 

list ($id, $offer, $subh, $desc, $promo, $notes ) = split ('\|', $line);

$line=array_reverse($id);

//template start

print"$id

<center><h3>$offer</h3></center>

<h4>$subh</h4>

<p align='left'>$desc</p>

Please quote Promo Code: <b>$promo</b><hr>";

//template end

}

$fp++;

}

fclose($fp);

 

?>

try

<?php
$fp = fopen('bbs.txt','r');
if (!$fp) {echo 'ERROR: Unable to open file.</body></html>'; exit;}
$row_count = 0;
while (!feof($fp)) {
// this is to exclude the first row
if($row_count==0){
fgets($fp, 2048); //use 2048 if very long lines
$row_count++;
continue;

}
$line = fgets($fp, 2048); //use 2048 if very long lines
if (!empty($line)){
list ($id, $offer, $subh, $desc, $promo, $notes ) = split ('\|', $line);
$output[$id]=$offer. $subh. $desc. $promo. $notes;
}
$fp++;
}
fclose($fp);

$output=array_reverse($output);
//template start
while(list($id, $values)=each($output)){
list ($offer, $subh, $desc, $promo, $notes ) = split (',', $values);
aprint"$id
<center><h3>$offer</h3>
<h4>$subh</h4></center>
<p align='left'>$desc</p>
Please quote Promo Code:<div align center> <b>$promo</b></div><hr>";
//template end
}
?>

 

Effectively this will load the file and close it, reverse the array then output each value.

ok, once more

 

<?php
$fp = fopen('test.csv','r');
if (!$fp) {echo 'ERROR: Unable to open file.</body></html>';
exit;
}
$row_count = 0;
while (!feof($fp)) {
// this is to exclude the first row
if($row_count==0){fgets($fp, 2048);
//use 2048 if very long lines
$row_count++;continue;
}
$line = fgets($fp, 2048); 
//use 2048 if very long lines
if (!empty($line)){list ($id, $offer, $subh, $desc, $promo, $notes ) = explode('\|', $line);
$output[$id]=$offer.",". $subh.",".  $desc.",".  $promo.",".  $notes;
}
$fp++;
}
fclose($fp);
$output=array_reverse($output);
//template start
while(list($id, $values)=each($output)){
	list ($offer, $subh, $desc, $promo, $notes ) = explode (',', $values);
	echo"$id<center><h3>$offer</h3><h4>$subh</h4></center><p align='left'>$desc</p>Please quote Promo Code:<div align center> <b>$promo</b></div><hr>";

	//template end
	}
?>

 

You should really replace split with explode for this as split is now deprecated

strange changed to print

 

print"<div align='center'><h3>$offer</h3><h4>$subh</h4></div><p align='left'>$desc</p><div align='center'>Please quote PromoCode:<b>$promo</b></div><hr>";

 

an now outputs: <div align="center"><h3></h3><h4></h4></div><p align="left"></p><div align="center">Please quote Promo Code:<b></b></div><hr>

sorted Nodral

all I had to do was to explode $id adding $no in the process:

 

from:

while(list($id, $values)=each($output)){

list ($offer, $subh, $desc, $promo, $notes ) = explode ('|', $values);

print"$id<center><h3>$offer</h3><h4>$subh</h4></center><p align='left'>$desc</p>Please quote Promo Code:<div align center> <b>$promo</b></div><hr>";

 

//template end

}

?>

to:

while(list($id, $values)=each($output)){

list ( $offer, $subh, $desc, $promo, $notes ) = explode ('|', $values);

list ( $no, $offer, $subh, $desc, $promo, $notes ) = explode ('|', $id);

print"<center><h3>$offer</h3><h4>$subh</h4></center><p align='left'>$desc</p><div align center> Please quote Promo Code:<b>$promo</b></div><hr>";

 

//template end

}

?>

 

thanks again

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.