PDA

View Full Version : displaying a text file


ivy
06-03-2005, 10:19 PM
I have a text file that contains:


Cadbury's|Boost|cadbury_boost|
Cadbury's|Bournville|cadbury_bournville|
Cadbury's|Chomp|cadbury_chomp|
Cadbury's|Crunchie|cadbury_crunchie|
Cadbury's|Curly Wurly|cadbury_curlywurly|
Cadbury's|Dairy Milk|cadbury_dairymilk|

.... and it goes on!


I would like to display the lines from the text file, but only 9 lines at a time, then add the prev-next links. I am using the following (a variant on some code i previously used from a tutorial):


<?php

// if a page isn't defined, we're on page one
if($page <= 0)
{
$page = 1;
}

// create an array of data
$file = file( 'products_text.txt' ) ;
$counter = "";

// how many lines of data to display
$display = 9;

// where to start depending on what page we're viewing
$start = ($page * $display) - $display;

// the actual news we're going to print
$news = array_slice($file, $start, $display);

// using the next bit of code, the various form entries can now be called upon

foreach($news as $line ){
list( $prodmanuf, $proddescr, $prodimage ) = explode('|', $line) ;


print("$prodmanuf, $proddescr, $prodimage<br>\n"); // this will be changed to a table layout
}
// printing the data
foreach($news as $key=>$value)
{
}
// The next bit is used for the next previous page coding

$newpage = $page + 1;
$prevpage = $page - 1;

$totalnews = count($file);


// This can be called to insert the previous and next links anywhere on the page

if($page > 1)
{
echo("<a href=\"http://www.website.com/thispage.php?page=$prevpage\">Previous listing </a> --");
}
else
{
}


if($totalnews > $page)
{
echo("-- <a href=\"http://www.website.com/thispage.php?page=$newpage\"> Next listing</a>");
}
else
{
}

?>


And it is not working properly!!!!

It displays 9 lines as I would like, but the next and previous do not function properly...... EVEN if I type ?page=3 after the URL, it still thinks it is on page 1!!! And the next link shows, links to ?page=2 but it displays page 1!!!

Very frustrating...

Any comments please woudl be most welcome!

marek_mar
06-03-2005, 11:02 PM
Well

// if a page isn't defined, we're on page one
if($page <= 0)
{
$page = 1;
}

How abou6t changing that to

if(isset($_REQUEST['page']) && is_numeric($_REQUEST['page']))
{
$page = $_REQUEST['page'];
}
else
{
$page = 1;
}

You may have register_globals off. Don't turn it on.

ivy
06-03-2005, 11:18 PM
Hi marek_mar

Thank you... But... That does not seem to be the problem (I have just tried after reading your suggestion).

It continues with the same problem as above!

It's driving me insane!

delinear
06-03-2005, 11:40 PM
Hm, I can't see anything wrong with the code. In fact, it works perfectly for me using both the original code (with register_globals) and marek's suggestion (without globals).

What happens if you specifcally set $page using $_GET? ie at the top of your code insert:
$page = (isset($_GET['page']) ? $_GET['page'] : '1');

Apart from that, all I can say really is to try and echo all your variables as you go through the script and see which one isn't being properly set, might help to narrow down the problem.

marek_mar
06-03-2005, 11:52 PM
Oddly it works for me too :) I have no idea why it shouldn't work and I don't see anyway in telling you how to amke it work as it does work. I printed the $file and $news and they showed the correct values. You could add a top limit so you can't select next page and get an empty page all the time. Ok you tryed to do that but it won't work that way.

$totalnews = ceil(count($file) / $display);

ivy
06-04-2005, 12:03 AM
Hi marek_mar and delinear

OK. I've sorted it! This is one that I shall not forget in a hurry -

I used an include on the page for the code! Thus, each time the page loaded, it was using the page number of the included page with the code (I guess).

I have now put the code onto the page itself, rather than including it and it works perfectly well!!!

I'm really sorry! Thank you very much for your time, both of you....

I was looking at the code for hours trying to correct any mistakes!

Thank you once again,

delinear
06-04-2005, 12:29 AM
Oh well, the important thing is that it works I guess :thumbsup: sometimes it's best not to question these things too closely :p

ivy
06-04-2005, 12:43 AM
sometimes it's best not to question these things too closely

Absolutely!!!!


One other thing, that I do not know how to accomplish...

The data that I am retrieving I would like to put into a table with three columns. WHat I need to know is how to determine whether the value of the returned line is divisible by 3, for example:



if ($line is divisible by 3)
{
echo("</td></tr>");
}
else
echo("</td>");
}



Could you help me with the "is divisdible by 3" bit please!

delinear
06-04-2005, 01:02 AM
The modulus operator (%) should help there. Basically it is used to tell you what the remainder is in any division - if there is no remainder then the value is divisible by the divisor, ie:

if($line % 3 == 0) {
echo 'No remainder so this value is divisible by 3!';
} else {
echo 'There is a remainder, this value is not perfectly divisible by 3.';
}

ivy
06-04-2005, 02:15 AM
Hi delinear

Thank you once again! I'll bear that in mind, but I have now decided to use the css float to layout the results.

It's reassuring to know that I am not the only one up at this hour of the night working!!!

That's enough for today though!

delinear
06-04-2005, 03:08 AM
Heh, happy to help. As for working at this ungodly hour, I seem to exist on zero sleep and it's either this or rubbish night time TV :p No contest, really.