PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old August 8th, 2002, 02:06 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
limited list from txt

hi,
I am just a beginner in PHP. I am using the script from one of the tutorials to get my news-items from a text file.

==================
<?PHP
$data = file('news.txt');
$data = array_reverse($data);
foreach($data as $element) {
$element = trim($element);
$pieces = explode("|", $element);

echo $pieces[0] . ": " . $pieces[1] . "<BR>";
}
?>
==================

How can I limit the list to the last 5 items???

Paul

Reply With Quote
  #2  
Old August 8th, 2002, 02:55 PM
greggory greggory is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Reims, France
Posts: 82 greggory User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: limited list from txt

Just count them

php Code:
Original - php Code
  1.  
  2. <?PHP
  3. $data = file('news.txt');
  4. $data = array_reverse($data);
  5. $i = 0;
  6. while ( $i < 5 ) {
  7. $element = $data[$i++];
  8. $element = trim($element);
  9. $pieces = explode("|", $element);
  10.  
  11. echo $pieces[0] . ": " . $pieces[1] . "<BR>";
  12. }
  13. ?> 

Reply With Quote
  #3  
Old August 9th, 2002, 08:10 AM
siteworkspro.com siteworkspro.com is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Sydney, Australia
Posts: 92 siteworkspro.com User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via ICQ to siteworkspro.com Send a message via AIM to siteworkspro.com
RE: limited list from txt

well ya learn something new every day

Reply With Quote
  #4  
Old August 9th, 2002, 10:20 AM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: limited list from txt

ShaneO : I had been didactic

For myself, I'll probably wrote something like
php Code:
Original - php Code
  1.  
  2. <?PHP
  3. $data = array_reverse( file('news.txt') );
  4. $i = 0;
  5. while ( $i < 5 ) {
  6.   $pieces = explode("|", $data[$i++]);
  7.   echo $pieces[0] . ": " . $pieces[1] . "<BR>";
  8. }
  9. ?> 


or

php Code:
Original - php Code
  1.  
  2. <?PHP
  3. $data = array_reverse( file('news.txt') );
  4. $i = 0;
  5. while ( $i < 5 ) {
  6.   echo preg_replace( Array( "/|/", "/$/" ), Array( ": ", "<br>" ), $data[$i++] );
  7. }
  8. ?> 


May be on one line ;)

Reply With Quote
  #5  
Old August 9th, 2002, 10:20 PM
zombie zombie is offline
Codewalkers Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2007
Location: serbia
Posts: 1,876 zombie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
RE: limited list from txt

or simply use array_slice() - that's what it is there for ;)

php Code:
Original - php Code
  1.  
  2. ...
  3. $data5=array_slice($data, 0, 5);
  4. foreach($data5 as $element) {
  5. ...


if u want to understand this better, go to http://php.net/array_slice

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > limited list from txt


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT