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
Solved

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

Posted on 2004-04-30
10
337 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wordpress Security 29 48
Test if unique ID is in log file 5 21
PHP Mail error 3 26
Extracting content from meta tag PHP MYHTML DOM 3 20
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

839 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