Computer HardwareXbox GamesGameCubePlaystation 2PSOnePC/Windows GamesGameboy AdvanceDreamcastNintendo 64Gameboy ColorNintendo DSSony PSPXbox 360Nintendo Wii GamesPS3 Games

Neoseeker Forums » Programming and Design » Web Coding » Requesting Help [PHP]

Moderated by: Kuldahar,  Artificer
REPLY TO THIS THREAD   START NEW THREAD
Options: Print   subscribe   remove   send to friend   PM this thread to a friendNeoPM
subscribe to thread Topic: Requesting Help [PHP]
MrGrimm
I'm too cool to have a title-- crap.
s-e-e-k-e-r



MrGrimm's profileNeoPM MrGrimm
total posts: 1747
since: Mar 2008
Nov 17, 09 at 2:43am
Requesting Help [PHP]

Hey guys, I've been having a tough time figuring this out. Right now, I have a .txt file with article entries inside it. I want a script to read each entry, and echo it to the browser, but I want it to stop echoing to the browser when it echoes 10 entries. Here's what I have so far:

php code


<html>
<body>

<?php

$data = file_get_contents('data.txt');
$entries = explode('|', $data);
$x = 0;

foreach($entries as $entry)
{
   $x++;
}

if($x > 11)
{
   echo "<br>";
   echo implode($entries);
}

?>

</body>
</html>
 



The script I made obviously doesn't work. It continues to echo article entries to the browser even after 10 entries have been echoed. Can anyone help me out? It'd be appreciated.
quote   quick quote   edit   quick edit   del  searchposts in thread  report
bobbonew
Nov 17, 09 at 5:32am
re: Requesting Help [PHP]

Here ya go buddy. Its a little more than you asked for as it has built in error checking so you don't try to loop stuff if nothing exists. Its untested but looks pretty clean.

php code


<?php

$data = file_get_contents('data.txt');
$entries = explode('|', $data);

/*COUNT HOW MANY ENTRIES THERE ARE*/
$total = count($entries);

/*SET THE LIMIT AS 10 IF THE TOTAL ITEMS IS MORE THAN 10, OR JUST AS MANY AS YOU HAVE IF NOT*/
if($total > 10) {
    $limit = 10;
} else {
    $limit = $total;
}

/*ONLY PERFORM THE LOOP IF THERE ACTUALLY ARE ENTRIES*/
if($limit > 0) {

    /*OUTPUT THE ENTRY DATA BY KEY, PRETTY MUCH ALWAYS STARTING AT ZERO*/
    $key = 0;
    while($key < $total) {
        print $entries[$key];
        $key++;
    }

}
 

quote   quick quote   edit   quick edit   del  searchposts in thread  report
MrGrimm
I'm too cool to have a title-- crap.
s-e-e-k-e-r



MrGrimm's profileNeoPM MrGrimm
total posts: 1747
since: Mar 2008
Nov 17, 09 at 7:07am
re: Requesting Help [PHP]

Thanks Bobbo!

It didn't work at first, but I changed line "while($key < $total)" to "while($key < $limit)" and it worked.

quote   quick quote   edit   quick edit   del  searchposts in thread  report
Dragooon
not-such-a-newbie

Dragooon's profile
total posts: 49
since: Oct 2009
Nov 17, 09 at 7:09am
re: Requesting Help [PHP]

Here's another :

php code

<?php
$data = file_get_contents('data.txt');
$entries = explode('|', $data);

for ($i = 0; $i < count($entries) || $i < 10; $i++)
    echo $entries[$i];

?> 
quote   quick quote   edit   quick edit   del  searchposts in thread  report
asdf
Neo-newbie



asdf's profile
since: May 2001
Nov 17, 09 at 8:00am
re: Requesting Help [PHP]

Peer pressure

php code


<?php

$data = array_slice(explode('|', file_get_contents('data.txt')), 0, 10);

foreach ($data as $item){
    echo $item . "<br>";
} 

quote   quick quote   edit   quick edit   del  searchposts in thread  report
bobbonew
Nov 17, 09 at 3:37pm
re: Requesting Help [PHP]

quote MrGrimm
Thanks Bobbo!

It didn't work at first, but I changed line "while($key < $total)" to "while($key < $limit)" and it worked.

Woops yepp that would do it. Was a very long day for me yesterday


-------------------
quote   quick quote   edit   quick edit   del  searchposts in thread  report
[All dates in (PT) time]Threads List   « Next Newest   Next Oldest »
REPLY TO THIS THREAD   START NEW THREAD


search:
Neoseeker Forums » Programming and Design » Web Coding » Requesting Help [PHP]



Jump to another forum:

Powered by neoforums v0.9.7g (equilibrium)
Copyright Neo Era Media, Inc. 1999-2009

neoseeker forum community
Neoseeker.com   |   Forum Rules   |   Forum FAQ   |   Neoseeker Terms of Use   |   Supermods On Duty [ server id: ascension ··· elapsed: 0.0741758347]
Affiliated sites:   GameGrep - Football Manager Wiki - Halo Wiki - MGS Wiki - GTA Wiki - Smackdown Wiki - Zelda Wiki - PS2seeker - Xbox seeker - DEVPEN - GFXcess