Page 1 of 1

How to loop using strtok

Posted: Sat Jan 21, 2012 10:26 pm
by BBoyd
I've been trying to figure out how I can take a file that has about 100 lines and loop it to create a variable for each line. For now I did it the hard and long way by using the following code.

Code: Select all

    include 'data.php';
    $var01 = rtrim(strtok ($data, "\n"));
    $var02 = rtrim(strtok ("\n"));
    $var03 = rtrim(strtok ("\n"));

and on, and on...
Below is the code I've been working with and after too much time with no results, I put it on hold. Since I'm just learning PHP, I'm trying to crawl before walking and then eventually running. The code below would be crawling by using the variables to fill in a complete HTML table. The running part would be to loop the structure of the table as well to save on typing, but I'm not there yet.

Can someone help with giving me a little guidance on this problem?

Code: Select all

      $token = rtrim(strtok($data,"\n"));
      $n=1;
      while($token){
      	$token;
      	$n++;
      }

Re: How to loop using strtok

Posted: Sat Jan 21, 2012 10:46 pm
by Celauran
BBoyd wrote:I've been trying to figure out how I can take a file that has about 100 lines and loop it to create a variable for each line.
Why not use an array?

Code: Select all

$array = array();
$handle = fopen($filename, 'r');
while (!feof($handle))
{
    $array[] = fgets($handle);
}
fclose($handle);

Re: How to loop using strtok

Posted: Sun Jan 22, 2012 6:50 am
by BBoyd
Celauran wrote:Why not use an array?
An array might be the best choice for this problem, but it's about learning in steps. I'm about 3 weeks ahead in the class I'm taking and I'm now on the chapter about loops so that is what I'm trying to learn. The chapter after loops covers arrays and I don't want to skip ahead and miss out learning about loops. Although I've learned about loops in C++ and in Excel, it still confuses me sometimes. Maybe it could have been spending a solid 11 hours yesterday studying and just got a little burning out.

Anyway, thanks for the tip about arrays and will experiment with the code you shared, but I still have to learn how to code loops in PHP.

Brian

Re: How to loop using strtok

Posted: Sun Jan 22, 2012 6:56 am
by Celauran
The array replaces having a bunch of variables, it doesn't replace the need for a loop. You'll notice I've used a while loop to read the lines from the file.

Re: How to loop using strtok

Posted: Sun Jan 22, 2012 7:17 am
by BBoyd
I can see your point about it still using the loop so it is worth using. However, the the file that has the data starts out and end like...

Code: Select all

<?php
$data =
"line01
line02
line03
so on, and so on
line99";
?>
Each time I try to run the code that you shared, the software stalls and the title bar message states (Not Responding). This happens in both PHP Coder and PHP Editor. Would this be because of the extra lines in the file that is not the data? Also I cannot change the file for this assignment.

Since the assignment was correct, I submitted it last night before going to bed, but I still want to learn how to code this better.

Re: How to loop using strtok

Posted: Sun Jan 22, 2012 7:26 am
by BBoyd
Eventually I would like to do the following with this problem where it takes to data from the file and creates a table around it. The problem requires a table that has 5 columns per row, however, the data file to import has 8 rows of data for each record with the last 3 rows not being used at all so those rows in the data file need to be skipped.

http://www.devnetwork.net/viewtopic.php ... 9e#p651097

This is a little ahead right now and many not even be covered in this class, but it is something I want to eventually learn.

Re: How to loop using strtok

Posted: Sun Jan 22, 2012 7:34 am
by Celauran
BBoyd wrote:However, the the file that has the data starts out and end like...

Code: Select all

<?php
$data =
"line01
line02
line03
so on, and so on
line99";
?>
Yes, if what you're trying to parse is already a variable, then fgets wouldn't be the right solution.

Code: Select all

$array = array();
$array[] = strtok($data, "\n");
while ($token = strtok("\n"))
{
    $array[] = $token;
}

Re: How to loop using strtok

Posted: Sun Jan 22, 2012 8:06 am
by BBoyd
This code doesn't stall the software so that is a good start. Now I'm confused how to get the data in the table. Maybe I'm looking at this the wrong way. I know looking at code posted by other people in the forum, the table can be generated within the code, but like I said before, that might be a little further down the road for me.

With the array you wrote, can it work with a table like below?

Code: Select all

        <tr>
            <td width="200px"><?php echo($name01);?></td>
            <td width="100px" align="right"><?php echo($name02);?></td>
            <td width="100px" align="right"><?php echo($name03);?></td>
            <td width="100px" align="right">$<?php echo number_format($name04, 2); ?></td>
            <td width="100px" align="right"><?php echo($name05);?></td>
            <td width="100px" align="right">$<?php echo number_format($name04 * $name05, 2); ?></td>
        </tr>

Re: How to loop using strtok

Posted: Sun Jan 22, 2012 8:12 am
by Celauran
BBoyd wrote:With the array you wrote, can it work with a table like below?
Absolutely. Replace the various variable names with their corresponding array keys.

Code: Select all

        <tr>
            <td width="200px"><?php echo($array[0]);?></td>
            <td width="100px" align="right"><?php echo($array[1]);?></td>
            <td width="100px" align="right"><?php echo($array[2]);?></td>
            <td width="100px" align="right">$<?php echo number_format($array[3], 2); ?></td>
            <td width="100px" align="right"><?php echo($array[4]);?></td>
            <td width="100px" align="right">$<?php echo number_format($array[3] * $array[4], 2); ?></td>
        </tr>
 

Re: How to loop using strtok

Posted: Sun Jan 22, 2012 8:39 am
by BBoyd
This works great except that I forgot the keys will start with zero instead of one. :) I can fix that after breakfast.

Thank you so much Celauran. Once I start reading the chapter on arrays, this will help make it more understandable.

BTW... does this forum have anything to add to reputation or kudos?