Author Topic: Page navigation from while loop  (Read 944 times)

0 Members and 1 Guest are viewing this topic.

Offline GamerGunTopic starter

  • Enthusiast
  • Posts: 151
  • Gender: Male
    • View Profile
    • GamerGun
Page navigation from while loop
« on: March 05, 2010, 05:11:08 AM »
Dear,

How can i turn this while loop into page navigation? So say that after 10 posts, page 2 is being created which shows posts 20 to 30 (and so on).

<?php

    $result 
mysql_query("SELECT * FROM berichten ORDER BY id DESC")
    or die(
mysql_error());

    include(
"includes/rating_functions.php");

    
$numofrows = @mysql_num_rows($result);
    for(
$i 0$i $numofrows$i++)
    {
    
$row mysql_fetch_array$result );

    
$id $row['id'];
    
$message $row['bericht'];
    
$onderwerp $row['onderwerp'];
    
$titel $row['titel'];

	
$titel2 eregi_replace(' ''-'$titel);
	
$titel2 strtolower($titel2);

	
	
$onderwerp2 strtolower($onderwerp);

	
$opvragen "SELECT COUNT(*) AS aantal FROM antwoorden WHERE postid = $id";
	
$aantal2 = (mysql_query($opvragen));
	
$getal mysql_result($aantal2'aantal');

           if(
$i 2)
                   {
                   
$color "brown";
           
$hexcolor "#e8d6b6";
                   }
           else
                   {
                   
$color "gray";
           
$hexcolor "#f4f4f4";
                   }
       echo 
"<table border=\"0\" bordercolor=\"#FF0000\" cellpadding=\"0\" cellspacing=\"0\" width=\"578\">";
       echo 
"<tr class=\"contentbox_top_".$color."\">";
       echo 
"<td height=\"54\"><b><a href=\"../".$titel2."-".$id.".html\">$titel</a></b> door Anoniem".$id." in <a href=\"../zoeken-".$onderwerp2.".html\">".$onderwerp."</a> op ".$row['datum']."</td>";
       echo 
"</tr>";
       echo 
"<tr BGCOLOR=\"".$hexcolor."\">";
       echo 
"<td>";
	
echo 
$message;
       echo 
"<br><br>";
       echo 
"<a href=\"javascript:popup('$id')\">Vertel een vriend(in) hierover</a> - ";
       echo 
"<a href=\"../".$titel2."-".$id.".html\">Geef commentaar ($getal)</a><br><br>";
       echo 
pullRating($id,true,true,true,NULL);
       echo 
"</td>";
       echo 
"</tr>";
       echo 
"<tr class=\"contentbox_bottom_".$color."\">";
       echo 
"<td height=\"17\"></td>";
       echo 
"</tr>";
       echo 
"</table>";

    }

    echo 
"<script type=\"text/javascript\">";
    echo 
"function popup(id){";
    echo 
"window.open('tell_a_friend.php?nummer=' + id +'','tellafriend_script','scrollbars=1,statusbar=1,resizable=1,width=400,height=510');}";
    echo 
"</script>";


    
?>


Thanks in advance.

Pz -T

« Last Edit: March 05, 2010, 05:13:24 AM by GamerGun »

Offline GamerGunTopic starter

  • Enthusiast
  • Posts: 151
  • Gender: Male
    • View Profile
    • GamerGun
Re: Page navigation from while loop
« Reply #1 on: March 08, 2010, 08:46:27 AM »
Anyone? :confused:

Offline simonrs

  • Irregular
  • Posts: 30
    • View Profile
Re: Page navigation from while loop
« Reply #2 on: March 08, 2010, 09:52:43 AM »
You need to add a "LIMIT" clause to your original query. For instance,

Code: [Select]
$result = mysql_query("SELECT * FROM berichten ORDER BY id DESC LIMIT 10, 10")
Will skip over the first 10 results, so the first result you get will be the 11th, and the last the 20th.

You will want to develop this further using a constant for the number of items per page and querying some kind of variable, most likely in the $_GET array, for how many items to skip over:

Code: [Select]
// number of items to show on each page
define('ITEMS_PER_PAGE', 10);

// now we're getting the number to start fetching from. This line of code is saying,
// if $_GET['start'] is set and it is numeric, then use it, otherwise default to zero
$start = (isset($_GET['start']) && is_numeric($_GET['start'])) ? $_GET['start'] : 0;

// now do the query: notice now that I'm actually fetching one more than the
// ITEMS_PER_PAGE constant: this will allow us to know whether or not to show
// another "next 10 items" link
$result = mysql_query("SELECT * FROM berichten ORDER BY id DESC LIMIT $start, " . ITEMS_PER_PAGE + 1)

I'm running out of time now but then you can go ahead, iterate through and print out a link which will be something like:

Code: [Select]
echo '<a href="yourpage.php?start=' . $start + 10 . '">next 10 &raquo;</a>';

I personally prefer a "get next 10 items" link which uses ajax and similar techniques to get the next 10.

*disclaimer: above isn't debugged!

Offline GamerGunTopic starter

  • Enthusiast
  • Posts: 151
  • Gender: Male
    • View Profile
    • GamerGun
Re: Page navigation from while loop
« Reply #3 on: March 09, 2010, 05:07:34 AM »
Thanks,

Although im having problems with the query;

define('ITEMS_PER_PAGE'10);

$start = (isset($_GET['start']) && is_numeric($_GET['start'])) ? $_GET['start'] : 0;

    
$result mysql_query("SELECT * FROM berichten ORDER BY id DESC LIMIT $start,.ITEMS_PER_PAGE; + 1")


This prints;

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.ITEMS_PER_PAGE; + 1' at line 1

I also tried ".ITEMS_PER_PAGE; + 1 and \".ITEMS_PER_PAGE; + 1 and '.ITEMS_PER_PAGE; + 1 but cant get it to work.

Edit; Leaving the ; away gives the same.

Thanks!
« Last Edit: March 09, 2010, 05:09:24 AM by GamerGun »

Offline GamerGunTopic starter

  • Enthusiast
  • Posts: 151
  • Gender: Male
    • View Profile
    • GamerGun
Re: Page navigation from while loop
« Reply #4 on: March 11, 2010, 05:00:03 AM »
Bump.

$result mysql_query("SELECT * FROM berichten ORDER BY id DESC LIMIT $start, " ITEMS_PER_PAGE 1)

Does not work either...

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Page navigation from while loop
« Reply #5 on: March 11, 2010, 05:02:55 AM »
Your not meant to take ITEMS_PER_PAGE literally. Example....


$result 
mysql_query("SELECT * FROM berichten ORDER BY id DESC LIMIT $start, 20+1")

Offline GamerGunTopic starter

  • Enthusiast
  • Posts: 151
  • Gender: Male
    • View Profile
    • GamerGun
Re: Page navigation from while loop
« Reply #6 on: March 11, 2010, 06:35:02 AM »
Why would you use the

define('ITEMS_PER_PAGE'10);

then?

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Page navigation from while loop
« Reply #7 on: March 11, 2010, 07:26:49 AM »
Why would you use the

define('ITEMS_PER_PAGE'10);

then?

Sorry, I didn't even see that there. Your last piece of code should work fine then.

Offline GamerGunTopic starter

  • Enthusiast
  • Posts: 151
  • Gender: Male
    • View Profile
    • GamerGun
Re: Page navigation from while loop
« Reply #8 on: March 11, 2010, 07:28:05 AM »
No problems.

But it says;

Code: [Select]
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Page navigation from while loop
« Reply #9 on: March 11, 2010, 07:31:17 AM »
Put the query in a variable and lets take a better look at it.


$sql 
"SELECT * FROM berichten ORDER BY id DESC LIMIT $start, " ITEMS_PER_PAGE 1;
echo 
$sql;
$result mysql_query($sql);

Offline GamerGunTopic starter

  • Enthusiast
  • Posts: 151
  • Gender: Male
    • View Profile
    • GamerGun
Re: Page navigation from while loop
« Reply #10 on: March 11, 2010, 07:37:03 AM »
Well, all it gives now is: 1

thanks for helping me :)

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Page navigation from while loop
« Reply #11 on: March 11, 2010, 07:41:42 AM »
Post your exact code.

Offline GamerGunTopic starter

  • Enthusiast
  • Posts: 151
  • Gender: Male
    • View Profile
    • GamerGun
Re: Page navigation from while loop
« Reply #12 on: March 11, 2010, 07:44:43 AM »
<?php

$find 
mysql_real_escape_string($_GET['find']);
$search mysql_real_escape_string($_GET['search']);

$pieces explode(" "$find);

define('ITEMS_PER_PAGE'10);

$start = (isset($_GET['start']) && is_numeric($_GET['start'])) ? $_GET['start'] : 0;

if(!empty(
$search))
{
    
$result mysql_query("SELECT * FROM berichten WHERE onderwerp LIKE '$search' ORDER BY id DESC")
    or die(
mysql_error());
}
elseif(!empty(
$pieces[0]) && !empty($pieces[1]))
{
   
$result mysql_query("SELECT * FROM berichten WHERE bericht LIKE '%$pieces[0]%' OR bericht LIKE '%$pieces[1]%' ORDER BY id DESC")
   or die(
mysql_error());
}
elseif(!empty(
$pieces[0]) && empty($pieces[1]))
{
   
$result mysql_query("SELECT * FROM berichten WHERE bericht LIKE '%$pieces[0]%' ORDER BY id DESC")
   or die(
mysql_error());
}
else
{
$sql "SELECT * FROM berichten ORDER BY id DESC LIMIT $start, " ITEMS_PER_PAGE 1;
echo 
$sql;
$result mysql_query($sql);
}

    include(
"includes/rating_functions.php");

    
$numofrows = @mysql_num_rows($result);
    for(
$i 0$i $numofrows$i++)
    {
    
$row mysql_fetch_array$result );

    
$id $row['id'];
    
$message $row['bericht'];
    
$onderwerp $row['onderwerp'];
    
$titel $row['titel'];

	
$titel2 eregi_replace(' ''-'$titel);
	
$titel2 strtolower($titel2);

	
	
$onderwerp2 strtolower($onderwerp);

	
$opvragen "SELECT COUNT(*) AS aantal FROM antwoorden WHERE postid = $id";
	
$aantal2 = (mysql_query($opvragen));
	
$getal mysql_result($aantal2'aantal');

           if(
$i 2)
                   {
                   
$color "brown";
           
$hexcolor "#e8d6b6";
                   }
           else
                   {
                   
$color "gray";
           
$hexcolor "#f4f4f4";
                   }
       echo 
"<table border=\"0\" bordercolor=\"#FF0000\" cellpadding=\"0\" cellspacing=\"0\" width=\"578\">";
       echo 
"<tr class=\"contentbox_top_".$color."\">";
       echo 
"<td height=\"54\"><b><a href=\"../".$titel2."-".$id.".html\">$titel</a></b> door Anoniem".$id." in <a href=\"../onderwerp-".$onderwerp2.".html\">".$onderwerp."</a> op ".$row['datum']."</td>";
       echo 
"</tr>";
       echo 
"<tr BGCOLOR=\"".$hexcolor."\">";
       echo 
"<td>";
	
echo 
$message;
       echo 
"<br><br>";
       echo 
"<a href=\"javascript:popup('$id')\">Vertel een vriend(in) hierover</a> - ";
       echo 
"<a href=\"../".$titel2."-".$id.".html\">Geef commentaar ($getal)</a><br><br>";
       echo 
pullRating($id,true,true,true,NULL);
       echo 
"</td>";
       echo 
"</tr>";
       echo 
"<tr class=\"contentbox_bottom_".$color."\">";
       echo 
"<td height=\"17\"></td>";
       echo 
"</tr>";
       echo 
"</table>";

    }

    echo 
"<script type=\"text/javascript\">";
    echo 
"function popup(id){";
    echo 
"window.open('tell_a_friend.php?nummer=' + id +'','tellafriend_script','scrollbars=1,statusbar=1,resizable=1,width=400,height=510');}";
    echo 
"</script>";


    
?>

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Page navigation from while loop
« Reply #13 on: March 11, 2010, 07:53:25 AM »
You really need to re-factor that code. All query results should be checked before attempting to use them. The general syntax would be....


$sql 
"your query";
if (
$result mysql_query($sql)) {
  if (
mysql_num_rows($result)) {
    
// it is safe to use $result in here.
  
} else {
    
// no results where found
  
}
} else {
  
// an error occurred, handle it.
}


You could likely re-factor allot of those queries into JOINs as well, making your code allot more efficient. As for this....


$sql 
"SELECT * FROM berichten ORDER BY id DESC LIMIT $start, " ITEMS_PER_PAGE 1;
echo 
$sql;


echo'ing 1. Not possible.

Offline GamerGunTopic starter

  • Enthusiast
  • Posts: 151
  • Gender: Male
    • View Profile
    • GamerGun
Re: Page navigation from while loop
« Reply #14 on: March 11, 2010, 07:56:48 AM »
Well, thanks for noticing but what does it change to the fact that it really outputs 1 with the code i posted above?

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Page navigation from while loop
« Reply #15 on: March 11, 2010, 08:03:50 AM »
Is this page being included into another?

Offline GamerGunTopic starter

  • Enthusiast
  • Posts: 151
  • Gender: Male
    • View Profile
    • GamerGun
Re: Page navigation from while loop
« Reply #16 on: March 11, 2010, 08:04:46 AM »
No, this is just the index.php which only includes a menu / footer and some config files.

Offline GamerGunTopic starter

  • Enthusiast
  • Posts: 151
  • Gender: Male
    • View Profile
    • GamerGun
Re: Page navigation from while loop
« Reply #17 on: March 12, 2010, 03:28:34 AM »
BTW, i was thinking about what you said concerning validating all query results, but i'm not kinda sure how to do that.

Let's say i have this code;


    $result 
mysql_query("SELECT * FROM berichten ORDER BY id DESC")
    or die(
mysql_error());

    
$numofrows = @mysql_num_rows($result);
    for(
$i 0$i $numofrows$i++)
    {
    
$row mysql_fetch_array$result );

    
$id $row['id'];
    
$message $row['bericht'];
    
$onderwerp $row['onderwerp'];
    
$titel $row['titel'];

    }


How would i add the if statement then to check it?

Like this?


    $result 
mysql_query("SELECT * FROM berichten ORDER BY id DESC") or die(mysql_error());
if (
$row mysql_query($result)) {
  if (
mysql_num_rows($row)) {

    
$id $row['id'];
    
$message $row['bericht'];
    
$onderwerp $row['onderwerp'];
    
$titel $row['titel'];

  } else {
    
// no results where found
  
}
} else {
  
// an error occurred, handle it.
}


Thanks
« Last Edit: March 12, 2010, 03:31:00 AM by GamerGun »

Offline GamerGunTopic starter

  • Enthusiast
  • Posts: 151
  • Gender: Male
    • View Profile
    • GamerGun
Re: Page navigation from while loop
« Reply #18 on: March 12, 2010, 07:28:57 AM »
Apart from that, thanks to a friend i got it working (via a different way though).

    $result2 mysql_query("SELECT * FROM berichten ORDER BY id DESC")
    or die(
mysql_error());
    
$totaalrecords mysql_num_rows($result2);

$ITEMS_PER_PAGE "3";

$start = (isset($_GET['start']) && is_numeric($_GET['start'])) ? $_GET['start'] : 0;

if ( 
$start+$ITEMS_PER_PAGE<$totaalrecords ) {
	
echo 
"<a href=\"index.php?start=$link\">next 3 &raquo;</a>";
}

$nummer "1";
for ( 
$counter 0$counter <= $totaalrecords$counter += $ITEMS_PER_PAGE ) {
	
echo 
"<a href=\"index.php?start=$counter\">pagina $nummer</a> ";
	
$nummer $nummer +1;


This outputs (i have 7 records at the moment)

index.php?start=0
next 3 »
page 1 page 2 page 3

index.php?start=3
next 3 »
page 1 page 2 page 3

index.php?start=6
page 1 page 2 page 3

So that's fine. Only thing i want is, instead of page 1 till 1500 (in the far future), something like this;

1 - 2 - 3 ... 120 - 121 - 122

When you are on page 15, it will show 12 - 13 - 14 ... 120 - 121 - 122.

Can anyone help me with that?

Thanks!

Offline GamerGunTopic starter

  • Enthusiast
  • Posts: 151
  • Gender: Male
    • View Profile
    • GamerGun
Re: Page navigation from while loop
« Reply #19 on: March 12, 2010, 08:58:24 AM »
Can't edit my message?

Here is my current code, made some changes:

 <?php include 'config/connect.php';

$ITEMS_PER_PAGE "2";

$start = (isset($_GET['start']) && is_numeric($_GET['start'])) ? $_GET['start'] : 0;

$find mysql_real_escape_string($_GET['find']);
$search mysql_real_escape_string($_GET['search']);

$pieces explode(" "$find);

if(!empty(
$search))
{
    
$totaal mysql_query("SELECT * FROM berichten WHERE onderwerp LIKE '$search' ORDER BY id DESC"
    or die(
mysql_error());
    
$result mysql_query("SELECT * FROM berichten WHERE onderwerp LIKE '$search' ORDER BY id DESC LIMIT $start,$ITEMS_PER_PAGE")
    or die(
mysql_error());
    
$totaalrecords mysql_num_rows($totaal);
}
elseif(!empty(
$pieces[0]) && !empty($pieces[1]))
{
    
$totaal mysql_query("SELECT * FROM berichten WHERE bericht LIKE '%$pieces[0]%' OR bericht LIKE '%$pieces[1]%' ORDER BY id DESC")
    or die(
mysql_error());
    
$result mysql_query("SELECT * FROM berichten WHERE bericht LIKE '%$pieces[0]%' OR bericht LIKE '%$pieces[1]%' ORDER BY id DESC LIMIT $start,$ITEMS_PER_PAGE")
    or die(
mysql_error());
    
$totaalrecords mysql_num_rows($totaal);
}
elseif(!empty(
$pieces[0]) && empty($pieces[1]))
{
    
$totaal mysql_query("SELECT * FROM berichten WHERE bericht LIKE '%$pieces[0]%' ORDER BY id DESC")
    or die(
mysql_error());
    
$result mysql_query("SELECT * FROM berichten WHERE bericht LIKE '%$pieces[0]%' ORDER BY id DESC LIMIT $start,$ITEMS_PER_PAGE")
    or die(
mysql_error());
    
$totaalrecords mysql_num_rows($totaal);
}
else
{
    
$result mysql_query("SELECT * FROM berichten ORDER BY id DESC LIMIT $start,$ITEMS_PER_PAGE")
    or die(
mysql_error());
    
$result2 mysql_query("SELECT * FROM berichten ORDER BY id DESC")
    or die(
mysql_error());
    
$totaalrecords mysql_num_rows($result2);
}

echo 
"<br><a href=\"plaats.html\"><img src=\"images/melden_knop.jpg\" border=\"0\" /></a><br><br>";

$link $start $ITEMS_PER_PAGE;

if(!empty(
$search))
{
   
$keuze "onderwerp-$search-pagina";
}
elseif(!empty(
$pieces[0]))
{
   
$keuze "zoeken-$find-pagina";
}
else
{
   
$keuze "pagina";
}

if ( 
$start+$ITEMS_PER_PAGE<$totaalrecords ) {
	
echo 
"<a href=\"$keuze-$link.html\">next $ITEMS_PER_PAGE &raquo;</a>";
}

echo 
"<br>";

$nummer "1";
for ( 
$counter 0$counter $totaalrecords$counter += $ITEMS_PER_PAGE ) {
	
echo 
"<a href=\"$keuze-$counter.html\">pagina $nummer</a> ";
	
$nummer $nummer +1;
}

    include(
"includes/rating_functions.php");

    
$numofrows = @mysql_num_rows($result);
    for(
$i 0$i $numofrows$i++)
    {
    
$row mysql_fetch_array$result );

    
$id $row['id'];
    
$message $row['bericht'];
    
$onderwerp $row['onderwerp'];
    
$titel $row['titel'];

	
$titel2 eregi_replace(' ''-'$titel);
	
$titel2 strtolower($titel2);

	
	
$onderwerp2 strtolower($onderwerp);

	
$opvragen "SELECT COUNT(*) AS aantal FROM antwoorden WHERE postid = $id";
	
$aantal2 = (mysql_query($opvragen));
	
$getal mysql_result($aantal2'aantal');

           if(
$i 2)
                   {
                   
$color "brown";
           
$hexcolor "#e8d6b6";
                   }
           else
                   {
                   
$color "gray";
           
$hexcolor "#f4f4f4";
                   }
       echo 
"<table border=\"0\" bordercolor=\"#FF0000\" cellpadding=\"0\" cellspacing=\"0\" width=\"578\">";
       echo 
"<tr class=\"contentbox_top_".$color."\">";
       echo 
"<td height=\"54\"><b><a href=\"../".$titel2."-".$id.".html\">$titel</a></b> door Anoniem".$id." in <a href=\"../onderwerp-".$onderwerp2.".html\">".$onderwerp."</a> op ".$row['datum']."</td>";
       echo 
"</tr>";
       echo 
"<tr BGCOLOR=\"".$hexcolor."\">";
       echo 
"<td>";
	
echo 
$message;
       echo 
"<br><br>";
       echo 
"<a href=\"javascript:popup('$id')\">Vertel een vriend(in) hierover</a> - ";
       echo 
"<a href=\"../".$titel2."-".$id.".html\">Geef commentaar ($getal)</a><br><br>";
       echo 
pullRating($id,true,true,true,NULL);
       echo 
"</td>";
       echo 
"</tr>";
       echo 
"<tr class=\"contentbox_bottom_".$color."\">";
       echo 
"<td height=\"17\"></td>";
       echo 
"</tr>";
       echo 
"</table>";

    }

    echo 
"<script type=\"text/javascript\">";
    echo 
"function popup(id){";
    echo 
"window.open('tell_a_friend.php?nummer=' + id +'','tellafriend_script','scrollbars=1,statusbar=1,resizable=1,width=400,height=510');}";
    echo 
"</script>";


    
?> 

Offline GamerGunTopic starter

  • Enthusiast
  • Posts: 151
  • Gender: Male
    • View Profile
    • GamerGun
Re: Page navigation from while loop
« Reply #20 on: March 16, 2010, 05:34:41 AM »
Can anyone help please? I made the code a bit more neat, just don't know how to do the trick when there are many pages (see some posts higher).

<?php include 'config/connect.php';

$ITEMS_PER_PAGE "2";

$start = (isset($_GET['start']) && is_numeric($_GET['start'])) ? $_GET['start'] : 0;

$find mysql_real_escape_string($_GET['find']);
$search mysql_real_escape_string($_GET['search']);

$pieces explode(" "$find);

if(!empty(
$search))
{
    
$totaal mysql_query("SELECT * FROM berichten WHERE onderwerp LIKE '$search' ORDER BY id DESC"
    or die(
mysql_error());
    
$result mysql_query("SELECT * FROM berichten WHERE onderwerp LIKE '$search' ORDER BY id DESC LIMIT $start,$ITEMS_PER_PAGE")
    or die(
mysql_error());
    
$totaalrecords mysql_num_rows($totaal);
}
elseif(!empty(
$pieces[0]) && !empty($pieces[1]))
{
    
$totaal mysql_query("SELECT * FROM berichten WHERE bericht LIKE '%$pieces[0]%' OR bericht LIKE '%$pieces[1]%' ORDER BY id DESC")
    or die(
mysql_error());
    
$result mysql_query("SELECT * FROM berichten WHERE bericht LIKE '%$pieces[0]%' OR bericht LIKE '%$pieces[1]%' ORDER BY id DESC LIMIT $start,$ITEMS_PER_PAGE")
    or die(
mysql_error());
    
$totaalrecords mysql_num_rows($totaal);
}
elseif(!empty(
$pieces[0]) && empty($pieces[1]))
{
    
$totaal mysql_query("SELECT * FROM berichten WHERE bericht LIKE '%$pieces[0]%' ORDER BY id DESC")
    or die(
mysql_error());
    
$result mysql_query("SELECT * FROM berichten WHERE bericht LIKE '%$pieces[0]%' ORDER BY id DESC LIMIT $start,$ITEMS_PER_PAGE")
    or die(
mysql_error());
    
$totaalrecords mysql_num_rows($totaal);
}
else
{
    
$result mysql_query("SELECT * FROM berichten ORDER BY id DESC LIMIT $start,$ITEMS_PER_PAGE")
    or die(
mysql_error());
    
$result2 mysql_query("SELECT * FROM berichten ORDER BY id DESC")
    or die(
mysql_error());
    
$totaalrecords mysql_num_rows($result2);
}

echo 
"<br /><a href=\"plaats.html\"><img src=\"images/melden_knop.jpg\" border=\"0\" alt=\"Plaats een verhaal\" /></a><br /><br />";

$link $start $ITEMS_PER_PAGE;

if(!empty(
$search))
{
   
$keuze "onderwerp-$search-pagina";
}
elseif(!empty(
$pieces[0]))
{
   
$keuze "zoeken-$find-pagina";
}
else
{
   
$keuze "pagina";
}

    include(
"includes/rating_functions.php");

    
$numofrows = @mysql_num_rows($result);
    for(
$i 0$i $numofrows$i++)
    {
    
$row mysql_fetch_array$result );

    
$id $row['id'];
    
$message $row['bericht'];
    
$onderwerp $row['onderwerp'];
    
$titel $row['titel'];

    
$date_entered date("d/m/Y"strtotime($row[datum]));

	
$titel2 eregi_replace(' ''-'$titel);
	
$titel2 strtolower($titel2);

	
	
$onderwerp2 strtolower($onderwerp);

	
$opvragen "SELECT COUNT(*) AS aantal FROM antwoorden WHERE postid = $id";
	
$aantal2 = (mysql_query($opvragen));
	
$getal mysql_result($aantal2'aantal');

           if(
$i 2)
                   {
                   
$color "brown";
           
$hexcolor "#e8d6b6";
                   }
           else
                   {
                   
$color "gray";
           
$hexcolor "#f4f4f4";
                   }

       echo 
"<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"578\">";
       echo 
"<tr class=\"contentbox_top_".$color."\">";
       echo 
"<td height=\"54\"><strong><a href=\"../".$titel2."-".$id.".html\">$titel</a></strong> door Anoniem".$id." in <a href=\"../onderwerp-".$onderwerp2.".html\">".$onderwerp."</a> op ".$date_entered."</td>";
       echo 
"</tr>";
       echo 
"<tr bgcolor=\"".$hexcolor."\">";
       echo 
"<td>";
	
echo 
$message;
       echo 
"<br /><br />";
       echo 
"<a href=\"javascript:popup('$id')\">Vertel een vriend(in) hierover</a> - ";
       echo 
"<a href=\"../".$titel2."-".$id.".html\">Geef commentaar ($getal)</a><br /><br />";
       echo 
pullRating($id,true,true,true,NULL);
       echo 
"</td>";
       echo 
"</tr>";
       echo 
"<tr class=\"contentbox_bottom_".$color."\">";
       echo 
"<td height=\"17\"></td>";
       echo 
"</tr>";
       echo 
"</table>";

    }

    echo 
"<script type=\"text/javascript\">";
    echo 
"function popup(id){";
    echo 
"window.open('tell_a_friend.php?nummer=' + id +'','tellafriend_script','scrollbars=1,statusbar=1,resizable=1,width=400,height=510');}";
    echo 
"</script>";

if ( 
$start+$ITEMS_PER_PAGE<$totaalrecords ) {
	
echo 
"<a href=\"$keuze-$link.html\">Volgende pagina &raquo;</a>";
}

echo 
"<br />";

$nummer "1";
for ( 
$counter 0$counter $totaalrecords$counter += $ITEMS_PER_PAGE ) {
	
echo 
"<a href=\"$keuze-$counter.html\">$nummer</a> ";
	
$nummer $nummer +1;
}

    
?>

Offline GamerGunTopic starter

  • Enthusiast
  • Posts: 151
  • Gender: Male
    • View Profile
    • GamerGun
Re: Page navigation from while loop
« Reply #21 on: March 30, 2010, 03:45:38 AM »
I changed the entire code. Made use of: http://www.phpeasystep.com/phptu/29.html

Here it is if anyone is interested:


<?php $valid_ips = array('194.171.252.101','130.138.227.10','88.159.185.149');
if (!
in_array($_SERVER['REMOTE_ADDR'],$valid_ips)) {
    echo 
"<b>Binnenkort online!</b>";
    exit();
}  
?>

<?php include 'header.php'?>

<div id="content">
<div id="contentleft">

<?php

$tbl_name
="berichten";

$adjacents 3;

$targetpage "index.php";
$limit 2;
$page $_GET['page'];
	

if(
$page)
	
$start = ($page 1) * $limit;
else
	
$start 0;

$find mysql_real_escape_string($_GET['find']);
$search mysql_real_escape_string($_GET['search']);

$pieces explode(" "$find);

if(!empty(
$search))
{
    
$result mysql_query("SELECT * FROM berichten WHERE onderwerp LIKE '$search' ORDER BY id DESC LIMIT $start$limit"
    or die(
mysql_error());

	
$query mysql_query("SELECT COUNT(*) as num FROM $tbl_name WHERE onderwerp LIKE '$search'")
    
	
or die(
mysql_error());
	
$total_pages mysql_fetch_array($query);
	
$total_pages $total_pages[num];

}
elseif(!empty(
$pieces[0]) && !empty($pieces[1]))
{
    
$result mysql_query("SELECT * FROM berichten WHERE bericht LIKE '%$pieces[0]%' OR bericht LIKE '%$pieces[1]%' ORDER BY id DESC LIMIT $start$limit")
    or die(
mysql_error());

	
$query mysql_query("SELECT COUNT(*) as num FROM $tbl_name WHERE onderwerp LIKE '%$pieces[0]%' OR bericht LIKE '%$pieces[1]%'")
    
	
or die(
mysql_error());
	
$total_pages mysql_fetch_array($query);
	
$total_pages $total_pages[num];
}
elseif(!empty(
$pieces[0]) && empty($pieces[1]))
{
    
$result mysql_query("SELECT * FROM berichten WHERE bericht LIKE '%$pieces[0]%' ORDER BY id DESC LIMIT $start$limit")
    or die(
mysql_error());

	
$query mysql_query("SELECT COUNT(*) as num FROM $tbl_name WHERE bericht LIKE '%$pieces[0]%'")
    
	
or die(
mysql_error());
	
$total_pages mysql_fetch_array($query);
	
$total_pages $total_pages[num];

}
else
{
    
$result mysql_query("SELECT * FROM berichten ORDER BY id DESC LIMIT $start$limit")
    or die(
mysql_error());

	
$query mysql_query("SELECT COUNT(*) as num FROM $tbl_name")
    
	
or die(
mysql_error());
	
$total_pages mysql_fetch_array($query);
	
$total_pages $total_pages[num];

}

if (
$page == 0$page 1;
$prev $page 1;
$next $page 1;
$lastpage ceil($total_pages/$limit);
$lpm1 $lastpage 1;

echo 
"<br /><a href=\"plaats.html\"><img src=\"images/melden_knop.jpg\" border=\"0\" alt=\"Plaats een verhaal\" /></a><br /><br />";

?>

<?php

if(!empty($search))
{
   
$keuze "onderwerp-$search-pagina";
}
elseif(!empty(
$pieces[0]))
{
   
$keuze "zoeken-$find-pagina";
}
else
{
   
$keuze "pagina";
}

	
$pagination "";
	
if(
$lastpage 1)
	
{
	

	
	
$pagination .= "<div class=\"pagination\">";
	
	
if (
$page 1
	
	
	
$pagination.= "<a href=\"$keuze-$prev.html\">&laquo; vorige</a>";
	
	
else
	
	
	
$pagination.= "<span class=\"disabled\">&laquo; vorige</span>";
	

	
	
	

	
	
if (
$lastpage + ($adjacents 2))
	
	
{
	

	
	
	
for (
$counter 1$counter <= $lastpage$counter++)
	
	
	
{
	
	
	
	
if (
$counter == $page)
	
	
	
	
	
$pagination.= "<span class=\"current\">$counter</span>";
	
	
	
	
else
	
	
	
	
	
$pagination.= "<a href=\"$keuze-$counter.html\">$counter</a>";
	
	
	
	
	

	
	
	
}
	
	
}
	
	
elseif(
$lastpage + ($adjacents 2))
	
	
{
	
	
	
if(
$page + ($adjacents 2))
	
	

	
	
	
{
	
	
	
	
for (
$counter 1$counter + ($adjacents 2); $counter++)
	
	
	
	
{
	
	
	
	
	
if (
$counter == $page)
	
	
	
	
	
	
$pagination.= "<span class=\"current\">$counter</span>";
	
	
	
	
	
else
	
	
	
	
	
	
$pagination.= "<a href=\"$keuze-$counter.html\">$counter</a>";
	
	
	
	
	

	
	
	
	
}
	
	
	
	
$pagination.= "...";
	
	
	
	
$pagination.= "<a href=\"$keuze-$lpm1.html\">$lpm1</a>";
	
	
	
	
$pagination.= "<a href=\"$keuze-$lastpage.html\">$lastpage</a>";
	
	

	
	
	
}
	
	
	
elseif(
$lastpage - ($adjacents 2) > $page && $page > ($adjacents 2))
	
	
	
{
	
	
	
	
$pagination.= "<a href=\"$keuze-1.html\">1</a>";
	
	
	
	
$pagination.= "<a href=\"$keuze-2.html\">2</a>";
	
	
	
	
$pagination.= "...";
	
	
	
	
for (
$counter $page $adjacents$counter <= $page $adjacents$counter++)
	
	
	
	
{
	
	
	
	
	
if (
$counter == $page)
	
	
	
	
	
	
$pagination.= "<span class=\"current\">$counter</span>";
	
	
	
	
	
else
	
	
	
	
	
	
$pagination.= "<a href=\"$keuze-$counter.html\">$counter</a>";
	
	
	
	
	

	
	
	
	
}
	
	
	
	
$pagination.= "...";
	
	
	
	
$pagination.= "<a href=\"$keuze-$lpm1.html\">$lpm1</a>";
	
	
	
	
$pagination.= "<a href=\"$keuze-$lastpage.html\">$lastpage</a>";
	
	

	
	
	
}
	
	
	
else
	
	
	
{
	
	
	
	
$pagination.= "<a href=\"$keuze-1.html\">1</a>";
	
	
	
	
$pagination.= "<a href=\"$keuze-2.html\">2</a>";
	
	
	
	
$pagination.= "...";
	
	
	
	
for (
$counter $lastpage - (+ ($adjacents 2)); $counter <= $lastpage$counter++)
	
	
	
	
{
	
	
	
	
	
if (
$counter == $page)
	
	
	
	
	
	
$pagination.= "<span class=\"current\">$counter</span>";
	
	
	
	
	
else
	
	
	
	
	
	
$pagination.= "<a href=\"$keuze-$counter.html\">$counter</a>";
	
	
	
	

	
	
	
	
}
	
	
	
}
	
	
}

	
	
if (
$page $counter 1
	
	
	
$pagination.= "<a href=\"$keuze-$next.html\">volgende &raquo;</a>";
	
	
else
	
	
	
$pagination.= "<span class=\"disabled\">volgende &raquo;</span>";
	
	
$pagination.= "</div>\n";
	
	

	
}

?>

<?php

    
include("includes/rating_functions.php");

    
$numofrows = @mysql_num_rows($result);
    for(
$i 0$i $numofrows$i++)
    {
    
$row mysql_fetch_array$result );

    
$id $row['id'];
    
$message $row['bericht'];
    
$onderwerp $row['onderwerp'];
    
$titel $row['titel'];

    
$date_entered date("d/m/Y"strtotime($row[datum]));

	
$titel2 eregi_replace(' ''-'$titel);
	
$titel2 strtolower($titel2);

	
	
$onderwerp2 strtolower($onderwerp);

	
$opvragen "SELECT COUNT(*) AS aantal FROM antwoorden WHERE postid = $id";
	
$aantal2 = (mysql_query($opvragen));
	
$getal mysql_result($aantal2'aantal');

           if(
$i 2)
                   {
                   
$color "brown";
           
$hexcolor "#e8d6b6";
                   }
           else
                   {
                   
$color "gray";
           
$hexcolor "#f4f4f4";
                   }

       echo 
"<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"578\">";
       echo 
"<tr class=\"contentbox_top_".$color."\">";
       echo 
"<td height=\"54\"><strong><a href=\"../".$titel2."-".$id.".html\">$titel</a></strong> door Anoniem".$id." in <a href=\"../onderwerp-".$onderwerp2.".html\">".$onderwerp."</a> op ".$date_entered."</td>";
       echo 
"</tr>";
       echo 
"<tr bgcolor=\"".$hexcolor."\">";
       echo 
"<td>";
	
echo 
$message;
       echo 
"<br /><br />";
       echo 
"<a href=\"vertel-$id.html\" target=\"arbitrade\" onclick=\"return !window.open(this.href, this.target, 'scrollbars,resizable,width=415,height=510');\">Vertel een vriend(in) hierover</a> - ";
       echo 
"<a href=\"../".$titel2."-".$id.".html\">Geef commentaar ($getal)</a><br /><br />";
       echo 
pullRating($id,true,true,true,NULL);
       echo 
"</td>";
       echo 
"</tr>";
       echo 
"<tr class=\"contentbox_bottom_".$color."\">";
       echo 
"<td height=\"17\"></td>";
       echo 
"</tr>";
       echo 
"</table>";

    }

    echo 
"<script type=\"text/javascript\">";
    echo 
"function popup(id){";
    echo 
"window.open('tell_a_friend.php?nummer=' + id +'','tellafriend_script','scrollbars=1,statusbar=1,resizable=1,width=400,height=510');}";
    echo 
"</script>";

?>

<?=$pagination?>

<br />

</div><!-- Closing contentleft -->

<?php include 'menu.php'?>

<?php include 'footer.php'?>

</div>

</body>
</html>
« Last Edit: March 30, 2010, 03:49:02 AM by GamerGun »