Jump to content

Guestbook


J-C

Recommended Posts

Well I have finally coded a simple guest book for my site, didn't take long is pretty simple  I haven't even added bb codes or smiles etc, but I want to know how to make a next page after 10 entries have been made in the database.

 

For example 10 people signed my guest book I want to automatically generate another page for 10 more entries because displaying all entries would make a page load slow.

 

I hope some one understood what i meant  :wtf:

Link to comment
Share on other sites

So, what you wanna have is a pagination or a navigation, with a number navigation and next and previous page. such example is, try to search something on a google, and if you get a lot of results there will be a number, next and previous navigation at the bottom.

 

EXACTLY.

I have tried looking for tutorial and man no luck,  I'm guessing it has to search mysql  and some other stuff, if you can point me to a tutorial to achieve something similar that'd be great

Link to comment
Share on other sites

So, what you wanna have is a pagination or a navigation, with a number navigation and next and previous page. such example is, try to search something on a google, and if you get a lot of results there will be a number, next and previous navigation at the bottom.

 

EXACTLY.

I have tried looking for tutorial and man no luck,  I'm guessing it has to search mysql  and some other stuff, if you can point me to a tutorial to achieve something similar that'd be great

 

There's this a magic site called Magic Tutorial Land Where It Is All Written For You.

 

Hell, even this site has a good tutorial written up:

http://www.phpfreaks.com/tutorial/basic-pagination

 

EDIT: Thorpe beat me to it by 3 seconds.

Link to comment
Share on other sites

OK so i followed the tutorial and everything works fine except for one thing... when ever i click next to go to next page it doesn't go to it, it goes to it on the browser but it still displays the same page i was currently at.

 


<?php

$host="localhost";
$username="";
$password="";
$db_name="guestbook";
$tbl_name="gb";

// Connect to server and select database.
$conn = mysql_connect("$host", "$username", "$password")or die("cannot connect to the server ");
$select_db = mysql_select_db("$db_name",$conn)or die("cannot select the Data Base");

$sql="SELECT COUNT(*) FROM $tbl_name";
$result=mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$rows = mysql_fetch_row($result);
$num_of_rows = $r[0];
$rowsperpage = 2;
$totalpages = ceil($num_of_rows / $rowsperpage);


// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if


// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

$offset = ($currentpage - 1) * $rowsperpage;


$sql = "SELECT name, datetime, email, comment, id, comment FROM $tbl_name LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);


//while ($list = mysql_fetch_assoc($result)) {

while($rows=mysql_fetch_assoc($result)){
?>

DISPLAY ROWS HERE!!!!

<?php

} // end while 

  
/******  build the pagination links ******/
// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <center><a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a></center> ";
} // end if


// range of num links to show
$range = 3;

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range)  + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo "<center> <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> </center>";
      } // end else
   } // end if 
} // end for


// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 
   echo "<center><a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>
   <font color=#6d80f6> ></font></a>  ";

        // echo forward link for last page 
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>
   <font color=red> >> </font></a> </center><br>";
} // end if
/****** end build pagination links ******/


mysql_close(); //close database
?>

Link to comment
Share on other sites

<?php

$host="localhost";
$username="";
$password="";
$db_name="guestbook";
$tbl_name="gb";

// Connect to server and select database.
$conn = mysql_connect("$host", "$username", "$password")or die("cannot connect to the server ");
$select_db = mysql_select_db("$db_name",$conn)or die("cannot select the Data Base");

$sql="SELECT COUNT(*) FROM $tbl_name";
$result=mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$rows = mysql_fetch_row($result);
$num_of_rows = $r[0];
$rowsperpage = 2;
$totalpages = ceil($num_of_rows / $rowsperpage);


// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if


// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

$offset = ($currentpage - 1) * $rowsperpage;


$sql = "SELECT name, datetime, email, comment, id, comment FROM $tbl_name LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);


//while ($list = mysql_fetch_assoc($result)) {

while($rows=mysql_fetch_assoc($result)){
?>

DISPLAY ROWS HERE!!!!

<?php

} // end while 

  
/******  build the pagination links ******/
// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <center><a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a></center> ";
} // end if


// range of num links to show
$range = 3;

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range)  + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo "<center> <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> </center>";
      } // end else
   } // end if 
} // end for


// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 
   echo "<center><a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>
   <font color=#6d80f6> ></font></a>  ";

        // echo forward link for last page 
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>
   <font color=red> >> </font></a> </center><br>";
} // end if
/****** end build pagination links ******/


mysql_close(); //close database
?>

 

 

Well i posted in the other thread but it says solved so i guess people won't look there,

 

Like i said in the other thread this works just fine except  when i click next page it just shows the main page the first one... I don't know what I did wrong

Link to comment
Share on other sites

what the hell!!!! i see it working fine in your server!!!!!! how come in mine it just stays in the same page like here.

 

http://74.170.18.102/gb

 

 

maybe is something in my tables here:

THIS is inside my while loop

 

<center><table class=gb_table1 style="border-collapse:collapse; border:solid 1px #999999;" > <tr> 
<td style=>

<div class=gbdiv  >
<table style="border-collapse:collapse; ">
<tr>
<td style="background:#202020; color:white; font-size:11px; font-weight:bold;" align=left> 
Date</td> 

<td style="background:#202020; color:white; font-size:11px; font-weight:bold;" align=left> 
: </td>

<td style="background:#202020; color:#cccccc; font-size:11px; font-weight:100;padding-left:10px;"> 
<?php echo $rows['datetime']; ?> 

</td> </tr>

<td style="background:#202020; color:white; font-size:11px; font-weight:bold;" align=left> 
Name</td>

<td style="background:#202020; color:white; font-size:11px; font-weight:bold;" align=left> 
: </td>

<td style="background:#202020; color:green; font-size:13px; font-weight:100;padding-left:10px;"> 
<?php echo $rows['name']; ?> 

</td></tr>

<tr>
<td style="background:#202020; color:white; font-size:11px; font-weight:bold;" align=left> 
Number </td>

<td style="background:#202020; color:white; font-size:11px; font-weight:bold;" align=left> 
: </td>

<td style="background:#202020; color:red; font-size:11px; font-weight:100;padding-left:10px;"> 
<?php echo $rows['id']; ?> </td></tr>

</table>


<tr><td class="comment" style="background:#333333; padding:5px; 
font-size:14px; color:white; " > 

<?php echo $rows['comment']; ?>   

</td></tr>

</div>




</td> </tr>

</td> </tr> </table > </center> 

 

 

Link to comment
Share on other sites

i only used the pagination code and the required values for it. i removed all references to database stuff. your problem must lie in there.

 

this is the code on my server:

<?php

$sql="SELECT COUNT(*) FROM $tbl_name";
$rows = 16;
$num_of_rows = $rows;
$rowsperpage = 2;
$totalpages = ceil($num_of_rows / $rowsperpage);


// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if


// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

$offset = ($currentpage - 1) * $rowsperpage;

/******  build the pagination links ******/
// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <center><a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a></center> ";
} // end if


// range of num links to show
$range = 3;

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range)  + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo "<center> <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> </center>";
      } // end else
   } // end if 
} // end for


// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 
   echo "<center><a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>
   <font color=#6d80f6> ></font></a>  ";

        // echo forward link for last page 
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>
   <font color=red> >> </font></a> </center><br>";
} // end if
/****** end build pagination links ******/

?>

 

PS: I saw $num_of_rows = $r[0]; in yur original code. I didn't know what that was but I saw $num_of_rows was required for the pagination calculations. So I just made $num_of_rows = $rows and $rows = count of how many rows are returned from database. in my code it is just a fixed integer of 16.

Link to comment
Share on other sites

i only used the pagination code and the required values for it. i removed all references to database stuff. your problem must lie in there.

 

this is the code on my server:

<?php

$sql="SELECT COUNT(*) FROM $tbl_name";
$rows = 16;
$num_of_rows = $rows;
$rowsperpage = 2;
$totalpages = ceil($num_of_rows / $rowsperpage);


// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if


// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

$offset = ($currentpage - 1) * $rowsperpage;

/******  build the pagination links ******/
// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <center><a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a></center> ";
} // end if


// range of num links to show
$range = 3;

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range)  + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo "<center> <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> </center>";
      } // end else
   } // end if 
} // end for


// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 
   echo "<center><a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>
   <font color=#6d80f6> ></font></a>  ";

        // echo forward link for last page 
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>
   <font color=red> >> </font></a> </center><br>";
} // end if
/****** end build pagination links ******/

?>

 

PS: I saw $num_of_rows = $r[0]; in yur original code. I didn't know what that was but I saw $num_of_rows was required for the pagination calculations. So I just made $num_of_rows = $rows and $rows = count of how many rows are returned from database. in my code it is just a fixed integer of 16.

 

But you made rows =16;  because that is how many rows they are on that table, so if there are more rows it won't show them it would just stop at row 16... and i want to show new entered rows, any one else might know how to fix this?

Link to comment
Share on other sites

After going tru the whole code one more time I finally found out what it was, thanks to you catfish  ;) i took a look at the portion you told me and figured it out, I was like wtf is $r if I have not declared it anywhere, and then i noticed that $r was incomplete it was supposed to be $rows  xD so here it is

 

 
$num_of_rows = $r[0]; 

 

 

that line should read

 
$num_of_rows = $rows[0];

 

xD thanks catfish [ SOLVED ]

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.