Jump to content

Ambitious while loop


JasonGreen

Recommended Posts

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

It would make more sense to only store the data, and not store PHP in the file, but you could preform eval() on the source of the file then parse the variable like:

 

$split = explode("\n", $serialpurchase);
foreach($split as $part)
{
     $info = explode(' ', $part); // $info[0] = serial, $info[1] = month purchased, etc..
}

Link to comment
Share on other sites

Ahh I see, in that case it shouldn't be too difficult. Something like this suit?

 

<?php
include 'pcinfo.php';

$lines = explode("\r\n", $serialpurchase);

foreach($lines as $line) {
   // do something with the line

   // if you need to then 
   $items = explode(" ", $line);
   foreach($items as $item) {
      // do something with item
   }
}
?>

Link to comment
Share on other sites

To Crayon - Because I'm not the only one who will have access to the file and the idea is to have many files that can be used in the future and manipulated simply.

 

And thanks, explode worked wonderfully. Now I'm just trying to figure out how to make it look nicer. All the numbers are so close together it will be difficult for others tell whats what.

Link to comment
Share on other sites

Put them in a table, then you can style it how you like with CSS.

 

<php
include 'pcinfo.php';

$lines = explode("\r\n", $serialpurchase);

echo '<table>';
echo '<tr>';
echo '<td>header 1</td>';
echo '<td>header 2</td>';
echo '<td>header 3</td>';
echo '<td>header 4</td>';
echo '<td>header 5</td>';
echo '</tr>';

foreach($lines as $line) {
   echo '<tr>';

   // if you need to then 
   $items = explode(" ", $line);
   foreach($items as $item) {
      echo '<td>' . $item . '</td>';
   }

   echo '</tr>';
}
echo '<table>';
?>

Link to comment
Share on other sites

If you wish to format each column differently (ie add in $ sign etc.) just replace the inner loop with a more specificy pieces of code

 

<?php
// this
foreach($items as $item) {
   echo '<td>' . $item . '</td>';
}
// with this
echo  '<td>' . $line[0] . '</td>';
echo  '<td>' . $line[1] . '</td>';
echo  '<td>' . $line[2] . '</td>';
echo  '<td>' . $line[3] . '</td>';
echo  '<td> $' . $line[4] . '</td>';
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.