Solved

Read file lines and assign them to variables

Posted on 2011-03-10
9
390 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
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 …

786 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