?
Solved

batch array text file contents group in ten lines within a loop

Posted on 2004-04-30
10
Medium Priority
?
346 Views
Last Modified: 2008-03-17
hi there

I need to take a text file insert that into an array then display the contents ten lines at a time for each loop

best regards

0
Comment
Question by:playstat
  • 5
  • 4
10 Comments
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 10964666
file -- Reads entire file into an array
http://www.php.net/manual/en/function.file.php

$yourArray = file("yourfilename");
for ($i = 0; $i < count($yourArray); $i++)
{
    for ($j = 0; $j < 10; $j++)
    {
        echo $yourArray[$i]
    }
    // do whatever you plan to do in between sets of ten lines
}
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 10964670
That's not right.  Let's try that again.

$yourArray = file("yourfilename");
for ($i = 0; $i < count($yourArray); $i++)
{
    echo $yourArray[$i]
    if ($i % 10 == 9)
    {
        // do whatever you plan to do in between sets of ten lines
    }
}
0
 
LVL 10

Expert Comment

by:eeBlueShadow
ID: 10964878
The top version of those it probably better programming practice, but you'd need to replace the middle line with
        echo $yourArray[(10*$i) + $j];

The reason I say this is better is that it is a lot easier to see that things are happening in blocks of 10 if you have a loop period 10 somewhere in your code. Trust me, if you come back to this code in a couple of months time, it'll be easier to remember.

_Blue
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:playstat
ID: 10964971
i have duplicates in the text file how do i get rid of all matching words before passing to the array
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 10964985
Are the lines exactly identical?  Then it would be easier to use array_unique.
http://www.php.net/manual/en/function.array-unique.php

Otherwise, you'll have to do some text parsing.
0
 

Author Comment

by:playstat
ID: 10965003
i need to strr_replace that using %data%

How would i do this

$data=str_replace(%data%,  ,   );
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 10965022
What?  Could you elaborate a little on that last question?
0
 

Author Comment

by:playstat
ID: 10965040
ob_start();
for ($k = 0; $k < count($content); $k++)
{
    for ($j = 0; $j < 10; $j++)
    {
        echo $content[(10*$k) + $j];
    }

}
$link=ob_get_contents();
ob_end_clean();

  $indexheader=str_replace('%TEXT%',$link,$indexheader);
0
 

Author Comment

by:playstat
ID: 10965080
i have compiled this as is that above is within a loop 2 which creates my pages from my text file info. But for somereason it only displays the first ten lines of text frfom the file for all my pages with the able codee. It just repeats them over and over. why does it not move onto the next ten and so forth

Does this code need to be outside a loop or is it the section above thats wrong

0
 
LVL 33

Accepted Solution

by:
snoyes_jw earned 1500 total points
ID: 10979448
I don't recommend using that style of loops.  eeBlueShadow may offer a different opinion, but then he might also work out the loop limiters for $k, which should not go to count($content), but to count($content)/10 (+/- 1), and then $j has to take into account what to do when you don't have an exact multiple of 10 records to display.

If you do it like this instead, you don't have to worry about that.

$yourArray = file("yourfilename");
for ($i = 0; $i < count($yourArray); $i++)
{
    echo $yourArray[$i]
    if ($i % 10 == 9)
    {
        // do whatever you plan to do in between sets of ten lines
    }
}
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question