Jump to content

[SOLVED] getting to last post via a link (pagestation set up on the page)


runnerjp

Recommended Posts

i have pageistation set up for my forum pages displaying 15 links per page...

 

i have created a side bar for all my pages showing the latest 10 posts in my forum...

 

the thing is how do i use the link to send the users to the last page of the thread??

 

here is a snippit showing my pageistation

 

<?php for($i = 1; $i <= $numofpages; $i++){
if($i == $pagenum){
echo "[".$i."] ";
}else{
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=message&id=$id&pagenum=$i\">$i</a> ";
}
}

// Check for straglers after the limit blocks
if(($rows % $page_rows) != 0){
if($i == $pagenum){
echo "[".$i."] ";
}else{
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=message&id=$id&pagenum=$i\">$i</a> ";
}
}
// Print out the Next 25 and Goto Last page links
if(($rows - ($page_rows * $pagenum)) > 0){
$pagenext = $pagenum++;
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=message&id=$id&pagenum=$pagenext\">NEXT </a>  ";
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=message&id=$id&pagenum=$totalpages\">>></a>  "; ?>

 

you see to go to the last page on the page itself is easy as its set to $totalpages = ceil($numofpages);  but how can i do this through a link on anouther page??

 

currently my link looks liek this

 

<a id="posts" href="http://www.runningprofiles.com/members/index.php?page=message&forum='.$row['forum'].'&id='.$row['postid'].'">'.$row['title'].'</a>

Link to comment
Share on other sites

Why don't you add an option that if $_GET['pagenum'] is set to the string "last" or to the integer 0 (Simply choose something and be consistent with it) it'd send you to the last page? This way you don't have to make a query in your sidebar for each thread to find out how many pages are there. You'll leave the work to the script that shows the thread.

 

Orio.

Link to comment
Share on other sites

ok so your saying set my link as pagenum=last... then on the page i would do

 

but where would i add it to my page :S

 

id suspect something around here $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;... but what would it be...

 

if (pagenum==last){ some how send to last page via $totalpages } :S... code is in full below

 

<link rel="stylesheet" type="text/css" href="http://www.runningprofiles.com/css/login.css">
<link rel="stylesheet" type="text/css" href="http://www.runningprofiles.com/members/include/style.css">

<?php
include "connect.php"; //mysql db connection here
session_start();
include "../BBClass.php";
$_SESSION['SecretFormPW'] = "runningprofiles";
$id=$_GET['id'];
$id1 = $_SESSION['user_id'];  
$timestamp = time();
$timeout = $timestamp - 180;
$username= get_username($_SESSION['user_id']);
function selfURL() { 
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s; 
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); 
return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']; } 
function strleft($s1, $s2) { return substr($s1, 0, strpos($s1, $s2)); }
$filename = (selfURL());
//Insert User
$insert = mysql_query("REPLACE INTO `useronline` SET `timestamp`='$timestamp', `ip`='".$_SERVER['REMOTE_ADDR']."', `file`='$filename',`user`='$username',`user_id`='".$_SESSION['user_id']."'") 
or die(mysql_error()); 
$pagenum = isset($_GET['pagenum']) ? $_GET['pagenum'] : "1";

$gettopic="SELECT * from forumtutorial_posts where postid='$id'";
$gettopic2=mysql_query($gettopic) or die("Could not get topic");
$gettopic3=mysql_fetch_array($gettopic2);
?>

<?php

//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * from forumtutorial_posts where parentid='$id'") or die(mysql_error());
$rows = mysql_num_rows($data);

//This is the number of results displayed per page
$page_rows = 4;

//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
//This is your query again, the same one... the only difference is we add $max into it
{
?>
<?
}?>
<p><A href='index.php?page=mainforums'><img src="http://www.runningprofiles.com/images/homeforum.gif" alt="home" border="0"  /> </a><A href='index.php?page=forum&forum=<?php echo $forum;?>'> <img src="http://www.runningprofiles.com/images/backforum.gif" border="0"  /> </a>
<?php
if($gettopic3['forumlock'] == 0)
{
     ?>
<a href='index.php?page=reply&id=<? echo $id ?>'><img src="http://www.runningprofiles.com/images/post_reply.gif" border="0"  /></a>
<? }?>
</p>
<p>users viewing forum: <? $result = mysql_query("SELECT * FROM useronline WHERE(file='http://www.runningprofiles.com/members/index.php?page=message&forum=$forum&id=$id')");
while($row = mysql_fetch_array( $result )) {

$last_active = time() - $row['timestamp'];
$onlineuser = $row['user'];
}

if($last_active < 300) {
echo $onlineuser;
}
?></p>
<table class='maintable'>

<tr class='headline'><td width=11%>Author</td>
<td width=89%>Post</td>
</tr> <?php
if ($pagenum == 1){
?>
<tr class='mainrow'><td valign='top'><div align="center"><?php //We need to add a Little variable that will automatically increase so that
//Each div (container) in the editforum.php script is unique:
$CONT_ID = 1;
echo $gettopic3['author'] ;?></div>
  <p align="center">
  <?


$query = "SELECT * FROM users WHERE Username = '".$gettopic3['author']."' LIMIT 1"; 
if ($result = mysql_query($query)){
    if (mysql_num_rows($result)) {
        $array = mysql_fetch_assoc($result);
        $image = $array['image'];
$postnum = $array['post_count'];

echo "<img src=\"http://www.runningprofiles.com/members/images/mini/$image\" border=\"1\"  />";?>
<p align="center"> Posts: <?php echo $postnum;?> 
<p align="center">
<?php $result = mysql_query("SELECT * FROM useronline WHERE(user='".$gettopic3['author']."')");
while($row = mysql_fetch_array( $result )) {

$last_active = time() - $row['timestamp'];
}

if($last_active < 300) {
?>
      <span   class="online">online</span>
    <?
} ?>
<p> <?
}
}
?>
</p></td>
<td valign='top'>Started 
<?php $now = time();
if ($now - $dbtime > 86400) { echo 'on '; $dbtime=$gettopic3['showtime']; $time = date("F j Y, g:i a", $dbtime); echo $time;

} else if ($now - $dbtime <= 86400) { echo 'today';
}
?> 
   <? $id3=$gettopic3['postid'];
  ?> id=<? echo $id3;?>

<br>
  <hr>
  <p>
  <?php

$message=$gettopic3['post'];

$message=BBCode($message); ?>

  <?php echo $message ?></p>
  <p align="right"><br>
  </p>
  <p> </p>
  <p align="right"><a href="index.php?page=report&user=<?php echo $gettopic3['author'];?>&msgid=<?php echo $gettopic3['postid'];?>&area=forum">Report to Admin</a></p></td></tr>
<?php
}

$getreplies="Select * from forumtutorial_posts where parentid='$id' ORDER BY showtime $max"; //getting replies

$getreplies2=mysql_query($getreplies) or die(mysql_error()."Could not get replies");

while($getreplies3=mysql_fetch_array($getreplies2))

{

   ?><tr class='mainrow'><td valign='top'><div align="center"><?php echo $getreplies3['author'];?></div>
  <p align="center">
  <?
   $query = "SELECT * FROM users WHERE Username = '".$getreplies3['author']."' LIMIT 1"; 
if ($result = mysql_query($query)){
    if (mysql_num_rows($result)) {
        $array = mysql_fetch_assoc($result);
        $image = $array['image'];
	$postnum = $array['post_count'];

echo "<img src=\"http://www.runningprofiles.com/members/images/mini/$image\" border=\"1\"  />";?>
<p align="center"> Posts: <?php echo $postnum;?> 
<p align="center">
<?php $result = mysql_query("SELECT * FROM useronline WHERE(user='".$getreplies3['author']."')");
while($row = mysql_fetch_array( $result )) {

$last_active = time() - $row['timestamp'];
}

if($last_active < 300) {
?>
      <span   class="online">online</span>
    <?
} ?>
<p> <?
}
}?> 
</p></td>
<td vakign='top'>replied  at 
  <? $dbtime=$getreplies3['showtime']; $time = date("F j Y, g:i a", $dbtime); echo $time;?> 
   <? $id2=$getreplies3['postid'];
  ?> id=<? echo $id2;?>
  <br>
  <hr>
    <p>
      <?php

   $message=$getreplies3['post'];

   $message= BBCode($message);
   $id2=$getreplies3['postid'];

   ?>
      <?php echo $message?></p>
    <p align="right"><br>
  <?php 

            include 'editmessage2.php'; 
//Now increase it:
$CONT_ID++;
         
        ; ?> 
      </p>
    <p align="right"><a href="index.php?page=report&user=<?php echo $getreplies3['author'];?>&msgid=<?php echo $getreplies3['postid'];?>&area=forum">Report to Admin</a></p></td></tr>
<?
}
?>
</table>

<p>
  <?
echo "<br>";

// Find out the total number of pages depending on the limit set
    $numofpages = $rows / $page_rows;
    $totalpages = ceil($numofpages);
// Start links for pages
$maxpage = $totalpages == 0 ? 1 : $totalpages; // add this line
echo "<p align=center>Page $pagenum of $maxpage <br /> "; // change this

// Sets link for previous 25 and return to page 1
    if($pagenum != 1){
        $pageprev = ($pagenum - 1);
         echo "<a href=\"".$_SERVER['PHP_SELF']."?page=message&id=$id&pagenum=1\"><<</a>  ";
         echo "<a href=\"".$_SERVER['PHP_SELF']."?page=message&id=$id&pagenum=$pageprev\">PREV </a> ";
    }else{
        echo "PREV ";
    }


// Loop thru all the pages and echo out the links
    for($i = 1; $i <= $numofpages; $i++){
        if($i == $pagenum){
            echo "[".$i."] ";
        }else{
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=message&id=$id&pagenum=$i\">$i</a> ";
        }
    }

// Check for straglers after the limit blocks
    if(($rows % $page_rows) != 0){
        if($i == $pagenum){
            echo "[".$i."] ";
        }else{
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=message&id=$id&pagenum=$i\">$i</a> ";
        }
    }
// Print out the Next 25 and Goto Last page links
    if(($rows - ($page_rows * $pagenum)) > 0){
        $pagenext = $pagenum++;
          echo "<a href=\"".$_SERVER['PHP_SELF']."?page=message&id=$id&pagenum=$pagenext\">NEXT </a>  ";
          echo "<a href=\"".$_SERVER['PHP_SELF']."?page=message&id=$id&pagenum=$totalpages\">>></a>  ";
    }else{
        echo("NEXT");
    }
echo "</p>";
?>
</p>
<p>  </p>
</body>

</html>

Link to comment
Share on other sites

I think this should do.. Change the line defining max into:

 

<?php

if($pagenum === "last")
{
$query = "Select COUNT(*) as C from forumtutorial_posts where parentid='$id'";
$result = mysql_query($query);
$data = mysql_fetch_array($result);
$pagenum = floor($data['C'] / $page_rows) + 1;
}
$pagenum = intval($pagenum);
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

?>>

 

 

Orio.

Link to comment
Share on other sites

Try it this way:

 

<?php

if($pagenum === "last")
{
   $query = "Select COUNT(*) as C from forumtutorial_posts where parentid='$id'";
   $result = mysql_query($query);
   $data = mysql_fetch_array($result);
   $pagenum = floor($data['C'] / $page_rows) + 1;
}
$pagenum = (is_numeric($pagenum) && $pagenum >= 1) ? (int)$pagenum : 1;
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

?>

 

The thing is that you haven't validated $pagenum - you haven't made sure it is a valid integer. I've added intval() to solve that in the previous script, but for some reason (I guess) it didn't function as expected. I think this should work, give it a try.

 

Orio.

Link to comment
Share on other sites

that works great thanks... ok there are a few things i dont know what they do , would you mind explainin other wise i wont learn form the script lol...

 

im not to sure what this row does $pagenum = (is_numeric($pagenum) && $pagenum >= 1) ? (int)$pagenum : 1;

 

and also this  floor($data['C']  form the c in the query

Link to comment
Share on other sites

In the query I've counted how many posts were made to that topic. "COUNT(*) as C" saves that number as if it was stored in a column named "C". So then later I can access that number in $data['C'].

 

The line you are talking about is using the ternary operator. (scroll down a bit for explanation about it). Basically, it says:

<?php

if(is_numeric($pagenum) && $pagenum >= 1) //Make sure that $pagenum is a number bigger-than/equal-to 1
$pagenum = (int)$pagenum; //Cast to int to round it (in case a float was given)
else
$pagenum = 1; //Default value- in case a invalid number was entered

?>

 

 

Orio.

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.