JasonGreen Posted October 3, 2009 Share Posted October 3, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/176419-ambitious-while-loop/ Share on other sites More sharing options...
.josh Posted October 3, 2009 Share Posted October 3, 2009 so is this pcinfo.php a regular text file with space separated info for each line? You can use file to put all of the lines into an array and use explode with space as delimiter to separate each value from each line. Or you can use fopen and fread to read the file one line at a time. Quote Link to comment https://forums.phpfreaks.com/topic/176419-ambitious-while-loop/#findComment-929886 Share on other sites More sharing options...
JasonGreen Posted October 3, 2009 Author Share Posted October 3, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/176419-ambitious-while-loop/#findComment-929940 Share on other sites More sharing options...
cags Posted October 3, 2009 Share Posted October 3, 2009 What do you mean it's in PHP? It would probably help if you could display a small sample of the file you wish to read. Quote Link to comment https://forums.phpfreaks.com/topic/176419-ambitious-while-loop/#findComment-929947 Share on other sites More sharing options...
JasonGreen Posted October 4, 2009 Author Share Posted October 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176419-ambitious-while-loop/#findComment-930210 Share on other sites More sharing options...
Alex Posted October 4, 2009 Share Posted October 4, 2009 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.. } Quote Link to comment https://forums.phpfreaks.com/topic/176419-ambitious-while-loop/#findComment-930213 Share on other sites More sharing options...
cags Posted October 4, 2009 Share Posted October 4, 2009 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 } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/176419-ambitious-while-loop/#findComment-930216 Share on other sites More sharing options...
.josh Posted October 4, 2009 Share Posted October 4, 2009 my question is if you have access to that php file and it's hardcoded in a var like that...then why not just change how it's hardcoded in the first place? Quote Link to comment https://forums.phpfreaks.com/topic/176419-ambitious-while-loop/#findComment-930229 Share on other sites More sharing options...
JasonGreen Posted October 4, 2009 Author Share Posted October 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176419-ambitious-while-loop/#findComment-930290 Share on other sites More sharing options...
cags Posted October 4, 2009 Share Posted October 4, 2009 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/176419-ambitious-while-loop/#findComment-930297 Share on other sites More sharing options...
JasonGreen Posted October 4, 2009 Author Share Posted October 4, 2009 Thanks cags! Using this table can I still assign the original cost a dollar sign with out assigning all numbers a dollar sign or do I have to start from scratch and assign unique variables? Quote Link to comment https://forums.phpfreaks.com/topic/176419-ambitious-while-loop/#findComment-930301 Share on other sites More sharing options...
cags Posted October 5, 2009 Share Posted October 5, 2009 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/176419-ambitious-while-loop/#findComment-930615 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.