Does anyone know how to organize topics by last post?

Discussion in 'PHP' started by wwwbryan, May 3, 2009.

  1. #1
    Like if Topic 1 has post number 5, put it before topic 2, which has post number 4 as the last post.

    How would I do that?

    <?php
    
    if ($usable != true) {header('location: ./index.php');}else
    define('THIS_SCRIPT', 'threads');
    
    function sortThreadsByLatestPost($theBoardId)
    {
        $postsQuery = mysql_query("SELECT * FROM `posts` WHERE `forum` = '$theBoardId' ORDER BY `id` DESC");
    
        $threadsArray = array();
    
        while( $postsArray = mysql_fetch_assoc($postsQuery) )
        {
            if( !in_array($postsArray['topic'], $threadsArray) )
            {
                $threadsArray[] = $postsArray['topic'];
            }
        }
        return $threadsArray;
    }
    
    $threadsIdArray =  sortThreadsByLatestPost($getforum);
    
    
    $threadsToDisplay = 15;
    
    $rowsQuery = mysql_query("SELECT * FROM `topics` WHERE `forum` = '$getforum'");
    $totalrows = mysql_num_rows($rowsQuery);
    
    $totalpages = $totalrows / $threadsToDisplay;
    $totalpages = ceil($totalpages);
    
    
    $page = (int)$_GET['page'];
    
    if( $totalpages == 0 )
    {
    	$totalpages = 1;
    }
    if( $page == 0 )
    {
    	$page++;	
    }
    
    $x = $threadsToDisplay * ($page - 1);
    $y = $threadsToDisplay * $page;
    
    if( $y > count($threadsIdArray) )
    {
    	$y = count($threadsIdArray);
    }
    
    if ( mysql_num_rows(mysql_query("SELECT * FROM `forums` WHERE id = '$getforum'")) > 0 )
    {
    	$category1f = mysql_query("SELECT * FROM `forums` WHERE `id` = '$getforum'");
    	if (!$category1f)
    	{
    		$main .=  'Could not run query: ' . mysql_error();
    		exit;
    	}
    }
    else
    {
    	$main .=  'Not found';
    	exit;
    }
    
    
    $forumArray = mysql_fetch_assoc($category1f);
    
    $main .=  "
    <div class=\"postbutton\">
    
    ";
    
    $nextpage = $page + 1;
    $prevpage = $page - 1;
    
    $main .=  "<span>";
    ;if (empty($_SESSION[username])){$main .= '<a href="./index.php?page=login">Login to Post</a>';}
    else{if($urlrewrite2 == true){$main .= "<a href='/forums/newtopic/$getforum.html'>Post New Topic</a>";}
    else{$main .= '<a href="./index.php?page=newtopic&f=' . $getforum . '">Post New Topic</a>';}}$main .= "
    </span>
    	";
    $main .=  "
    
    		Page $page of $totalpages ";
    
    if( $prevpage != 0  )
    {
    	$main .=  " <a href=\"?forum=$getforum&amp;page=1\">First page</a> <a href=\"?forum=$getforum&amp;page=". $prevpage ."\">Previous page</a> ";	
    }
    
    if( $nextpage <= $totalpages )
    {
    	$main .=  " <a href=\"?forum=$getforum&amp;page=". $nextpage ."\">Next page</a> <a href=\"?forum=$getforum&amp;page=". $totalpages ."\">Last page</a>";	
    }
    $main .= "</div>";
    /*	
    if( empty($_SESSION['username']) )
    {
    	$main .=  "<a href='/accounts/login.php'>Login to Post</a>";
    }
    else
    {
    	if($urlrewrite2 == true)
    	{
    		$main .=  "<a href='/forums/newtopic/$getforum.html'>Post New Topic</a>";
    	}
    	else
    	{
    		$main .=  "<a href='/forums/newtopic.php?forum=$getforum'>Post New Topic</a>";
    	}
    }*/
    
    $main .= '
    
    
    <div class="multicode">
    <div class="thead"><div class="thead_l"><div class="thead_r"><p>' . $title . '<span>Viewing Threads</span></p></div></div></div>
    <div class="tborder">
     <div class="fsection" style="background: #FFFFFF;border-bottom: 1px solid #677ea8;">
    <div style="float:right;color: #555555;margin-right: 7%;text-align:right;"><p>
    Last Post
    </p>
    </div>
    
    <div class="col1" style="background:none;width: 60%"><p>
    Topic
    </p>
    </div>
    
     <div class="col2" style="background:none;width: 100px;text-align:center;"><p>
     Statistics
     </p>
     </div>
    </div>
    ';
    
        $threadQuery2 = mysql_query("SELECT * FROM `topics` WHERE `forum` = '".$getforum ."'");
        $threadArray2 =  mysql_num_rows($threadQuery2); 
    
    if($threadArray2 < 1){$main .= '<tr><td class="t1" colspan="5"><p style="text-align:center;">There are currently no topics in this forum.</p></td></tr>';}
    while( $x < $y )
    {
        $threadQuery = mysql_query("SELECT * FROM `topics` WHERE `id` = '".$threadsIdArray[$x] ."'");
        $threadArray = mysql_fetch_assoc($threadQuery);
    
        $postQuery = mysql_query("SELECT * FROM `posts` WHERE `topic` = '{$threadArray['id']}' ORDER BY `id` DESC LIMIT 1");
        $postArray = mysql_fetch_assoc($postQuery);
    	
    	$urlsubject=str_replace(" ", "-", $threadArray['subject']); 
    	$urlsubject = ereg_replace("[^A-Za-z0-9-]", "", $urlsubject);
    	
       $numpostsQuery = mysql_query("SELECT id FROM `posts` WHERE `topic` = '{$threadArray['id']}'");
       $numposts = mysql_num_rows($numpostsQuery); 
       
       $userinfoQuery = mysql_query("SELECT * FROM `members` WHERE `mid` = '{$threadArray['membernumber']}'");
       $userinfoArray = mysql_fetch_array($userinfoQuery);
       
       $userinfo2Query = mysql_query("SELECT * FROM `members` WHERE `mid` = '{$postArray['membernumber']}'");
       $userinfo2Array = mysql_fetch_array($userinfo2Query);
       
       $threadArray['time'] = date("F d, Y", $threadArray['time']);
       $postArray['time'] = date("F d, Y", $postArray['time']);
    
        $main .=  '
    	
    	 <div class="fsection" style="background: #f0f4f8;">
    	 
    <div style="float:right;color: #555555;margin-right: 7%;overflow:hidden;width: 200px;"><p>
    ' . $postArray['time'] . '<br /><a href="./index.php?topic=' . $threadsIdArray[$x] . '&page=last&' . $urlsubject . '.html#' . $postArray['id'] . '">' . $threadArray['subject'] . '</a><br />By <a href="?id=' . $postArray['membernumber'] . '">' . $userinfo2Array['username'] . '</a>
     
     </p>
     </div>
    	 
     <div class="col1" style="width: 60%"><p>
    <a href="./index.php?topic=' . $threadArray['id'] . '&page=1&' . $urlsubject . '.html"><b>' . $threadArray['subject'] . '</b></a><span style="font-size: 11px;display:block;">
    Topic Starter: <a href="./index.php?member=' . $threadArray['membernumber'] . '">' . $userinfoArray['username'] . '</a></span></p>
    </div>
    
    
     <div class="col2" style="width: 100px"><p>
    <b style="text-align:center;display:block;">' . $threadArray['time'] . '</b>
    <span style="font-size: 11px;text-align:center;">Posts:  ' . $numposts . '</span>
    </p>
    </div>
    
    
    
    
    
    </div>
    <div style="background: #CCCCCC;height: 1px;"></div>
    
    ';
        $x++;
    }
    	
    $main .=  "
    </div></div>
    ";
    $main .=  "
    <div class=\"postbutton\">
    
    ";
    
    $nextpage = $page + 1;
    $prevpage = $page - 1;
    
    
    $main .=  "<span>";
    ;if (empty($_SESSION[username])){$main .= '<a href="./index.php?page=login">Login to Post</a>';}
    else{if($urlrewrite2 == true){$main .= "<a class=\"postbutton\" href='/forums/newtopic/$getforum.html'>Post New Topic</a>";}
    else{$main .= '<a href="./index.php?page=newtopic&f=' . $getforum . '">Post New Topic</a>';}}$main .= "
    </span>
    
    
    
    
    ";
    
    $main .=  "
    
    		Page $page of $totalpages ";
    
    if( $prevpage != 0  )
    {
    	$main .=  " <a href=\"?forum=$getforum&amp;page=1\">First page</a> <a href=\"?forum=$getforum&amp;page=". $prevpage ."\">Previous page</a> ";	
    }
    
    if( $nextpage <= $totalpages )
    {
    	$main .=  " <a href=\"?forum=$getforum&amp;page=". $nextpage ."\">Next page</a> <a href=\"?forum=$getforum&amp;page=". $totalpages ."\">Last page</a>";	
    }
    
    $main .=  "</div>";
    
    ?>
    Code (markup):
    A friend made that script for me, but for some reason it doesn't work anymore, and it orders by latest topic rather than the post.
     
    wwwbryan, May 3, 2009 IP
  2. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #2
    Use datetime fields instead of id or posts count.

    Regards
     
    koko5, May 3, 2009 IP
  3. wwwbryan

    wwwbryan Well-Known Member

    Messages:
    181
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #3
    NEVERMIND, I figured out the problem, it was a problem with the forum column.
     
    wwwbryan, May 3, 2009 IP