We help IT Professionals succeed at work.
Get Started

php paging sections

rossh1977
rossh1977 asked
on
257 Views
Last Modified: 2008-02-26
Hi i'm trying to split the pages by section insted of by $no_of_records = 5;

The sections are taken from the foriegn key.  I want to be able to count the number of records which hold a specific forigen key and display on one page and then the next.

Thanks

Ross

<?php
      require("c:\Inetpub\wwwroot\mysql_connect.php");
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>

<title></title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?php
      if (isset ($_GET['page']))
      {
            $page = $_GET['page'];
      }
      else
      {
            $page = 1;
      }
      
      //count sections
      
      $records_to_display = 5;
      $sql_paging = "SELECT COUNT(*) AS numRecords FROM question";
      $newArray = mysql_query($sql_paging, $db) or die(mysql_error());
      $records = mysql_fetch_array($newArray);
      
      $recordsNum = $records['numRecords'];
      $remainder =  $recordsNum%$records_to_display;
      $recordsNum = $recordsNum - $remainder;
      $pages = $recordsNum/$records_to_display;
      
      if ($remainder>0)
      {
            $pages++;
      }
      
      if ($page>$pages)
      {
            $page = 1;
      }
      
      $query = 'SELECT question_type.question_type, question_group.question_group, question_id, question, question_order FROM question
      LEFT JOIN question_group ON question.ID_question_group = question_group.question_group_id
      LEFT JOIN question_type ON question.ID_question_type = question_type.question_type_id
      ORDER BY question_group_order ASC, question_order ASC';
      
      function newGroup($myresource)
      {
            print "<h1>".$myresource->question_group."</h1>";
      }
      $previous_group = -1;
      $my_query_result = mysql_query ($query, $db);
      
      if ($page>1)
      {
            for ($i=1; $i<$page; $i++)
            {
                  for($j=0; $j<$records_to_display; $j++)
                  {
                        $myresource = mysql_fetch_object($my_query_result);
                  }
            }
      }
      
      $no = 0;
?>

</head>

<body>

<?php
      while ($myresource = mysql_fetch_object($my_query_result))
      {
            if ($myresource->question_group != $previous_group)
            {
                  newGroup($myresource);
                  $previous_group = $myresource->question_group;
            }
            
            print $myresource->question_order.", ";
            print $myresource->question."<br/>";
            
            $no++;
            
            if ($no==$records_to_display)
            {
                  break;
            }
      }
      
      mysql_close($db);
?>

<?php
      if ($page<2)
      {
            print("<p>&lt;&lt;");
      }
      else
      {
            print("<p><a href='".$_SERVER['PHP_SELF']."?page=".($page-1)."'>&lt;&lt;</a>");
      }
      
      for ($i=1; $i<=$pages; $i++)
      {
            if ($i==$page)
            {
                  print ("&nbsp;<a href='".$_SERVER['PHP_SELF']."?page=".($i)."'><b>".$i."</b></a>");
            }
            else
            {
                  print ("&nbsp;<a href='".$_SERVER['PHP_SELF']."?page=".($i)."'>".$i."</a>");
            }
      }
      
      if ($page>=$pages)
      {
            print "&nbsp;&gt;&gt;<p>";
      }
      else
      {
            print"&nbsp;<a href='".$_SERVER['PHP_SELF']."?page=".($page+1)."'>&gt;&gt;</a><p>";
      }
?>

</body>

</html>
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE