Page 1 of 1

Ambitious while loop

Posted: Sat Oct 03, 2009 4:17 pm
by JasonGreen
I have a php file with lines of data.

In order they are 1)pc serial number, 2)month purchased, 3)day of month purchased, 4)number of days serviced(taken away from users), and 4)original cost

each of these values are separated by a space

Is it possible to include 'pcinfo.php' into another php file and use strtok (I'm sure with rtrim) to retrieve all of the data one line at a time (or 4 values at a time) using a While Loop instead of print for each line?

I've been experimenting for days, but am getting very little. Thanks

Re: Ambitious while loop

Posted: Sat Oct 03, 2009 5:11 pm
by jackpf
You could look into file() and explode().

Re: Ambitious while loop

Posted: Sat Oct 03, 2009 5:57 pm
by JasonGreen
Thanks for the response. It doesn't seems like an array would be optimal, unless I'm doing it wrong. This original php file of data is in php, not text. Which is why I was attempting to use strtok, my goal is to use as little code as possible to display the data.

Thanks

Re: Ambitious while loop

Posted: Sun Oct 04, 2009 2:28 pm
by JasonGreen
Sorry for the confusion. I understand and yo make a valid point. Let me show you a bit of what the file looks like

<?php
$serialpurchase =
"GXC1233 5 9 19 1124.95
GXB1F65 3 7 5 795.13
GXF1977 9 4 2 1229.
FXG4986 9 17 5 497.35
etc.


this is why I'd like an easy way to tug it out. Thanks again.

Re: Ambitious while loop

Posted: Sun Oct 04, 2009 2:52 pm
by jackpf
Like this:

Code: Select all

<?php
foreach(file('test.php') as $line)
{
    if(preg_match('/[^A-Za-z0-9\s\.]+/', $line) || substr_count($line, ' ') != 4)
        continue;
    
    list(
    $serial_number,
    $month_purchased,
    $day_purchased,
    $days_serviced,
    $original_cost
    ) = explode(' ', $line);
    
    //do whatever with the variables
}
?>
Although I think maybe you should be using a database for this..