Jump to content

[SOLVED] While loops+pagination


supanoob

Recommended Posts

ok so i have the following code for the forum i am working on. i have looked over it time and time again, checked and double check my insert code and my database to make sure everything is going in right and it is, yet for some reason it only seems to be looping once then stopping so it i will only show one post (the first one posted.) now what i need it to do is show all posts in order of the Latest reply in them or the time they were posted for example:

 

someone makes a post at 10:00pm on the 17th of april, then someone make a post at 10:01 on the 17th of april it would look like this

 

10:01

10:00

 

but if someone was to reply to the 10:00 one it would move back to the top of the list. Can you please look over the code and see if you can spot what has gone wrong, thanks in advance

 

<?php 

if ($_GET['step'] == 'view_board')
{
    $limit      = 20;               
    // Sets how many results shown per page
    $board_id=$_GET['board_id'];
    $query_count    = "SELECT * FROM tord_forums where posted_in='$board_id'";    
    // Sets what we want to pull from the database
    // count(*) is better for large databases (thanks Greg!)
    
    $result_count   = mysql_query($query_count);    
    // Pulls what we want from the database
    
    $totalrows  = mysql_num_rows($result_count);    
    // This counts the number of users 
    
if ($board_id == '1'){$board = 'Announcments';} if ($board_id == '2'){$board = 'Site Suggestions';}
if ($board_id == '3'){$board = 'Site Bugs';}if ($board_id == '4'){$board = 'General Talk';}
if ($board_id == '5'){$board = 'Site Help';}if ($board_id == '6'){$board = 'Find an Opponent';}
if ($board_id == '7'){$board = 'Entertainment Board';}



//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-//
//-=-=-=-=-=-Section 8.1-=-=-=-=-=-=-=-=-=-=-=-//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-//
if ($_GET['option'] == 'delete_post')
{
$post_id=$_GET['post_id'];
$board_id=$_GET['board_id'];

$query="select posted_by from tord_forums where post_id='$post_id'";
$result=mysql_query($query);
if (!$result)
{
die (mysql_error());
}

$num_rows=mysql_num_rows($result);

$row=mysql_fetch_array($result);
$posted_by=($row['posted_by']);

if ($rank != 'Owner' || $rank != 'Moderator')
{
if ($account_id != $posted_by)
{
echo "You cannot delete this Post.";
die(include_once('logged_in_bottom.php'));
}
}

$sql2="UPDATE tord_forums SET deleted='yes' WHERE post_id='$post_id'";
if(mysql_query($sql2))

$sql2="UPDATE tord_forum_stats SET topics=topics-1 WHERE forum='$board_id'";
if(mysql_query($sql2));

echo "Post Deleted";
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-//
//-=-=-=-=-=-Section 8.1-=-=-=-=-=-=-=-=-=-=-=-//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-//
if ($_GET['option'] == 'sticky_post')
{
$post_id=$_GET['post_id'];
$sql2="UPDATE tord_forums SET sticky='1' WHERE post_id='$post_id'";
if(mysql_query($sql2))

echo "Post Stickied";
}
if ($_GET['option'] == 'lock_post')
{
$post_id=$_GET['post_id'];
$sql2="UPDATE tord_forums SET locked='1' WHERE post_id='$post_id'";
if(mysql_query($sql2))

echo "Post Stickied";
}





echo "<p align=\"center\"><a href=\"forum.php?step=forum_index\">Forums</a> >> <a href=\"forum.php?step=view_board&board_id=$board_id\">$board</a><br>";

    
echo "</p>
<table border=\"0\" width=\"100%\" id=\"table1\">
<tr>
	<td width=\"46%\" align=\"center\" bgcolor=\"#838383\">Topic Subject</td>
	<td width=\"20%\" align=\"center\" bgcolor=\"#838383\">Started By</td>
	<td width=\"10%\" align=\"center\" bgcolor=\"#838383\">Views</td>
	<td width=\"10%\" align=\"center\" bgcolor=\"#838383\">Replies</td>
	<td width=\"24%\" align=\"center\" bgcolor=\"#838383\">Options</td>
</tr>";


if(empty($_GET['page'])){    // Checks if the $page variable is empty (not set)
        $_GET['page'] = 1;      // If it is empty, we're on page 1
    }
    $limitvalue = $_GET['page'] * $limit - ($limit); 
    // Ex: (2 * 25) - 25 = 25 <- data starts at 25
    $board_id=$_GET['board_id'];
    $query  = "SELECT post_id, post_topic, locked, post_views, post_replies, latest_post, posted_by, sticky, deleted, reply_to, post_type FROM tord_forums where posted_in='$board_id' and deleted='no' and post_type='topic' order by latest_post, sticky desc LIMIT $limitvalue, $limit";        
    $result = mysql_query($query) or die("Error: " . mysql_error()); 
    // Selects all the data from table.
    // mysql_error() will print an error if one occurs.
    
    /* Tip: The MySQL LIMIT value syntax is as follows: 
    
    LIMIT $row_to_start_at, $how_many_rows_to_return 
    
    */ 

                      while($row = mysql_fetch_array($result)){
                      $post_id=($row['post_id']);
                      $post_topic=($row['post_topic']);
                      $post_replies=($row['post_replies']);
                      $posted_by=($row['posted_by']);
                      $sticky=($row['sticky']);
                      $locked=($row['locked']);
                      $deleted=($row['deleted']);
                      $reply_to=($row['reply_to']);
                      $post_type=($row['post_type']);
                      $post_views=($row['post_views']);
                      $latest_post=($row['latest_post']);
                      
                      $query="select username from tord_accounts where account_id='$posted_by'";
                      $result=mysql_query($query);
                      if (!$result)
                      {
                      die (mysql_error());
                      }

                      $num_rows=mysql_num_rows($result);

                      $row=mysql_fetch_array($result);
                      $name_2=($row['username']);
                      
                      $board_id=$_GET['board_id'];
                      echo "<tr>
	<td bgcolor=\"#A7A7A7\" width=\"46%\">";if ($sticky == '1'){echo "<b>[sticky] </b>";}if ($locked == '1'){echo "<b>[Locked] </b>";}
	echo "<a href=forum.php?step=view_forum_post&post_id=$post_id&board_id=$board_id>"; echo "$post_topic";  echo "</a></td>
	<td width=\"20%\" align=\"center\" bgcolor=\"#A7A7A7\">$name_2</td>
	<td width=\"10%\" align=\"center\" bgcolor=\"#A7A7A7\">$post_views</td>
	<td width=\"10%\" align=\"center\" bgcolor=\"#A7A7A7\">$post_replies</td>
	<td width=\"24%\" align=\"center\" bgcolor=\"#A7A7A7\">"; 
    if ($posted_by == $account_id || $rank == 'Owner' || $rank == 'Moderator')
    {
    echo "[<a href=\"forum.php?step=view_board&option=delete_post&post_id=$post_id&board_id=$board_id\">D</a>]"; 
    }
    if ($rank == 'Owner' || $rank == 'Moderator')
    {
    if ($sticky == '0'){echo "[<a href=\"forum.php?step=view_board&option=sticky_post&post_id=$post_id&board_id=$board_id\">S</a>]";}
    if ($sticky == '1'){echo "[uS]";}
    if ($locked == '0'){echo "[<a href=\"forum.php?step=view_board&option=lock_post&post_id=$post_id&board_id=$board_id\">L</a>]";}
    if ($locked == '1'){echo "[uL]";}
    } 
    echo "</td>
</tr>";
}


echo"</table>";

if($_GET['page'] != 1){ 
$page=$_GET['page'];
        $pageprev = $page-1;
        
        echo("[<a href=\"forum.php?step=view_board&board_id=1&page=$pageprev\">Previous</a>|"); 
    }else{
        echo("[Previous|");
    }

    $numofpages = $totalrows / $limit; 
    
    for($i = 1; $i <= $numofpages; $i++){
    $page=$_GET['page'];
            if($i == $page){
            echo("$i|");
        }else{
            echo("<a href=\"forum.php?step=view_board&board_id=1&page=$i\">$i</a>|");
        }
    }


    if(($totalrows % $limit) != 0){
    $page=$_GET['page'];
     if($i == $page){
            echo("$i|");
        }else{
            echo("<a href=\"forum.php?step=view_board&board_id=1&page=$i\">$i</a>|");
        }
    }
    $page=$_GET['page'];
    if(($totalrows - ($limit * $page)) > 0){
    $page=$_GET['page'];
        $pagenext = $page+1;
         
        echo("<a href=\"forum.php?step=view_board&board_id=1&page=$pagenext\">Next</a>]"); 
    }else{
        echo("Next]"); 
    }
    
    mysql_free_result($result);
$board_id=$_GET['board_id'];
  if ($board_id != '1'){   
  
echo "<br><a href=\"forum.php?step=start_topic&board_id=$board_id\">Start Topic</a><br><br>
  Options Key: [d] = Delete [s] = Sticky [us] = un-sticky";
  }
  else 
  {
  if ($rank == 'Owner')
  {
  echo "<br><a href=\"forum.php?step=start_topic&board_id=$board_id\">Start Topic</a>";
  }
  echo "<br><b>You Cannot Post Here.</b>";
  }


}


?>

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.