Hi im now making a simple guestbook. The problem is about displaying the number of comments . The maximum of commecnts to be display is 4. And i have a button. First, Previous, Next, Last. I have an idea but dont know how to start with it
| SitePoint Sponsor |
Hi im now making a simple guestbook. The problem is about displaying the number of comments . The maximum of commecnts to be display is 4. And i have a button. First, Previous, Next, Last. I have an idea but dont know how to start with it
What do you have so far?
I think it's just a case of some simple pagination.
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
Actually im not started to code yet. Im just planning and thinking of the problem that i may take. And the difficult one is the navigation of displaying the comments. The maximum number of comments to be displayed must be 4. About navigation i have 4 buttons like i said its First,Prev,Nxt,Lst.

Yup, that's pagination. It's quite fun to figure out by yourself, although if you're new, it can be a bit frustrating as well. There's a similar question on that a bit earlier. Lemme fetch the link...

What I would do is have a mysql table for the comments. Use this query to fetch the results. increasing $_GET['commentpage'] will change the page. If $_GET['commentpage'] is not set, then it defaults to 0.
Make the next button add 1 to $_GET['commentpage'] on the url, for example:PHP Code:$page = (isset($_GET['commentpage'])) ? $_GET['commentpage'] : 0;
$start = $page * 4;
$q = mysql_query("SELECT * FROM `comments` LIMIT $start,4");
PHP Code:<?
$nextpage = $page + 1;
echo "<a href=\"?commentpage=".$nextpage."\">Next</a>";
?>
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
Ok let me try your code and also i try my own code even its too long using a for loop.
This is the code i use to display the comments, who post it, and the date posted it.
But how im gonna limit that into 4 contents only and the other post must be in the other page when i click the button next or previous or last or first.PHP Code:<?php
$getdata="SELECT * FROM comments ORDER BY com_id DESC";
$getdata2=mysql_query($getdata) or die("Could not get data");
while($getdata3=mysql_fetch_array($getdata2))
{
$getdata3[com_name]=strip_tags($getdata3[com_name]);
$getdata3[com_message]=strip_tags($getdata3[com_message]);
$getdata3[com_date]=strip_tags($getdata3[com_date]);
?>
<div class="headinglarge"> Title <img src="images/del.gif"></div><div class="adminbtn"><img src="images/admin.gif" width="65" height="52" border="0" ></div>
<div class="headingsub"> <?php "$getdata3[com_date]"; ?> by <?php echo "$getdata3[com_name]"; ?></div>
<div class="content"> <?php echo "$getdata3[com_message]"; ?>
</div><hr>
<?php } ?>

Use limit in your SQL query.
I place the Limit 4. on my sql variable. but what if i press the next button or previous button should i place a limit 4 also. but how im gonna attain that if i meet the last record the next button is not enable

You got to make a variable for the first and last number. It won't be LIMIT 0,4, but LIMIT $start, $stop. You change $start and $stop according to the button pressed (Next/Previous/Last/First)
Ok but before that how im gonna display an image inside the php code
This is not workingPHP Code:<?php
for ($i=0; $i>"$getdata3[com_rating]"; $i++)
{
echo <img src="star.gif">
}
?>

It doesn't show star.gif any time, I bet. Read your for loop aloud:
i is 0. While i is bigger than getdata3[com_rating], draw star.gif, then increase i by one.
That would require get_data3 to be negative in value, and if that would be the case, it would loop endlessly.
oh ! i wrote a wrong for loop. It should be "<=" not ">". The reason the image doesn't show because i put a double quote of it
so, your fixed code there would be:
PHP Code:<?
for($i = 0; $i < $getdata3; $i++){
?><img src="star.gif" alt="star" /><?
}
?>
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona

Uhmmm, I see. Dunno how I didn't see this before.
Try it like that. The quotes should be around the associative array key, not the entire variable.Code:<?php for ($i=0; $i<$getdata3["com_rating"]; $i++) { echo '<img src="star.gif">'; } ?>
Imstuck with the buttons
in the images i fixed it already but in the buttons im stuck.

Can you show us the code that's giving trouble?
I know this is wrong but im still getting the idea of how to to thatPHP Code:
<?php
for ($x=1; $x>"getdata3[com_id]"; $x++)
{
?>
<table width="119" border="0" align="center" cellpadding="0" cellspacing="3">
<tr>
<td width="16" align="right"><?php $x; ?><img src="images/pfirst.gif" width="15" height="16"></td>
<td width="16" align="right"><?php $x; ?><img src="images/pprev.gif" width="15" height="16"></td>
<td width="7" align="center"><?php $x; ?>1</td>
<td width="16" align="left"><?php $x; ?><img src="images/pnext.gif" width="15" height="16"></td>
<td width="46" align="left"><?php $x; ?><img src="images/plast.gif" width="15" height="16"></td>
</tr>
</table>
<?php } ?>
don't put quotes around variables.
change
toPHP Code:$x>"getdata3[com_id]";
PHP Code:$x>$getdata3["com_id"];
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
But is it my code do the displaying 4 by 4 . if i click it will go to the 4 comments again and if previous the vice versa. and so on
Im really stuck with it. Dont know what to do with my buttons
PHP Code:<?php
$getdata="SELECT * FROM comments ORDER BY com_id DESC LIMIT 0,4";
$getdata2=mysql_query($getdata) or die("Could not get data");
while($getdata3=mysql_fetch_array($getdata2))
{
$getdata3[com_name]=strip_tags($getdata3[com_name]);
$getdata3[com_rating]=strip_tags($getdata3[com_rating]);
$getdata3[com_message]=strip_tags($getdata3[com_message]);
$getdata3[com_date]=strip_tags($getdata3[com_date]);
?>
<div class="headinglarge"> Title <?php
for ($i=1; $i<="$getdata3[com_rating]"; $i++)
{
echo '<img src="images/star.gif">';
}
?><img src="images/del.gif"></div><div class="adminbtn"><img src="images/admin.gif" width="65" height="52" border="0" ></div>
<div class="headingsub"> <?php echo "$getdata3[com_date]"; ?> by <?php echo "$getdata3[com_name]"; ?></div>
<div class="content"> <?php echo "$getdata3[com_message]"; ?>
</div><hr>
<?php } ?>
<table width="119" border="0" align="center" cellpadding="0" cellspacing="3">
<tr>
<td width="16" align="right"><img src="images/pfirst.gif" width="15" height="16"></td>
<td width="16" align="right"><img src="images/pprev.gif" width="15" height="16"></td>
<td width="7" align="center">1</td>
<td width="16" align="left"><img src="images/pnext.gif" width="15" height="16"></td>
<td width="46" align="left"><img src="images/plast.gif" width="15" height="16"></td>
</tr>
</table>
read post 6
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
what is this all about ?
PHP Code:(isset($_Get['commentpage']))
Bookmarks