3 Replies Latest reply: Apr 17, 2013 5:23 PM by matthew stuart RSS

    Previous and next article navigation question

    matthew stuart Community Member

      I have a load of articles in a database table, and I need to apply a previous button and a next button to navigate to the previous or next article.

       

      How do I do it?

       

      Thanks.

        • 1. Re: Previous and next article navigation question
          matthew stuart Community Member

          Don't worry, I've done it!

           

          Turned out to be quite easy in the end.

          • 2. Re: Previous and next article navigation question
            bregent CommunityMVP

            Matthew, can you please post some details about your solution to assist others with similar questions?

            • 3. Re: Previous and next article navigation question
              matthew stuart Community Member

              I created a Previous and Next Link with the following code:

               

              <a href="blog-detail.php?fld_bID=<?php echo $row_rs_prev['fld_bID']; ?>">Previous</a> | <a href="blog-detail.php?fld_bID=<?php echo $row_rs_next['fld_bID']; ?>">Next</a>

               

              The above code gets its values from the three recordsets below. The first gives a URL parameter for the current record, the second uses the current URL param and the < makes sure that I am using any records below. I've also ordered by fld_bID desending to make sure the next record is that which is direct previous in the DB. The third RS uses > and is ordered by ascending to display the record that is directly after the current record.

               

              fld_bID is key to the navigation requirements along with the SELECT queries, so I have made them bold for better visibility.

               

              // 1st RS Allows me to go to a specific record via URL param

              $colname_rs_blog = "-1";

              if (isset($_GET['fld_bID'])) {

                $colname_rs_blog = $_GET['fld_bID'];

              }

              mysql_select_db($database_conn_mrs, $conn_mrs);

              $query_rs_blog = sprintf("SELECT * FROM tbl_blog WHERE fld_bID = %s AND fld_bSHOW = 1 ORDER BY fld_bDATE DESC", GetSQLValueString($colname_rs_blog, "int"));

              $query_limit_rs_blog = sprintf("%s LIMIT %d, %d", $query_rs_blog, $startRow_rs_blog, $maxRows_rs_blog);

              $rs_blog = mysql_query($query_limit_rs_blog, $conn_mrs) or die(mysql_error());

              $row_rs_blog = mysql_fetch_assoc($rs_blog);

               

               

              if (isset($_GET['totalRows_rs_blog'])) {

                $totalRows_rs_blog = $_GET['totalRows_rs_blog'];

              } else {

                $all_rs_blog = mysql_query($query_rs_blog, $conn_mrs);

                $totalRows_rs_blog = mysql_num_rows($all_rs_blog);

              }

              $totalPages_rs_blog = ceil($totalRows_rs_blog/$maxRows_rs_blog)-1;

               

              // 2nd RS Moves to previous record

              $colname_rs_prev = "-1";

              if (isset($_GET['fld_bID'])) {

                $colname_rs_prev = $_GET['fld_bID'];

              }

              mysql_select_db($database_conn_mrs, $conn_mrs);

              $query_rs_prev = sprintf("SELECT * FROM tbl_blog WHERE fld_bID < %s AND fld_bSHOW = 1 ORDER BY fld_bID DESC", GetSQLValueString($colname_rs_prev, "int"));

              $rs_prev = mysql_query($query_rs_prev, $conn_mrs) or die(mysql_error());

              $row_rs_prev = mysql_fetch_assoc($rs_prev);

              $totalRows_rs_prev = mysql_num_rows($rs_prev);

               

              // 3rd RS Moves to next record

              $colname_rs_next = "-1";

              if (isset($_GET['fld_bID'])) {

                $colname_rs_next = $_GET['fld_bID'];

              }

              mysql_select_db($database_conn_mrs, $conn_mrs);

              $query_rs_next = sprintf("SELECT * FROM tbl_blog WHERE fld_bID > %s AND fld_bSHOW = 1 ORDER BY fld_bID ASC", GetSQLValueString($colname_rs_next, "int"));

              $rs_next = mysql_query($query_rs_next, $conn_mrs) or die(mysql_error());

              $row_rs_next = mysql_fetch_assoc($rs_next);

              $totalRows_rs_next = mysql_num_rows($rs_next);

               

              Hopefully, this makes some sense, but by all means ask if you need help.

               

              Mat