Need pagination script, can't find one that works with my special query!

Discussion in 'PHP' started by wwwbryan, Feb 18, 2009.

  1. #1
    Hello World!

    I am trying to get a pagination working on my forum script. I can't seem to get it working with it because my script's query is a little different.

    Take a look:

    <?php 
    $forum_name = 'Uncutweb.com';
    $forum_desc = 'Discussion about updates or anything related to the site.';
    $table = 'topics';//Table name here
    $limit = 1;//Limit of results here
    
    
    $totalrows = mysql_num_rows(mysql_query("SELECT id FROM topics WHERE forum=$getforum and important=0"));//Get the total rows of the table
    
    
    
    
    /* check if forum exits */
    if (mysql_fetch_row(mysql_query("SELECT * FROM forums WHERE id = '$getforum'"))) {
    /*Get forum title*/
    if (mysql_fetch_row(mysql_query("SELECT * FROM forums WHERE id = '$getforum'"))) {
    $getforuminfo = mysql_query("SELECT * FROM forums WHERE id = '$getforum'");
    if (!$getforuminfo) {
        echo 'Could not run query: ' . mysql_error();
        exit;
    }
    }
    $foruminfo = mysql_fetch_row($getforuminfo);
    /***** END ******/
    
    
    $countrows = mysql_query("SELECT * FROM topics WHERE forum='$getforum'");
    $numofrows = mysql_num_rows($countrows);
    
    function sortThreadsByLatestPost($forum)
    {
        // First of all get all the posts that are on the board/subject.. Whatever you call it.
        $postsQuery = mysql_query("SELECT * FROM `posts` WHERE `forum` = '$forum' ORDER BY `id` DESC");
    
        $threadsArray = array();
    
    // Loop through them all
        while( $postsArray = mysql_fetch_array($postsQuery) )
        {
            // Now create an array that only contains each thread/topic id once, orderd by the latest post first.
            if( !in_array($postsArray['topic'], $threadsArray) )
            {
                $threadsArray[] = $postsArray['topic'];
            }
        }
        // Return the array of all the id's
        return $threadsArray;
    }
    
    
    $threadsIdArray =  sortThreadsByLatestPost($getforum);//$getforum = Forum ID
    
    // How many threads to be listed pert page?
    $threadsToDisplay = $limit;
    
    $x = 0;
    $y = $threadsToDisplay * ($uncutweb_com + 1);
    
    
    
    echo"<h1><span>";if (empty($_SESSION[username])){echo"<a href='/accounts/login.php'>Login to Post</a>";}else{if($urlrewrite2 == true){echo"<a href='/forums/newtopic/$getforum.html'>Post New Topic</a>";}else{echo"<a href='/forums/newtopic.php?forum=$getforum'>Post New Topic</a>";}}echo"</span>$forum_name</h1>";
    echo'
    <div class="category_h"><h2>' . $foruminfo[1] . '</h2>
    <span></span>';
    
    while( $x < $y )
    {
        $threadQuery = mysql_query("SELECT * FROM `topics` WHERE `id` = '".$threadsIdArray[$x] ."'");
        $threadArray = mysql_fetch_assoc($threadQuery);
    	$urlsubject=str_replace(" ", "-", $threadArray['subject']); 
    	$urlsubject = ereg_replace("[^A-Za-z0-9-]", "", $urlsubject);
    
    /*    echo "
        <strong>Id</strong>: {$threadArray['id']}<br />
        <strong>Name</strong>: {$threadArray['subject']}<br />
        <strong>Date</strong>: {$threadArray['date']}<br />
    	<strong>Topic Starter:</strong>: {$threadArray['poster']}<br />
    	<strong>Last Post</strong>: {$postsQuery['poster']}<br />
        <hr />\n";*/
    echo'
    <div class="category">
    <div class="cright">
    Topic Starter:<br /><a href="/?id=' . $threadArray['membernumber'] . '">' . $threadArray['poster'] . '</a>
    </div>
    <div class="cleft">';
       if($urlrewrite == true){
       echo '<a href="topic/' . $threadArray['id']. '/1/' . $urlsubject . '.html">' . $threadArray['subject'] . '</a><br />' . $threadArray['date'] . '';
       } else {
       echo '<a href="topicbody.php?topic=' . $threadArray['id'] . '">' . $threadArray['subject'] . '</a><br />' . $threadArray['date'] . '';
       }
    echo'
    </div>
    </div>';
    	
    
        $x++;
    }
    
    echo'</div>';
    
    } /* END check if forum exists */
    else
    {
    header( 'Location: /forums/');
    }
    
    echo"<h1><span>";if (empty($_SESSION[username])){echo"<a href='/accounts/login.php'>Login to Post</a>";}else{if($urlrewrite2 == true){echo"<a href='/forums/newtopic/$getforum.html'>Post New Topic</a>";}else{echo"<a href='/forums/newtopic.php?forum=$getforum'>Post New Topic</a>";}}echo"</span>$forum_name</h1>";
    ?>
    PHP:
    Could anyone please figure out a way to get pagination to work with this? I would be very happy.
     
    wwwbryan, Feb 18, 2009 IP
  2. firmaterra

    firmaterra Peon

    Messages:
    756
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Can you post your error message or a bit more description of whats going wrong, what exactly your trying to do?
     
    firmaterra, Feb 18, 2009 IP
  3. wwwbryan

    wwwbryan Well-Known Member

    Messages:
    181
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #3
    ?

    *THE ABOVE CODE DOES NOT HAVE ANY PAGINATION IN IT.

    The error is:

    All pagination don't work correctly? No error it just messes it up?

    The first post couldn't be more self-explanatory.
     
    wwwbryan, Feb 18, 2009 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I think that's more code than most people providing free advice in their free time are willing to look through without a more specific question. You just want someone to write your pagination code?
     
    SmallPotatoes, Feb 19, 2009 IP
  5. wwwbryan

    wwwbryan Well-Known Member

    Messages:
    181
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #5
    No I want to know why like all the pagination scripts don't work right with that.
     
    wwwbryan, Feb 19, 2009 IP
  6. bhagwat_banna

    bhagwat_banna Peon

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    yes u can try it phpclasses.org.


    there is very good pagination scripts. :)
     
    bhagwat_banna, Feb 21, 2009 IP
  7. Valiant

    Valiant Peon

    Messages:
    284
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
  8. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #8
    http://php.fm/pagination

    I can't really be bothered to go through your code so I'll show how to use the PHP.fm pagination script with the following query:
    $postsQuery = mysql_query("SELECT * FROM `posts` WHERE `forum` = '$forum' ORDER BY `id` DESC");
    PHP:
     <?php
    
    require_once 'Pagination.Class.php';
    
    $connection = mysql_connect('server', 'username', 'password');
    mysql_select_db('database', $connection);
    
    if(!($forum = intval($_GET['f'])))
    	$forum = 1;
    
    $class = new Pagination($connection);
    $class->set(array(
        'per_page' => 25,  
        'url_to_build' => '?f=' . $forum . '&page=%d',
    )); 
    
    $class->display("SELECT * FROM `posts` WHERE `forum` = '$forum' ORDER BY `id` DESC", intval($_GET['page'])); 
    PHP:
     
    Danltn, Feb 21, 2009 IP