Solved

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

Posted on 2004-04-30
10
339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

738 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