Solved

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

Posted on 2004-04-30
10
334 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:playstat
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:playstat
Comment Utility
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
Comment Utility
What?  Could you elaborate a little on that last question?
0
 

Author Comment

by:playstat
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now