MySQL/PHP Need Pagination

Discussion in 'Programming' started by scottlpool2003, Sep 12, 2012.

  1. #1
    Been trying for hours, I need to make my results come out paginated as they are loading quite large images.

    I need a limit of 16 items per page.

    
    <?php 
    
    //GET ID FROM URL
    
     if(isset($_GET['id'])){
     $id =$_GET['id']; 
    
    //Connect to db
    
    mysql_connect("", "", "") or die(mysql_error());
    mysql_select_db("") or die(mysql_error());
    
    //Search query
    $result = mysql_query("SELECT * FROM cat_objects WHERE id = '$id'");
    
    //Make sure there's rows
    if(mysql_num_rows($result)!=0)
    {
    while($row = mysql_fetch_array($result))
      {
      $title = $row[title];
      $url = $row[url];
      }}
    
      else {
      echo "There is no catgegory here!";
      }}
    
    include"../includes/head.php";?>
    
    
    <div style="float:left;width:30%;margin-right:-200px;border:1px solid #747474;min-height:300px;">CONTENT BOX/AD BOX/FEATURED MAGAZINES BOX<br></div>
    
    
    <?php
    
    //Get publication issues to generate images and category titles
    $resultc = mysql_query("SELECT * FROM publication_issue WHERE cat = '$id'");
    ?>
    <div style="float:right;width:57%;margin-right:100px;"><h2>Latest <?php echo "$title";?> Publications</h2></div>
        <div style="border-bottom:4px solid #747474;width:57%;float:right;margin-top:-30px;margin-right:100px;">&nbsp;</div>
    
    <div style="width:57%;margin-left:32px;float:right;margin-right:100px;">
    	  <div class="otherpubcontainer">
    <?
    while($rowc = mysql_fetch_array($resultc))
      {
     $pubid = $rowc[publication_id];
    
    //Get generic publication
    
    $resultb = mysql_query("SELECT * FROM publication WHERE id = '$pubid' ORDER BY id ASC LIMIT 16");
    while($rowb = mysql_fetch_array($resultb))
      {
    	$pubtitle = $rowb[title];
      
    
     $rowimg = $rowc[img];
    }
      echo "<div class=\"otherpub1\"><img src=\"images/uploads/publications/image/big/$rowimg\" title=\"Read $pubtitle Online\" height=\"190px\"></div>";
      }
    
    ?>
    
    	  </div>
    
    <!-- THESE ARE THE BUTTONS I NEED TO IMPLEMENT AS NEXT/PREVIOUS PAGE -->
    
    <div style="clear:both;"></div><br /><div style="width:650px;"><div style="float:left;width:40%;background:beige;border:1px solid silver;-moz-border-radius: 15px;border-radius: 15px;padding:5px;text-align:center;">Previous</div><div style="float:right;margin-right:30px;width:40%;background:beige;border:1px solid silver;-moz-border-radius: 15px;border-radius: 15px;padding:5px;text-align:center;">Next</div></div></div>
    	</div>
    </div>
    </div>
       <?php include"../includes/footer.php";?>
    
    
    PHP:
    What is the best way to go about paginating the results without making utterly messy URLs?
     
    scottlpool2003, Sep 12, 2012 IP
  2. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #2
    Here's a quick example:
    
    <?php
    $itemsPerPage = 16;
    
    mysql_connect("", "", "");
    mysql_select_db("");
    
    $currentPageNumber = (int) $_GET['page'];
    if(empty($currentPageNumber))
    	$currentPageNumber = 1;
    
    $mpnQuery = "SELECT count(id) FROM example";
    $result = mysql_query($mpnQuery) or die(mysql_error());
    $data = mysql_fetch_array($result);
    
    $maxPageNumber = ceil($data[0]/$itemsPerPage);
    
    $start = ($currentPageNumber - 1) * $itemsPerPage;
    
    $query = "SELECT * FROM example LIMIT $start, $itemsPerPage";
    $result = mysql_query($query) or die(mysql_error());
    
    while($row = mysql_fetch_array($result)){
    	echo $row["example"];
    	echo '<br>';
    }
    
    if($currentPageNumber==1){
    	echo 'Previous';
    }
    else{
    	echo '<a href="?page=' .($currentPageNumber - 1). '">Previous</a>';
    }
    
    echo ' - ';
    
    if($currentPageNumber==$maxPageNumber){
    	echo 'Next';
    }
    else{
    	echo '<a href="?page=' .($currentPageNumber + 1). '">Next</a>';
    }
    
    mysql_close();
    ?>
    
    PHP:
    Also, are you aware of that your script is SQL injection vulnerable?
     
    Arttu, Sep 12, 2012 IP
  3. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #3
    I know I should escape and close, I'm writing my scripts before I secure them. I've also just found out that MD5 is insecure so I decided to finish writing all the scripts then go and add a solid layer of security on them.

    Thanks.
     
    scottlpool2003, Sep 13, 2012 IP
  4. wyfytangsh

    wyfytangsh Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Why not write the style in a CSS file ?I think that more better
     
    wyfytangsh, Sep 13, 2012 IP
  5. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #5
    I have an awful habit of writing my styles on the actual page BEFORE I put it all into a stylesheet. I know, ballache but its the way I work.

    1. Design & Style on page
    2. Convert style on page to css file
    3. Write scripts and style on page
    4. Convert script style to css file

    One day, I'll learn to stop making too much work for myself.
     
    scottlpool2003, Sep 14, 2012 IP