Search
Left Quote    Nearly all men can stand adversity, but if you want to test a man's character, give him power.
- Abraham Lincoln    
Right Quote
 
[login] | [Register]
Forum Index » PHP and mySQL » Viewing Topic and Replies
Post Reply


        Viewing Topic: Pagnation in Comment Script
  This user is offline  gravy
  Subject: "Pagnation in Comment Script" Posted: @ 9:55 pm on Mar 10 2007   

Member #: 294
Rank: User - (63)
Since: 03/07/07
Posts: 66
From: MN

Is this even possible, cause I have the biggest headache trying to figure out how to connect to the data base and bring back the appropriate info to split the pages.

I don't know, maybe I have to whole thing screwed up.

And after reading through the tutorial I am still confused on the LIMIT part of it. If you set the $limit to 10 (which is the default) it will show 10 comments before making a new page?

I have a headache.... Here is my code....


Code:

<?
//query comments for this page of this article
if(isset($_SESS ION['username'] ) && isset($_SESSION ['password'])) {
$inf = "SELECT * FROM `comments` WHERE page = '".stripsl ashes($_SERVER[ 'REQUEST_URI']) ."' ORDER BY time ASC";
$info = mysql_query($in f);
if(!$info) die(mysql_error ());
}
$info_rows = mysql_num_rows( $info);
if($info_rows > 0) {
echo '';
echo '<table width="100 %" align="cen ter" border="1& quot; cellpadding=&qu ot;3" cellspacing=&qu ot;3" bordercolor=&qu ot;#808080" ; style="bor der-collapse: collapse"& gt;';

while($info2 = mysql_fetch_obj ect($info)) {
echo '<tr>';
echo '<td bgcolor="# cbd7e2"> ;<font size="1&qu ot; face="verd ana">&q uot;'.stripslas hes($info2-> subject).'" ; by: <b>'.stri pslashes($info2 ->username). '</b>< /a></td&g t; <td bgcolor="# cbd7e2"> ;<font size="1&qu ot; face="verd ana">&l t;div align="rig ht">Add ed on '.$info2->da te.'</div> ;</td>';
echo '</tr>< ;tr>';
echo '<td colspan="2 " bgcolor="# e9edf3"> ;<font size="2&qu ot; face="verd ana">&l t;br><div align="lef t"> '.stripslashes( $info2->comm ent).' </div>< ;br></td& gt;';
echo '</tr>';
}//end while

echo '</table> ';
} else echo '<font size="2&qu ot; face="verd ana">&l t;b>No comments for this photo. Feel free to be the first! <br>';

if(isset($_POS T['submit'])) {
if(!addslashes( $_POST['usernam e'])) die('<u>E RROR:</u> you must enter a username to add a comment.');
if(!addslashes( $_POST['subject '])) die('<u>E RROR:</u> enter a subject to your comment.');
if(!addslashes( $_POST['comment '])) die('<u>E RROR:</u> cannot add comment if you do not enter one!?');





//add comment
$q ="INSERT INTO `comments` (article_id, page, date, time, username, contact, ip, subject, comment) VALUES ('".$_GET[ 'id']."', '".$_POST[ 'page']."' , '".$_POST[ 'date']."' , '".$_POST[ 'time']."' , '".addslas hes(htmlspecial chars($_POST['u sername'])).&qu ot;', '".$_SERVE R['REMOTE_ADDR' ]."', '".addslas hes(htmlspecial chars($_POST['c ontact'])).&quo t;', '".addslas hes(htmlspecial chars($_POST['s ubject'])).&quo t;', '".addslas hes(htmlspecial chars(nl2br($_P OST['comment']) ))."')&quo t;;

$q2 = mysql_query($q) ;
if(!$q2) die(mysql_error ());

//refresh page so they can see new comment
//header(" Location: $info2";
echo ("<font size='2' face='verdana' color='blue'> ;<b>Give me a second.....Inse rting Comment";
echo ("<meta http-equiv='ref resh' content='2;URL= $_SERVER[HTTP_R EFERER]'>&qu ot;

$t = mysql_query(&qu ot;SELECT * FROM `comments` WHERE `article_id` = '".addslas hes($_GET['cat' ])."'" ;
if(!$t) die(mysql_error ());

$a = mysql_fetch_obj ect($t);
$total_items = mysql_num_rows( $t);
$limit = $_GET['limit'];
$type = $_GET['type'];
$page = $_GET['page'];

//set default if: $limit is empty, non numerical, less than 10, greater than 50
if((!$limit) || (is_numeric($li mit) == false) || ($limit < 10) || ($limit > 50)) {
$limit = 10; //default
}
//set default if: $page is empty, non numerical, less than zero, greater than total available
if((!$page) || (is_numeric($pa ge) == false) || ($page < 0) || ($page > $total_items)) {
$page = 1; //default
}

//calcuate total pages
$total_pages = ceil($total_ite ms / $limit);
$set_limit = $page * $limit - ($limit);

//query: **EDIT TO YOUR TABLE NAME, ECT.

$q = mysql_query(&qu ot;SELECT * FROM `comments` WHERE `article_id` = '".addslas hes($_GET['cat' ])."' LIMIT $set_limit, $limit";
if(!$q) die(mysql_error ());
$err = mysql_num_rows( $q);
if($err == 0) die("<b r>No matches met your criteria."< img src=http://www. allsyntax.com/f orums/images/sm ilies/wink.gif border=0>;

//Results per page: **EDIT LINK PATH**
echo("
<a href=comment.ph p?cat=$cat& amp;limit=10&am p;amp;page=1> ;10</a> |
<a href=comment.ph p?cat=$cat& amp;limit=25&am p;amp;page=1> ;25</a> |
<a href=comment.ph p?cat=$cat& amp;limit=50&am p;amp;page=1> ;50</a>&q uot;

//show data matching query:
while($code = mysql_fetch_obj ect($q)) {
echo("item : ".$code-&g t;title."& lt;BR>"< img src=http://www. allsyntax.com/f orums/images/sm ilies/wink.gif border=0>;
}

$cat = urlencode($cat) ; //makes browser friendly

//prev. page: **EDIT LINK PATH**

$prev_page = $page - 1;

if($prev_page >= 1) {
echo("< b>&lt;&a mp;lt;</b> ; <a href=http://com ment.php?cat=$c at&amp;limi t=$limit&am p;page=$prev_pa ge><b> Prev.</b> </a>" ;
}

//Display middle pages: **EDIT LINK PATH**

for($a = 1; $a <= $total_pages; $a++)
{
if($a == $page) {
echo("< b> $a</b> | "; //no link
} else {
echo(" <a href=http://com ment.php?cat=$c at&amp;limi t=$limit&am p;page=$a> $a </a> | ";
}
}

//next page: **EDIT THIS LINK PATH**

$next_page = $page + 1;
if($next_page <= $total_pages) {
echo("< a href=http://com ment.php?cat=$c at&amp;limi t=$limit&am p;page=$next_pa ge><b> Next</b>& lt;/a> &gt; &gt;";
}

} else { //display form
?>
</td>
</tr>
</table>< br /> <?
} // end else
?>




Photo Hosting Scripts? It's what I do....
    Viewed: 5,460 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Pagnation in Comment Script" Posted: @ 11:02 pm on Mar 10 2007    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

Yea, $limit is the max number of results for the query and $set_limit is the number to start at - to either begin 1 through 10, or pick up where you left off if you are not on page 1 any longer.

Also, you dont have to make any pages, just 1 page for pagination. You query the same page with a different request like: yoursite.com/pa ginate.php?page =2&limit=10

So the pagination script will show whatever you want based on the inputs (page number, limit)




-bs0d | AllSyntax.com

    Viewed: 5,455 Times | Reply to This | To top
  This user is offline  gravy
  Subject: "re: Pagnation in Comment Script" Posted: @ 6:36 pm on Mar 11 2007    

Member #: 294
Rank: User - (63)
Since: 03/07/07
Posts: 66
From: MN

yup that did it, works like a charm, thanks for the explaination!




Photo Hosting Scripts? It's what I do....

    Viewed: 5,436 Times | Reply to This | To top
Viewing Page: 1 of 1


1 |

You must be logged in to post on the forums. Login or Register








"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank