homepage Welcome to WebmasterWorld Guest from 54.237.213.31
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Reversing display
how to reverse the order of display from text file
wheelie34

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3911659 posted 8:40 am on May 12, 2009 (gmt 0)

Hi

I am reading the last 10 lines of a .txt file and printing them out to screen, I would like the order to be reversed so the last line prints at the top and the others below it, here's what I have

$openfile = fopen ( "data.txt", "a" );

$lines = file( 'data.txt' );

$count = count($lines);
echo '<pre>';
for($i=$count - 10; $i<$count; $i++){
echo $lines[$i];
}
echo '</pre>';

The above does work, I just need to reverse the display order, whats the best way?

 

wheelie34

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3911659 posted 9:11 am on May 12, 2009 (gmt 0)

This seems to work

$newlines = array_reverse($lines);

is that the best method?

edit: problem now is its stopping at 10 lines and not showing any new lines, any ideas? heres what I have

$lines = file('data.txt');

$newlines = array_reverse($lines);

$count = count($newlines);
echo '<pre>';
for($i=$count - 10; $i<$count; $i++){
echo $newlines[$i];
}
echo '</pre>';

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 3911659 posted 12:31 pm on May 12, 2009 (gmt 0)

I don't think you necessarily need to reverse the array, since for() loops can go backwards if you wish...

$lines = file('data.txt'); 
$max = count($lines)-1;
for($i=$max; $i>$max-10; $i--) {
echo $lines[$i].'<br>';
}

wheelie34

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3911659 posted 12:54 pm on May 12, 2009 (gmt 0)

perfect, thanks penders

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 3911659 posted 2:00 pm on May 12, 2009 (gmt 0)

You're welcome. Just a thought... that code does assume that there are at least 10 lines in your data file. If there are less then you'll get errors/warnings as your array index becomes negative - should really check for that.

is that the best method?

If the data file is massive then probably not, as the entire file is read into memory. In that case you would perhaps need to look at fopen(), fread(), fseek() etc. to manually read backwards from the end of the file? And only read into memory what is reqd.

wheelie34

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3911659 posted 2:24 pm on May 12, 2009 (gmt 0)

Good point, here's what happens in a nutshell.

if (! $_POST['number']){
unlink ("data.txt");#delete file if it exists
}
else
{
$input = $_POST['number'];

$openfile = fopen ( "data.txt", "a" );
fwrite ( $openfile, "$input\n" );

$lines = file('data.txt');
$max = count($lines)-1;
echo '<pre>';
for($i=$max; $i>$max-10; $i--) {
echo $lines[$i];
}
echo '</pre>';
}
The idea is, when the page first loads it creates the file data.txt then each form submission adds a line, so yes the file is empty to start with (I get no errors) and lists the entry in the reverse order, the data is only 2 chars per line, 01 24 12 etc for the duration of the work I only need to see and use the last 10 entries, I will be needing to work with those last 10 numbers by comparing them with each other for example to see if there are any the same within the last 10, is there a better way of doing it, as you can see I am not a php pro, I struggle through ;)

wheelie34

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3911659 posted 5:24 pm on May 12, 2009 (gmt 0)

For us guys n girls that dont do php on a daily or weekly basis we often can think of what we need but lack the experience to actually just sit down and do it, the things I find difficult are knowing what to use where, would it be possible if someone could write out the pseudo code (I think thats what I need) for what I am trying to accomplish, I appreciate members also have lives but it would save a lot of questions here.

This is what I have and am trying to do.

##############
include (comparison_data.php)
which contains the following data, theres 25 arrays in total

$products = array (

array (
"number" => "40",
"colour" => "violet",
"shape" => "triangle",
),
array (
"number" => "91",
"colour" => "blue",
"shape" => "round",
),
array (
"number" => "72",
"colour" => "blue",
"shape" => "square",
)
)

as you can see, 72 and 91 share the same colour 'blue'

I have a page that has a drop down list of all the numbers, in my first post I wanted them listed and printed to the page as the submit button is hit thus entering a new number to the text file, this works fine but I also want to check against the array in comparison_data.php (above) for numbers entered after each other that match and print a note to the page after 2 or more matches like so

"Blue 2 time/s"

and if another blue was found straight away the note would change to 3 etc etc

So in a nutshell, as I enter the data it will be checking "comparison_data.php" on every page load and giving a running commentary. Hopefully someone would be kind enough to just show me what php functions to use where and in what order, thanks very much in advance.

wheelie34

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3911659 posted 9:24 am on May 13, 2009 (gmt 0)

ok no replies so, I think I need to send the array of 10 lines to a function to process it but I cant gat the data into the function, I have tried this

$thelines = array();
$checklines = file('data.txt');
$max = count($checklines)-1;
for($i=$max; $i>$max-10; $i--) {
array_push ($thelines, $checklines[$i]);
}
#print_r ($thelines); this shows the data I want
print compare($thelines);

print_r has the data in it that I need but when I try to send it to the function I get the 'Call to undefined function compare()' warning, at the moment function compare() looks like

function compare($thelines){
$result = sizeof($thelines);
return $result;
}

How do I pass $thelines array to the function?

EDIT: why is it when you post for help you find the problem, I hadn't included the function file to my script, this now shows me a value of 10 which is correct, sorry for wasting your time reading this

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 3911659 posted 11:35 am on May 13, 2009 (gmt 0)

Hi wheelie34... in reply to your post above (go back 3)...
so yes the file is empty to start with (I get no errors) and lists the entry in the reverse order

You don't see any errors, because "Notice" messages are not set to display by default. Notice messages aren't necessarily errors, but highlight potential problems with the code that could result in issues later (if not currently). ie. in this case you are referring to array indexes which don't exist (NULL is returned). If you are simply displaying these values then it may not be a problem, but if you want to use these values to lookup something or use in some kind of calculation it could be. Best avoided in any case.

To enable more verbose error reporting, place this at the top of your code:
ini_set('display_errors','On'); // Probably already On
error_reporting(E_ALL);

To correct this issue with accessing 'out of range' indices you could do:
for($i=$max; $i>$max-10; $i--) { 
$line = isset($lines[$i]) ? $lines[$i] : null; // "ternary conditional operator"
if (is_null($line)) {
break;
}
echo $line;
}

Also, this is not necessarily secure:
if (!$_POST['number']) { 
unlink ("data.txt");
}

If $_POST['number'] evaluates to false then your file will be deleted (eg. '0' evaluates to false in PHP)

It may be better to:
if (!isset($_POST['number'])) {

But if may be better to use a session variable instead of a file. If more than 1 person are accessing your page then they are all accessing/writing to the same file - problem.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved