Jump to content

Whats wrong with my pagination code


liam1412

Recommended Posts

Hi

I have tried to integrate pagination into my forum and its throwing back a whole load of undefined index errors etc.

Please can you help


This is what I have



[code]while
($fetch_replies = mysql_fetch_array($sql_answer_result)){
if($_GET['page'])
{
$page = $_GET['page'];
}else{
$page = 1;
}
$max = 20;
$cur = (($page * $max) - $max);
$total_rows = mysql_num_rows($sql_answer_result);
$total_pages = ceil($total_rows / $max);

for($i = 1; $i <= $total_pages; $i++)
{
if($page == $i)
{
echo '<b>".$i."</b>';

}else{

echo '<a href="?page='.$next.'">Next ></a>';

}
}

$ans_text = $fetch_replies['ans_text'];
$ans_id = $fetch_replies['ans_id'];
$ans_topic_id = $fetch_replies['ans_topic_id'];
$ans_text = nl2br($ans_text);
$ans_text = BBCODE($ans_text);
?>
<tr>
    <td valign="center" width="150" bgcolor="#565656"><p class="grey">
<?php echo $fetch_replies['ans_datetime'];?>
<br />
<br />
<?php echo $fetch_replies['ans_poster_username'];?>
<br />
<img src="images/profilepics/vw.jpg" width="50" heigth="50" border="2" />
</p></td>
    <td valign="top" width="450" bgcolor="#6d6d6d">
<table width="450" cellspacing="0" cellpadding="0" border="0">
<tr>
    <td colspan="2"><p class="grey"><?php echo $ans_text; ?></p></td>
</tr>
<tr>
    <td width="350" align="right"></td>
    <td width="100" align="right">
<br />
<br />
<br />
<br />
<?php if($fetch_replies['ans_poster_username'] == $_SESSION['username']){
?>
<a href="edit_reply.php?ans_id=<?php echo $ans_id ?>&ans_topic_id=<?php echo $ans_topic_id; ?>"><img src="images/edit_post.jpg" width="25" height="25" border="0" alt="Edit Reply" /></a>&nbsp;&nbsp;
    <?php
};
?>
</td>
</tr>
</table>
</td>
</tr>[/code]
Link to comment
Share on other sites

Okay this is my entire page.

I have found a couple of errors and edited I now have one error.

Its saying that the index page is undefined on line 165
and then it is echoing

".$i."

This i repeated 3 times as this is how many record it is retrieving.


[code]<?php
session_start();
include 'header.html';
$topic_id = $_GET['topic_id'];

function BBCODE($bbcode)
{
//the bbcode tags..
$bbc_a=array(
"[strike]",
"[/strike]",
"[b]",
"[/b]",
"[u]",
"[/u]",
"[big]",
"[/big]",
"[huge]",
"[/huge]",
"[e_mail]",
"[/e_mail]",
"[url]",
"[/url]",
"::smile::",
"::mad::",
"::sad::",
"::rolleyes::",
"::smirk::",
"::wink::",
"::surprised::",
"::thinking::",
"::tongue::",
"::cool::",
);

//bbcode gets converted to..
$bbc_b=array(
"<strike>",
"</strike>",
"<b>",
"</b>",
"<u>",
"</u>",
"<font size=4>",
"</font>",
"<font size=8>",
"</font>",
"<a href=\"mailto:",
"\">E-MAIL LINK</a>",
"<a href=\"",
"\" target=\"blank\">WEB LINK</a>",
"<img src='images/smilies/smile.gif' />",
"<img src='images/smilies/mad.gif' />",
"<img src='images/smilies/sad.gif' />",
"<img src='images/smilies/rolleyes.gif' />",
"<img src='images/smilies/smirk.gif' />",
"<img src='images/smilies/wink.gif' />",
"<img src='images/smilies/surprised.gif' />",
"<img src='images/smilies/thinking.gif' />",
"<img src='images/smilies/tongue.gif' />",
"<img src='images/smilies/cool.gif' />",
);

$bbc_num=count($bbc_a);
$loop=0;
while($loop<$bbc_num)
{
$bbcode=str_replace($bbc_a[$loop], $bbc_b[$loop], $bbcode);
$loop++;
}

return $bbcode;


$host = 'localhost';
$db_username = 'root';
$db_password = '';
$db_name = 'klubdeutsch';
$table_name_topic = 'forum_question';
$table_name_answer = 'forum_answer';
$user_level = '1';

mysql_connect("$host" , "$db_username" , "$db_password")or die("Cannot Connect to Server");
mysql_select_db("$db_name")or die("Cannot connect to Databse");

$sql_topic_query = "SELECT * FROM forum_question WHERE topic_id = $topic_id";
$sql_get_topic = mysql_query($sql_topic_query);
$sql_fetch_topic = mysql_fetch_array($sql_get_topic);

$topic_id = $sql_fetch_topic['topic_id'];
$topic = $sql_fetch_topic['topic'];
$topic_text = $sql_fetch_topic['topic_text'];
$topic_text = nl2br($topic_text);
$topic_text = BBCODE($topic_text);
$topic_start_datetime = $sql_fetch_topic['topic_start_datetime'];
$topic_started_by = $sql_fetch_topic['topic_starter_name'];
$topic_view = $sql_fetch_topic['topic_view'];
$topic_view = ($topic_view + 1);
$topic_reply = $sql_fetch_topic['topic_reply'];

$sql_answer_query = "SELECT * FROM forum_answer WHERE ans_topic_id = '$topic_id' ORDER BY ans_id";
$sql_answer_result = mysql_query($sql_answer_query);

$sql_no_posts_query = "SELECT * FROM forum_question WHERE topic_id = '$topic_id'";
$sql_no_posts_result = mysql_query($sql_no_posts_query);
$no_posts_result = mysql_fetch_array($sql_no_posts_result);
$no_of_posts = $no_posts_result['topic_reply'];
$no_of_views = $no_posts_result['topic_view'];
$topic_status = $no_posts_result['topic_status'];

if($topic_status == 0){
$topic_update = 4;}
elseif($topic_status == 1){
$topic_update = 5;}
elseif($topic_status == 2){
$topic_update = 6;}
elseif($topic_status == 3){
$topic_update = 7;}
?>

<br />
<table align="center" width="600" cellspacing="2" cellpadding="0" border="0">
<tr>
    <td width="500"><p class="header2"><?php echo $topic; ?></p></td>
<td align="right" width="100"><a href="post_reply.php?topic_id=<?php echo $topic_id?>" onmouseover="chgImg('postreply','On')" onmouseout="chgImg('postreply','Off')"><img src="images/postreplyOff.jpg" width="100" border="0" name="postreply"></a></td>
</tr>
</table>
<table class="border" align="center" width="600" cellspacing="2" cellpadding="3" border="0">
<tr>
    <td valign="top" width="150" bgcolor="#565656"><p class="grey">
<?php echo $topic_start_datetime ?>
<br />
<br />
<?php echo $topic_started_by ?><br />
<img src="images/profilepics/bmw.jpg" width="50" height="50" border="2" />
<br />
</td>
    <td colspan="2" valign="top" width="450" bgcolor="#6d6d6d">
<table width="450" cellspacing="0" cellpadding="0" border="0">
<tr>
    <td colspan="2"><p class="grey"><?php echo $topic_text; ?></p></td>
</tr>
<tr>
<td width="410"><br /></td>
    <td width="40" align="right">
<br />
<br />
<br />
<br />
<?php
if($topic_started_by == $_SESSION['username']){
?>
<a href="edit_topic.php?topic_id=<?php echo $topic_id ?>"><img src="images/edit_post.jpg" width="25" height="25" border="0" alt="Edit Post" /></a>&nbsp;&nbsp;
    <?php
};
?>
</td>
</tr>
</table>
</td>

<?php
while
($fetch_replies = mysql_fetch_array($sql_answer_result)){
if($_GET['page'])
{
$page = $_GET['page'];
}else{
$page = 1;
}
$max = 20;
$cur = (($page * $max) - $max);
$total_rows = mysql_num_rows($sql_answer_result);
$total_pages = ceil($total_rows / $max);

for($i = 1; $i <= $total_pages; $i++)
{
if($page == $i)
{
echo '<b>".$i."</b>';

}else{

echo '<a href="?page='.$next.'">Next ></a>';

}
}

$ans_text = $fetch_replies['ans_text'];
$ans_id = $fetch_replies['ans_id'];
$ans_topic_id = $fetch_replies['ans_topic_id'];
$ans_text = nl2br($ans_text);
$ans_text = BBCODE($ans_text);
?>
<tr>
    <td valign="center" width="150" bgcolor="#565656"><p class="grey">
<?php echo $fetch_replies['ans_datetime'];?>
<br />
<br />
<?php echo $fetch_replies['ans_poster_username'];?>
<br />
<img src="images/profilepics/vw.jpg" width="50" heigth="50" border="2" />
</p></td>
    <td valign="top" width="450" bgcolor="#6d6d6d">
<table width="450" cellspacing="0" cellpadding="0" border="0">
<tr>
    <td colspan="2"><p class="grey"><?php echo $ans_text; ?></p></td>
</tr>
<tr>
    <td width="350" align="right"></td>
    <td width="100" align="right">
<br />
<br />
<br />
<br />
<?php if($fetch_replies['ans_poster_username'] == $_SESSION['username']){
?>
<a href="edit_reply.php?ans_id=<?php echo $ans_id ?>&ans_topic_id=<?php echo $ans_topic_id; ?>"><img src="images/edit_post.jpg" width="25" height="25" border="0" alt="Edit Reply" /></a>&nbsp;&nbsp;
    <?php
};
?>
</td>
</tr>
</table>
</td>
</tr>

<?php
}
if($no_of_views == 199){
$sql_hot_topic = "UPDATE forum_question SET topic_status = $topic_update WHERE topic_id = '$topic_id'";
mysql_query($sql_hot_topic)or die("unable to update topic status");
}
$sql_update_topic_view = "UPDATE forum_question SET topic_view = '$topic_view' WHERE topic_id = '$topic_id'";
mysql_query($sql_update_topic_view)or die("failed to update topic_reply");
mysql_close();
?>

[/code]


Link to comment
Share on other sites

It seems like a couple of posts out of my thread have gon??????


The problem I am now having is this

My limit is set to 10.  There are 13 record in my query but the pagination reads.

PREV 1 NEXT 

None of them are links. Its acting like there are less than 10 records. GRRRRRRRRRRRRRRRRRRRR

[code]<?php
$limit = 10;
$query_count = "SELECT count(*)FROM forum_answer WHERE ans_topic_id = $topic_id";
$result_count = mysql_query($query_count);
$total_rows = mysql_num_rows($result_count);

if(empty($page)){
$page = 1;
}

$limitvalue = $page * $limit - ($limit);

$topic_query = "SELECT * FROM forum_answer WHERE ans_topic_id = $topic_id ORDER BY ans_id LIMIT $limitvalue, $limit";
$topic_result = mysql_query($topic_query)or die("Error: " . mysql_error());


while
($fetch_replies = mysql_fetch_array($topic_result)){
$ans_text = $fetch_replies['ans_text'];
$ans_id = $fetch_replies['ans_id'];
$ans_topic_id = $fetch_replies['ans_topic_id'];
$ans_text = nl2br($ans_text);
$ans_text = BBCODE($ans_text);
?>
<tr>
    <td valign="center" width="150" bgcolor="#565656"><p class="grey">
<?php echo $fetch_replies['ans_datetime'];?>
<br />
<br />
<?php echo $fetch_replies['ans_poster_username'];?>
<br />
<img src="images/profilepics/vw.jpg" width="50" height="50" border="2" />
</p></td>
    <td valign="top" width="450" bgcolor="#6d6d6d">
<table width="450" cellspacing="0" cellpadding="0" border="0">
<tr>
    <td colspan="2"><p class="grey"><?php echo $ans_text; ?></p></td>
</tr>
<tr>
    <td width="350" align="right"></td>
    <td width="100" align="right">
<br />
<br />
<br />
<br />
<?php if($fetch_replies['ans_poster_username'] == $_SESSION['username']){
?>
<a href="edit_reply.php?ans_id=<?php echo $ans_id ?>&ans_topic_id=<?php echo $ans_topic_id; ?>"><img src="images/edit_post.jpg" width="25" height="25" border="0" alt="Edit Reply" /></a>&nbsp;&nbsp;
    <?php
};
?>
</td>
</tr>
</table>
</td>
</tr>
<?php

}
if($page != 1){
$pageprev = $page--;
echo("<a href=\"$PHPSELF&page=$pageprev\">PREV</a> ");
}else{

echo ("PREV");

$numofpages = $total_rows / $limit;
for($i = 1; $i <= $numofpages; $i++){
  if($i == $page){
            echo($i." ");
        }else
            echo("<a href=\"$PHP_SELF&page=$i\">$i</a> "); 
        }
}
if(($total_rows % $limit) != 0){
  if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF&page=$i\">$i</a> ");
        }
}

if(($total_rows - ($limit * $page)) > 0){
  $pagenext  = $page++;
  echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT</a>");
}else{
      echo("NEXT"); 
 
}
if($no_of_views == 199){
$sql_hot_topic = "UPDATE forum_question SET topic_status = $topic_update WHERE topic_id = '$topic_id'";
mysql_query($sql_hot_topic)or die("unable to update topic status");
}
$sql_update_topic_view = "UPDATE forum_question SET topic_view = '$topic_view' WHERE topic_id = '$topic_id'";
mysql_query($sql_update_topic_view)or die("failed to update topic_reply");
mysql_close();
?>
[/code]

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.