Solved

Read file lines and assign them to variables

Posted on 2011-03-10
9
395 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
[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
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
Independent Software Vendors: 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: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

Title # Comments Views Activity
embadded search engine in website 4 44
e commerce steps shown instead of self testing 2 50
PHP processing webform 25 40
How do I add date to text file name 15 24
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

740 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