Ambitious while loop

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!

Moderator: General Moderators

Post Reply
JasonGreen
Forum Newbie
Posts: 7
Joined: Sun Sep 13, 2009 10:22 am

Ambitious while loop

Post 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
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Ambitious while loop

Post by jackpf »

You could look into file() and explode().
JasonGreen
Forum Newbie
Posts: 7
Joined: Sun Sep 13, 2009 10:22 am

Re: Ambitious while loop

Post 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
JasonGreen
Forum Newbie
Posts: 7
Joined: Sun Sep 13, 2009 10:22 am

Re: Ambitious while loop

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Ambitious while loop

Post 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..
Post Reply