Solved

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

Posted on 2004-04-30
10
336 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hosting images 4 31
write screen output to text file 21 47
000webhost.com default error log 1 28
Can't Setup WAMP. Keep Being Told I've Got the Wrong Directory... 3 20
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

832 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