# Solved: PHP, more help :D [pages again!]



## Xperience (Feb 13, 2007)

Howdy again 

This time, i've got a news system, where admins make posts. It uses flat file for database.

Ive coded it myself so any code you give me shouldnt be hard to implement  Anyway, i want to know, if just say there is 100 posts. Ouch thats a lot for one page, how do i break it up, say have 5 posts for each page.

Moreover, i'd like to have the code also generate the page numbers 

Thanks in advance.


----------



## MMJ (Oct 15, 2006)

How are the posts separated?


----------



## Xperience (Feb 13, 2007)

Basically it gets each line from a text file. Then for each line it explodes the post data (author, title, date, desc, post etc). Each bit of data is separated by a pipe ("|") and the explode looks a little like:


```
explode("|", $line[$n])
```
And then it just basically writes it to the page from there. If you want the code ill give it to you, might be easier, so yeh, here it is:


```
$mode = 0;
if ($mode == 0) { $opFile = $_SERVER['DOCUMENT_ROOT'] . "/admin/database/blog_data.txt"; }
if ($mode == 1) { $opFile = "blogfile1.txt"; }

$bp = fopen($opFile,"r") or die("Error Reading File");
  $data = fread($bp, filesize($opFile)+1);
fclose($bp);

$line = explode("\n", $data);
$i = count($line);

if ($data == "") { echo "[B]There are no entries[/B] to show at this present time
"; }

for ($n=0 ; $n < $i-1 ; $n++ ) {
  $blog = explode("|", $line[$n]);

  if ( $blog[5] == "" ) {

	$news->vars['desc'] = "";

  }

  else {

	$news->vars['desc'] = ",";

  }

  if (isset($blog[0]))
   {

	echo ("

		[IMG]http://xflive.techguy.org/sources/images/calendar.png[/IMG] " .$blog[1]."[B]" .$blog[2]."[/B]".$news->vars['desc']." " .$blog[5]."
		[B]Posted by:[/B] " .$blog[0]."

		" .$blog[3]."

		[B]Priority:[/B] " .$blog[4]."

	");

    }

}
```


----------



## MMJ (Oct 15, 2006)

Yeah, post as much code as you can please.


----------



## Xperience (Feb 13, 2007)

That is all the code that takes the data from the file and writes each post to the page. You probably didnt see it, since i edited the post, but check post #3 again, ive got the code there.


----------



## Xperience (Feb 13, 2007)

bump


----------



## MMJ (Oct 15, 2006)

Hi, I put this together real quick.

I really have no idea if it works.


```
<?php
$mode = 0;
if ($mode == 0) { $opFile = $_SERVER['DOCUMENT_ROOT'] . "/admin/database/blog_data.txt"; }
if ($mode == 1) { $opFile = "blogfile1.txt"; }

$bp = fopen($opFile,"r") or die("Error Reading File");
  $data = fread($bp, filesize($opFile)+1);
fclose($bp);

$line = explode("\n", $data);
$i = $_GET['page'] ? $_GET['page']*count($line) : count($line);

for ($a = 0; $a+5; $a < count($line))
{
	echo $a == $_GET['page'] ? "$a, " : "[URL]$a[/URL]";
}

if ($data == "") { echo "[B]There are no entries[/B] to show at this present time
"; }

for ($n=$i-1 ; $n < $i+5 ; $n++ ) {
  $blog = explode("|", $line[$n]);

  if ( $blog[5] == "" ) {

    $news->vars['desc'] = "";

  }

  else {

    $news->vars['desc'] = ",";

  }

  if (isset($blog[0]))
   {

    echo ("

        [IMG]http://xflive.techguy.org/sources/images/calendar.png[/IMG] " .$blog[1]."[B]" .$blog[2]."[/B]".$news->vars['desc']." " .$blog[5]."
        [B]Posted by:[/B] " .$blog[0]."

        " .$blog[3]."

        [B]Priority:[/B] " .$blog[4]."

    ");

    }

}
?>
```


----------



## Xperience (Feb 13, 2007)

eek :/ nope didnt work. just echoed a near infinite amount of zeroes :/


----------



## MMJ (Oct 15, 2006)

Sorry 

Replace
for ($a = 0; $a+5; $a < count($line))
with
for ($a = 0; $a < count($line); $a+5)


----------



## Xperience (Feb 13, 2007)

Hmm strange. Still doing the zero thing :/


----------



## MMJ (Oct 15, 2006)

Xperience said:


> Hmm strange. Still doing the zero thing :/


Is this on a site? Do you have a link?

EDIT:

Replace with this:

for ($a = 0; $a <= count($line); $a = $a+5)


----------



## Xperience (Feb 13, 2007)

ok the zero thing is gone but now it displays blank announcements :/


----------



## MMJ (Oct 15, 2006)

blank announcements? Got a link? You can pm me if its private.


----------



## Xperience (Feb 13, 2007)

oh hang on a sec :/ how does this work exactly? it only seems to generate the page numbers. ya i wanted that but i also wanted it to only display say 5 announcements per page. Can you do this too? Thanks, i wrote that in the orig post anyway


----------



## MMJ (Oct 15, 2006)

Xperience said:


> oh hang on a sec :/ how does this work exactly? it only seems to generate the page numbers. ya i wanted that but i also wanted it to only display say 5 announcements per page. Can you do this too? Thanks, i wrote that in the orig post anyway


Thats what it should be doing.  All the code looks right but since I don't have the link I can't figure out whats wrong.


----------



## Xperience (Feb 13, 2007)

Ok, it shows 6 per page but they are blank:

Here is a link:

http://imgday.com/index.php?do=news


----------



## MMJ (Oct 15, 2006)

Xperience said:


> Ok, it shows 6 per page but they are blank:
> 
> Here is a link:
> 
> http://imgday.com/index.php?do=news


What I see is three news items.


----------



## Xperience (Feb 13, 2007)

Ive kinda modified the code. I can get it to display 5 per page and also change pages etc. Now i've a new issue  How to list the page numbers, instead of just Next and previous.

Thanks for your inspiration with the previous codes to get it all working, but ill give you my new code and see if you can fiddle with that for me to get it listing page numbers too ?

Thanks again 


```
<?php
$mode = 0;
if ($mode == 0) { $opFile = $_SERVER['DOCUMENT_ROOT'] . "/admin/database/blog_data.txt"; }
if ($mode == 1) { $opFile = "blogfile1.txt"; }

$bp = fopen($opFile,"r") or die("Error Reading File");
  $data = fread($bp, filesize($opFile)+1);
fclose($bp);

$line = explode("\n", $data);
$i = count($line);

if ($data == "") { echo "<div class='simplebox' style='margin:0'><b>There are no entries</b> to show at this present time</div><br />"; }

$point_start = $_GET['st'] ? $_GET['st'] : 0 ;
$point_stop = 5;

echo "<span id='pagelinkt'></span><br /><br />";

for ($n=0 ; $n < $i-1 ; $n++ ) {
  $e = $n;

  $blog = explode("|", $line[$n]);

if ( $e >= $point_start ) {

  $e++;

  if ( $blog[5] == "" ) {

    $news->vars['desc'] = "";

  }

  else {  

    $news->vars['desc'] = ",";

  }

  if (isset($blog[0]))
   {

    echo ("

        <div class='borderwrap' style='border:1px solid #444'>
        <div class='maintitle' style='padding:6px'><span style='float:right'><img src='/sources/images/calendar.png'>&nbsp;" .$blog[1]."</span><b>" .$blog[2]."</b>".$news->vars['desc']."&nbsp;" .$blog[5]."</div>
        <div class='simplebox' style='border:0;width:auto;padding:4px;margin:0'><b>Posted by:</b>&nbsp;" .$blog[0]."</div>
        <div clas='simplebox' style='margin:0;padding:6px'>
        " .$blog[3]."
        </div>
        <div class='simplebox' style='border:0;width:auto;padding:4px;margin:0;text-align:right'><b>Priority:</b>&nbsp;" .$blog[4]."</div>
        </div><br />

    ");

    }

  $el = $e-6;

  if ( $e == $point_stop ) {

	break;

  }


 }

}


	if ( $el >= 0 ) {

		$_pagelink =  "<a href=/index.php?do=news>&laquo; Previous page</a>&nbsp;&nbsp;";

	}

	$j = $e-1;

	if ( ( $e > 1 ) && ( $_GET['st'] < $j ) ) {

		$_pagelink = "<a href=/index.php?do=news&st=".$e.">Next page &raquo;</a>";

	}

echo "<span id='pagelink'>".$_pagelink."</span>";

echo ("

	<script>
	document.getElementById('pagelinkt').innerHTML = document.getElementById('pagelink').innerHTML
	</script>

");

?>
```


----------



## MMJ (Oct 15, 2006)

Replace with this code:

```
<?php
$mode = 0;
if ($mode == 0) { $opFile = $_SERVER['DOCUMENT_ROOT'] . "/admin/database/blog_data.txt"; }
if ($mode == 1) { $opFile = "blogfile1.txt"; }

$bp = fopen($opFile,"r") or die("Error Reading File");
  $data = fread($bp, filesize($opFile)+1);
fclose($bp);

$line = explode("\n", $data);
$i = $_GET['page'] ? $_GET['page']*5 : 5;

for ($a = 0; $a <= count($line); $a = $a+5)
{
    echo $a == $_GET['page'] ? "$a, " : "[URL]$a[/URL]";
}

if ($data == "") { echo "[B]There are no entries[/B] to show at this present time
"; }

for ($n=$i ; $n < $i+5 ; $n++ ) {
  $blog = explode("|", $line[$n]);

  if ( $blog[5] == "" ) {

    $news->vars['desc'] = "";

  }

  else {

    $news->vars['desc'] = ",";

  }

  if (isset($blog[0]))
   {

    echo ("

        [IMG]http://xflive.techguy.org/sources/images/calendar.png[/IMG] " .$blog[1]."[B]" .$blog[2]."[/B]".$news->vars['desc']." " .$blog[5]."
        [B]Posted by:[/B] " .$blog[0]."

        " .$blog[3]."

        [B]Priority:[/B] " .$blog[4]."

    ");

    }

}
?>
```
EDIT:

I saw your new post.

Replace your current code with this new code and if it doesn't work right than post your modified code.


----------



## Xperience (Feb 13, 2007)

Ok it didnt work [showing blanks again] and seems my other modified code aint working either:
http://imgday.com/index.php?do=news

Any more ideas  Thanks a lot on this, btw


----------



## MMJ (Oct 15, 2006)

Okay, I see the problem. I'm working on it.


----------



## Xperience (Feb 13, 2007)

Thanks a lot


----------



## MMJ (Oct 15, 2006)

```
<?php
$mode = 0;
if ($mode == 0) 
	$opFile = $_SERVER['DOCUMENT_ROOT'] . "/admin/database/blog_data.txt";
if ($mode == 1)
	$opFile = "blogfile1.txt";

$bp = fopen($opFile,"r") or die("Error Reading File");
$data = fread($bp, filesize($opFile)+1);
fclose($bp);

$line = explode("\n", $data);
$i = $_GET['page'] ? $_GET['page']*5 : 5;

$f = 0;
for ($a = 0; $a <= count($line); $a = $a+5)
{
    echo $f == $_GET['page'] ? "$f, " : "[URL]$f[/URL]";
	$f++;
}

if (!$data)
	echo "[B]There are no entries[/B] to show at this present time
";

for ($n = $i-5 ; $n <= $i ; $n++ )
{
	$blog = explode("|", $line[$n/5]);

	if ( $blog[5] == "" )
		$news->vars['desc'] = "";
	else
		$news->vars['desc'] = ",";

	if (isset($blog[0]))
		echo
		"
	     [IMG]http://xflive.techguy.org/sources/images/calendar.png[/IMG] " .$blog[1]."[B]" .$blog[2]."[/B]".$news->vars['desc']." " .$blog[5]."
	     [B]Posted by:[/B] " .$blog[0]."

	     " .$blog[3]."

	     [B]Priority:[/B] " .$blog[4]."

		";

}
?>
```
If it doesn't work than if you can pm me the blogfile I can fix it but now I don't have the file so i can't test it myself.


----------



## Xperience (Feb 13, 2007)

ok, it not work, it shows 6 per page, and going to next page shows same content as previous page. so ill give you the blog file so you can have more play with it  and also, if you manage to get it, can you make it so it doesnt show "0", but instead "1", and bold or something the currently on page, and prev and next?

thanks a bunch!


----------



## MMJ (Oct 15, 2006)

okay, lol.


----------



## Xperience (Feb 13, 2007)

hope its all going well


----------



## MMJ (Oct 15, 2006)

Okay, I've tested this extensively and it seems to be working fine.


```
<?php
$mode = 0;
if ($mode == 0) 
    $opFile = $_SERVER['DOCUMENT_ROOT'] . "/admin/database/blog_data.txt";
if ($mode == 1)
    $opFile = "blogfile1.txt";

$bp = fopen($opFile,"r") or die("Error Reading File");
$data = fread($bp, filesize($opFile)+1);
fclose($bp);

$line = explode("\n", $data);
if ($_GET['page'])
	$i = ($_GET['page']+1)*5;
else
	$i = 5;

$f = 0;
for ($a = 0; $a <= count($line); $a = $a+5)
{
    echo $f == $_GET['page'] ? "[B]$f[/B], " : "[URL]$f[/URL], ";
	$f++;
}

if (!$data)
	echo "[B]There are no entries[/B] to show at this present time
";

for ($n = $i-5 ; $n < $i ; $n++ )
{
	$blog = explode("|", $line[$n]);

	if ( $blog[5] == "" )
		$news->vars['desc'] = "";
	else
		$news->vars['desc'] = ",";

	if ($blog[0])
		echo
		"
	     [IMG]http://xflive.techguy.org/sources/images/calendar.png[/IMG] " .$blog[1]."[B]" .$blog[2]."[/B]".$news->vars['desc']." " .$blog[5]."
	     [B]Posted by:[/B] " .$blog[0]."

	     " .$blog[3]."

	     [B]Priority:[/B] " .$blog[4]."

		";

}
?>
```
If its missing something just post back.


----------



## Xperience (Feb 13, 2007)

great  yep its missing a few things. the previous and next things, and also, the last page link, when i click it, shows an empty page 
can you do and fix the aforementioned? thanks a lotttt!


----------



## MMJ (Oct 15, 2006)

Easily solved:

1. *echo $f == $_GET['page'] ? "$f, " : "$f+1, ";*
2.
-1 ?>]Previous page
+1 ?>]Next page

No problem.


----------



## Xperience (Feb 13, 2007)

the previous and next work ok, but.... what if there is no prev page? or if there is no next page  can we solve that too?

oh, and the other problem: go here: http://imgday.com/index.php?do=news

click "7" or "8" why are "7" and "8" listed if they show blank?

thanks alot for your help


----------



## MMJ (Oct 15, 2006)

1.
Yeah, for previous, check if page is 0. For next, check if the page is equal to or greater than $f. Should be easy to implement.

2. I only see one page.


----------



## Xperience (Feb 13, 2007)

yeh there is only one page now. ill make more than one page and then you can check it


----------



## Xperience (Feb 13, 2007)

actually, just click "next" and it shows a blank page :/ strange


----------



## Xperience (Feb 13, 2007)

woot i managed to fix those bugs  thanks alot!

this is solved now


----------



## MMJ (Oct 15, 2006)

I'm glad I helped you fix your problems. 

Good luck on the image hosting site!


----------



## Xperience (Feb 13, 2007)

Thanks! We're doing well  Well.... were doing well till an admin turned on us and deleted all our uploaded images. meh


----------

