baccarak Posted July 21, 2011 Share Posted July 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/242529-minor-tweak-on-csv-parser-needed/ Share on other sites More sharing options...
Nodral Posted July 21, 2011 Share Posted July 21, 2011 You could create the $list array using id as the key then before you output it, use the array_reverse function. Quote Link to comment https://forums.phpfreaks.com/topic/242529-minor-tweak-on-csv-parser-needed/#findComment-1245569 Share on other sites More sharing options...
baccarak Posted July 21, 2011 Author Share Posted July 21, 2011 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/242529-minor-tweak-on-csv-parser-needed/#findComment-1245611 Share on other sites More sharing options...
Nodral Posted July 21, 2011 Share Posted July 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/242529-minor-tweak-on-csv-parser-needed/#findComment-1245624 Share on other sites More sharing options...
Nodral Posted July 21, 2011 Share Posted July 21, 2011 Ignore that last post!!! It's a load of rubbish!!! lol That's what happens when a phone rings 7 times while trying to write a script!! Quote Link to comment https://forums.phpfreaks.com/topic/242529-minor-tweak-on-csv-parser-needed/#findComment-1245633 Share on other sites More sharing options...
Nodral Posted July 21, 2011 Share Posted July 21, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/242529-minor-tweak-on-csv-parser-needed/#findComment-1245636 Share on other sites More sharing options...
baccarak Posted July 21, 2011 Author Share Posted July 21, 2011 brilliant - I owe you one email me - Nodral if you need any graphic or web template work done Quote Link to comment https://forums.phpfreaks.com/topic/242529-minor-tweak-on-csv-parser-needed/#findComment-1245665 Share on other sites More sharing options...
baccarak Posted July 21, 2011 Author Share Posted July 21, 2011 outputs in right order now but lost formatting and echoes seperator tx anyway Quote Link to comment https://forums.phpfreaks.com/topic/242529-minor-tweak-on-csv-parser-needed/#findComment-1245671 Share on other sites More sharing options...
Nodral Posted July 21, 2011 Share Posted July 21, 2011 Change the echo back to a print if you like, I just used echo for testing on a basic level Quote Link to comment https://forums.phpfreaks.com/topic/242529-minor-tweak-on-csv-parser-needed/#findComment-1245672 Share on other sites More sharing options...
baccarak Posted July 21, 2011 Author Share Posted July 21, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/242529-minor-tweak-on-csv-parser-needed/#findComment-1245679 Share on other sites More sharing options...
Nodral Posted July 21, 2011 Share Posted July 21, 2011 Someone else may want to jump in on this now, I'm not that hot on the echo vs print situation Quote Link to comment https://forums.phpfreaks.com/topic/242529-minor-tweak-on-csv-parser-needed/#findComment-1245694 Share on other sites More sharing options...
baccarak Posted July 21, 2011 Author Share Posted July 21, 2011 thanks for your help anyways Quote Link to comment https://forums.phpfreaks.com/topic/242529-minor-tweak-on-csv-parser-needed/#findComment-1245700 Share on other sites More sharing options...
baccarak Posted July 21, 2011 Author Share Posted July 21, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/242529-minor-tweak-on-csv-parser-needed/#findComment-1245716 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.