dividing output into columns with Pagination

Discussion in 'PHP' started by freemancomputer, Mar 7, 2012.

  1. #1
    As the title says I am looking to divide my data output into 2 columns along with using pagination. My goal is to have 2 columns of 20 rows on each page. Here is what i have that works for making one column. I know I have to take what I get with $rowsperpage and divide that in half and throw each half on a side of a table. I am not sure how to do that part tho. But then I could be compleatly wrong. Thanks in advance for your help.

    
    <?php  session_start(); 
    $rank=$_SESSION['rank'];
    $loggedinusername=$_SESSION['loggedinusername'];
    $loggedinuseremail=$_SESSION['loggedinuseremail'];
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Drink To The Credits</title>
    <link href="mainstyle.css" rel="stylesheet" type="text/css" />
    </head>
    
    
    <body class="background">
    <div id="header"> <?php include_once"header.php" ?></div>
    
    
    <div id="content">   
    
    
    <?php
    
    
    include"scripts/connect.php" ;
    
    
    mysql_connect('localhost',$username,$password);
    @mysql_select_db($database) or trigger_error("SQL", E_USER_ERROR);
    
    
    $sql = "SELECT COUNT(*) FROM movies WHERE type LIKE 'movie'";
    $result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
    $r = mysql_fetch_row($result);
    $numrows = $r[0];
    $rowsperpage = 20;
    $totalpages = ceil($numrows / $rowsperpage);
    
    
    
    
    if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
       $currentpage = (int) $_GET['currentpage'];
    } else {
       $currentpage = 1;
    }
    if ($currentpage > $totalpages) {
       $currentpage = $totalpages;
    } 
    if ($currentpage < 1) {
       $currentpage = 1;
    } 
    
    
    $offset = ($currentpage - 1) * $rowsperpage;
    $sql = "SELECT title FROM movies ORDER BY title LIMIT $offset, $rowsperpage" ;
    $result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
    ?>
    <table width="70%" align="left">
    <tr>
    <td align="center" class="rulesub">Movies</td>
    </tr>  
    <?php
    while ($list = mysql_fetch_assoc($result)) {
         ?>
         
    <tr>
    <td> <a class="nav" href=/rules.php?title=<?php echo urlencode($list['title']); ?>><?php echo $list['title']; ?></a> <br />
    
    
    <?php
    } 
    $range = 3;
    
    
    if ($currentpage > 1) {
       echo " <a class='nav' href='<?php movies.php?currentpage=1'><<</a> ";
       $prevpage = $currentpage - 1;
       echo " <a class='nav' href='movies.php?currentpage=$prevpage'><</a> ";
    } 
    for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
       if (($x > 0) && ($x <= $totalpages)) {
          if ($x == $currentpage) {
             echo " [<b>$x</b>] ";
          } else {
             echo " <a class='nav' href='movies.php?currentpage=$x'>$x</a> ";
          } 
       } 
    } 
            
    if ($currentpage != $totalpages) {
       $nextpage = $currentpage + 1;
        
       echo " <a class='nav' href='movies.php?currentpage=$nextpage'>></a> ";
       echo " <a class='nav' href='movies.php?currentpage=$totalpages'>>></a> ";
    } 
    ?>
    
    
    </table>
    </div>
    
    
    
    
    <div id="sidecontent"><?php include_once"newmovies.php" ?></div>
    <div id="footer"> <?php include_once"footer.php" ?></div>
    </body>
    </html>
    PHP:

     
    freemancomputer, Mar 7, 2012 IP
  2. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Don't use while() for your data fetch loop. Before you loop, check mysql_num_rows() on your $result. Then do a for($i=0; $i<=$numRows; $i+=2). Output a <tr>. Fetch a result and output the left column <td>. Increment a counter. If counter<$numRows, fetch another result and output the right column <td>. Increment the counter. Output a </tr>. Loop 'til done.
     
    rainborick, Mar 7, 2012 IP