Solved

Read file lines and assign them to variables

Posted on 2011-03-10
9
392 Views
Last Modified: 2012-05-11
Hi Experts,

I wanna read from text file, the text file look like this:
test.txt:
----start file ----
1
2
3
4
---
1
6
2
8
---
9
8
2
4
----end file ----

As you have seen, the text file starts by four lines and then a separator (---). What I want is that, I want to read the text file from the first line to the first separator (---) and then assign each line ( there are four lines) and then print them like this:
from first line to the first separator:
for (first line to the first separator ) {
echo var[0];  //Var[0] = 1
echo var[1];  //Var[1] = 2
echo var[2];  //Var[2] = 3
echo var[3];  //Var[3] = 4
}
----------------
from after the first separator to the second separator:
for (after the first separator to the second separator ) {
echo var[0];  //Var[0] = 1
echo var[1];  //Var[1] = 6
echo var[2];  //Var[2] = 2
echo var[3];  //Var[3] = 8
}
----------------
from after the second separator to the third separator:
for (after the second separator to the third separator ) {
echo var[0];  //Var[0] = 9
echo var[1];  //Var[1] = 8
echo var[2];  //Var[2] = 2
echo var[3];  //Var[3] = 4
}

I am sure we can do one for loop for all the lines, can you help me with this?

Thanks in advance.
Regards
0
Comment
Question by:Sat80
  • 5
  • 4
9 Comments
 
LVL 4

Expert Comment

by:Doug Walton
ID: 35098187
Maybe something like this:

$fp = fopen("filepath", "r");
$numbers = array();
$i = 0;
while ($line = fgets(file))
{
  if($line != "---")
  {
    $numbers[$i] = $line;
    echo $line;
  }
  else //you could do something with the variables you just set in this else
    $i = 0;
  $i++;
}

Open in new window


You could take out the whole $numbers stuff if all you want to do is print (I put it in because it seemed like you wanted an array).
0
 

Author Comment

by:Sat80
ID: 35098319
Hi,
Thanks for help.
The variable (file) is undefined? do you mean the path of txt file?

Yes you are right, actually I will insert them to DB so when they are in array then I can insert each line to different column
Thanks
0
 
LVL 4

Expert Comment

by:Doug Walton
ID: 35098459
Oops sorry I meant $fp not file

$fp = fopen("filepath", "r");
//connect to db
$numbers = array();
$i = 0;
while ($line = fgets($fp))
{
  if($line != "---")
  {
    $numbers[$i] = $line;
    echo $line;
  }
  else //you could do something with the variables you just set in this else
  {
    //insert into....
    $i = 0;
  }
  $i++;
}
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 

Author Comment

by:Sat80
ID: 35098702
Hi,

The code is reading the lines and output them, but I wanna have something like this:

$numbers[0] = first line
$numbers[1] = second line
$numbers[2] = third line
$numbers[3] = fourth line

and then read the second value after the separator (---) and then again the same array with the four values until read the whole value? I know that there will be four lines and then the separator (---) in the whole file as I have shown in the question. Can you please help me with this?
Thx
0
 

Author Comment

by:Sat80
ID: 35098891
This should work , in theory, but it didn't work when try it, Can you please help me with it?


$fp = fopen("test.txt", "r");
$numbers = array();
$i = 0;
while ($line = fgets($fp))
{
  if($line != "---")
  {
    $numbers[$i] = $line;
    echo $line;

  }
  else //you could do something with the variables you just set in this else
  {
    //insert into....
   
      echo  $numbers[0];
      echo "<br>";
      echo  $numbers[1];
      echo "<br>";
      echo  $numbers[2];
      echo "<br>";
      echo  $numbers[3];
      echo "<br>";
      echo  $numbers[4];
      reset($numbers);
      $i = 0;
      echo "<br>---<br>";
  }
  $i++;
        
}
0
 
LVL 4

Expert Comment

by:Doug Walton
ID: 35099053
I think this will work... I moved the $i++ into the if statement.  It was skipping $numbers[0] each time after the first one.  Was anything else not working about it?
$fp = fopen("test.txt", "r");
$numbers = array();
$i = 0;
while ($line = fgets($fp))
{
  if($line != "---")
  {
    $numbers[$i] = $line;
    echo $line;
    $i++;
  }
  else //you could do something with the variables you just set in this else
  {
    //insert into....
   
      echo  $numbers[0];
      echo "<br>";
      echo  $numbers[1];
      echo "<br>";
      echo  $numbers[2];
      echo "<br>";
      echo  $numbers[3];
      echo "<br>";
      echo  $numbers[4];
      reset($numbers);
      $i = 0;
      echo "<br>---<br>";
  }   
}

Open in new window


Also if you're only taking in 4 numbers, you shouldn't need $numbers[4]
0
 

Author Comment

by:Sat80
ID: 35099100
Hi, Thanks for helping me, I guess there is something wrong with the array, as I have deleted
echo $line;
So it will only print the array number when the line is (---), but will find a blank page? what do you think? This is the code I am using:
<?PHP
$fp = fopen("test.txt", "r");
$numbers = array();
$i = 0;
while ($line = fgets($fp))
{
  if($line != "---")
  {
    $numbers[$i] = $line;

    $i++;
  }
  else //you could do something with the variables you just set in this else
  {
    //insert into....
   
      echo  $numbers[0];
      echo "<br>";
      echo  $numbers[1];
      echo "<br>";
      echo  $numbers[2];
      echo "<br>";
      echo  $numbers[3];
      echo "<br>";
      echo  $numbers[4];
	  
      reset($numbers);
      $i = 0;
      echo "<br>---<br>";
  }   
}
?>

Open in new window

0
 
LVL 4

Accepted Solution

by:
Doug Walton earned 500 total points
ID: 35099249
OK, this works for me:
<?php
$fp = fopen("test.txt", "r");
$numbers = array();
$i = 0;
while ($line = fgets($fp))
{
  $line = trim($line);
  if($line != "---")
  {
    $numbers[$i] = $line;
    $i++;
  }
  else
  {
   
      echo  $numbers[0];
      echo "<br>";
      echo  $numbers[1];
      echo "<br>";
      echo  $numbers[2];
      echo "<br>";
      echo  $numbers[3];
      echo "<br>";
	  
      reset($numbers);
      $i = 0;
      echo "<br>---<br>";
  }
}
echo  $numbers[0];
echo "<br>";
echo  $numbers[1];
echo "<br>";
echo  $numbers[2];
echo "<br>";
echo  $numbers[3];
echo "<br>";
?>

Open in new window


Hope that helps!
0
 

Author Comment

by:Sat80
ID: 35099276
Yes, its working now, thanks :)
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

829 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