PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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.
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?
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.
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.
<?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.
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.
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?