Page 1 of 1

OMG, pagination question

Posted: Mon Dec 15, 2008 4:22 pm
by fairyprincess18
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I can't seem to figure why my page hyperlinks are not working in this code snippet. They render fine, but don't go anywhere.

Code:

Code: Select all

<?php
                    $DBConnect = mysqli_connect("localhost", "root", "newpwd", "portfolio")
                        Or die("<p>Connection failed dude!</p>");
                    
                    //checks if there's a page number, if not, sets it to page 1
                    if (!(isset($pagenum)))
                    {
                        $pagenum = 1;
                    }
                    
                    //counts the number of rows returned by query               
                    $data = mysqli_query($DBConnect, "SELECT * FROM links") or die(mysqli_error($DBConnect));
                    $rows = mysqli_num_rows($data); 
                    
                    //number of results displayed per page
                    $page_rows = 5;
                    
                    //page number of our last page
                    $last = ceil($rows / $page_rows);
                    
                    //makes sure the page number isn't below one, or more than our maximum pages
                    if ($pagenum < 1)
                    {
                        $pagenum = 1;
                    }
                    elseif ($pagenum > $last)
                    {
                        $pagenum = $last;
                    }
                    
                    //sets the range to display query
                    $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;  
                    
                    //same query again with $max added into it
                    $data_p = mysqli_query($DBConnect, "SELECT * FROM links $max") or die(mysqli_error($DBConnect));
                    
                    //display query results
                    echo "<table width='10%' border='1'>";
                    echo "<tr><th>Name</th><th>Description</th><th>Link</th></tr>";
                    while($info = mysqli_fetch_array($data_p, MYSQL_NUM))
                    {
                        //display name, desc, link
                        echo "<tr><td>{$info[0]}</td>";
                        echo "<td>{$info[1]}</td>";
                        echo "<td><a href='{$info[2]}'>Go There</a></td></tr>";
                    }
                    
                    //shows user what page they are on and total number of pages
                    echo "<h2>--Page $pagenum of $last--</h2><p>";
                    
                    echo "</table>";
                    
                    // check if on page one, if not, generate links to first page and previous page.
                    if ($pagenum == 1)
                    {
                    }
                    else
                    {
                        echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
                        echo " ";
                        $previous = $pagenum-1;
                        echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
                    }
                    
                    //checks if on last page and generates Next and Last links
                    if ($pagenum == $last)
                    {
                    }
                    else 
                    {
                        $next = $pagenum + 1;
                        echo "<p><a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
                        echo " ";
                        echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
                    }
                    mysqli_close($DBConnect);   
                ?>
thanks for any help.


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: OMG, pagination question

Posted: Mon Dec 15, 2008 4:38 pm
by Chris Corbyn

Code: Select all

if (!(isset($pagenum)))
               {
                   $pagenum = 1;
               }
Where does $pagenum come from? I'll take a stab in the dark and guess it's a register globals issue and you should be using:

$_GET['pagenum']

I only glanced though ;)

Re: OMG, pagination question

Posted: Mon Dec 15, 2008 6:19 pm
by fairyprincess18
i tried this:

Code: Select all

 
<?php
//checks if there's a page number, if not, sets it to page 1
if (!(isset($_GET[$pagenum])))
{
     $pagenum = 1;
}
?>
 
is this what you meant?

Re: OMG, pagination question

Posted: Mon Dec 15, 2008 7:19 pm
by cavemaneca
i think he means add this just before the code

Code: Select all

$pagenum = $_GET['pagenum'];
I do not think however that would help you

First, you added a little extra to this

Code: Select all

$data = mysqli_query($DBConnect, "SELECT * FROM links")
Just do this

Code: Select all

$data = mysqli_query("SELECT * FROM links")
next you need this

Code: Select all

$row = mysqli_fetch_assoc($data)
to actually store the data, then

Code: Select all

$pagenum = $row['pagenum']
that will store the variable you are looking for(obviously, you might need to change the last part depending on what your column name is in the table)

together it might look something like this

Code: Select all

$data = mysqli_query("SELECT * FROM links")
$row = mysqli_fetch_assoc($data)
$pagenum = $row['pagenum']
and that's my ten cents for now.

Re: OMG, pagination question

Posted: Mon Dec 15, 2008 7:42 pm
by fairyprincess18
not quite sure where i should insert your suggestion.

i tried this:

Code: Select all

 
<?php
    //checks if there's a page number, if not, sets it to page 1
    if (!(isset($pagenum)))
    {
        $pagenum = 1;
    }
    
    //old code
    /*counts the number of rows returned by query               
    $data = mysqli_query("SELECT * FROM links");
    $rows = mysqli_num_rows($data);*/ 
    
    //new code
    $data = mysqli_query($DBConnect, "SELECT * FROM links");  //this didn't work without link id
    $row = mysqli_fetch_assoc($data);
    $pagenum = $row['pagenum'];
?>
 
thoughts?

Re: OMG, pagination question

Posted: Mon Dec 15, 2008 8:17 pm
by cavemaneca
replace the first line with line 11 on yours, then put the other two right after line 12

Re: OMG, pagination question

Posted: Tue Dec 16, 2008 3:50 pm
by fairyprincess18
you mean like this?

Code: Select all

 
<?php
//new code
$data = mysqli_query($DBConnect, "SELECT * FROM links");
$rows = mysqli_num_rows($data);
$row = mysqli_fetch_assoc($data);
$pagenum = $row['pagenum'];
?>
 
i see you're trying to store an index of the returned associative
array in pagenum, but what would i do with it then?

Re: OMG, pagination question

Posted: Tue Dec 16, 2008 4:27 pm
by fairyprincess18
thanks Breakbeat Nuttzer!

your solution worked!