Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7864049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:40:20+00:00 2026-06-02T23:40:20+00:00

I have been doings bits here and there on some code which essentially pulls

  • 0

I have been doings bits here and there on some code which essentially pulls data down from mysql and displays through php. Results are displayed 3 at a time on the page and I have never had any issues.

I have recently added a field to the database items however: ‘sold out’ and ‘in stock’ and am trying (miserably) to get two searches going – firstly I want to display the items in stock, and then those that have sold out. Unfortunately, the pagination code I have used for years doesn’t like me running two php queries and is simply adding 3 extra items to the page (where applicable).

Full code is:

        <form name="form1" method="get" action="products.php">
            <?php 
      if(!empty($msg)) {
      echo $msg[0];
      }
      ?>



             <input name="q" class="textInput2" input type="search" id="q" placeholder="search image name..." autosave="applestyle_srch" results="5" onKeyUp="applesearch.onChange('srch_fld','srch_clear')" />     


              <input name="doSearch" type="hidden" id="doSearch2" value="Search">


        <?php if ($get['doSearch'] == 'Search') {
      // find out how many rows are in the table 
$sql = "SELECT COUNT(*) FROM products";
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 3;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;


      if($get['q'] == '') {  
    $sql = "SELECT * FROM products WHERE status='sold' ORDER BY `price` ASC LIMIT $offset, $rowsperpage";
      } 
      else { 
      $sql = "select * from products where `title` like '%$_REQUEST[q]%' LIMIT $offset, $rowsperpage";
      }

$result1 = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);


       if($get['q'] == '') {  
    $sql = "SELECT * FROM products WHERE status='in stock' ORDER BY `price` ASC LIMIT $offset, $rowsperpage";
      } 
      else { 
      $sql = "select * from products where `title` like '%$_REQUEST[q]%' LIMIT $offset, $rowsperpage";
      }

$result2 = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);


      ?></form></div>
 <div id="pagination" style="float:right; display:inline; margin-right:10px;">
          page:<?php

/******  build the pagination links ******/
// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?q=&doSearch=Search&currentpage=1'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='{$_SERVER['PHP_SELF']}?q=&doSearch=Search&currentpage=$prevpage'><</a> ";
} // end if
      // range of num links to show
$range = 3;

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range)  + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='{$_SERVER['PHP_SELF']}?q=&doSearch=Search&currentpage=$x'>$x</a> ";
      } // end else
   } // end if 
} // end for

// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 
   echo " <a href='{$_SERVER['PHP_SELF']}?q=&doSearch=Search&currentpage=$nextpage'>></a> ";
   // echo forward link for lastpage
   echo " <a href='{$_SERVER['PHP_SELF']}?q=&doSearch=Search&currentpage=$totalpages'>>></a> ";
} // end if
/****** end build pagination links ******/
?>
          </div>

    </div>
    </div>


</div></p>


<div class="category-products">


  <form name "searchform" action="products.php" method="post">

   <?php while($rrows = mysql_fetch_array($result2))
{
    echo '<div><div id="searchimage"><a class="product-image" href="productspec.php?productcode=' . $rrows['productcode'] . '" title="' . $rrows['title'] . '">';
    echo '<img src="' . $rrows['photo'] . '" width="225" height="150" alt="" title="' . $rrows['title'] . '" /></a></div>';
    echo '<div id="searchdetails">
<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
    <td height="30"><h3 class="product-name"><a href="productspec.php?productcode=' . $rrows['productcode'] . '" title="' . $rrows['title'] . '">' . $rrows['title'] . '</a></h3>';
    echo '</td>
  </tr>
  <tr>
    <td>' . $rrows['desc'] . '';
    echo '</td>
  </tr>
   <tr>
    <td><h3 class="price">&pound;' . $rrows['price'] . '&nbsp;' . $rrows['pandp'] . '</h3>'; echo '</td>
  </tr>
</table></div>';
echo '</div>';
}
?>

 <?php while($rrows = mysql_fetch_array($result1))
{
    echo '<div><div id="searchimage"><a class="product-image" href="productspec.php?productcode=' . $rrows['productcode'] . '" title="' . $rrows['title'] . '">';
    echo '<img src="' . $rrows['photo'] . '" width="225" height="150" alt="" title="' . $rrows['title'] . '" /><img src="soldout.png" id="soldout" /></a></div>';
    echo '<div id="searchdetails">
<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
    <td height="30"><h3 class="product-name"><a href="productspec.php?productcode=' . $rrows['productcode'] . '" title="' . $rrows['title'] . '">' . $rrows['title'] . '</a></h3>';
    echo '</td>
  </tr>
  <tr>
    <td>' . $rrows['desc'] . '';
    echo '</td>
  </tr>
   <tr>
    <td><h3 class="price">&pound;' . $rrows['price'] . '&nbsp;' . $rrows['pandp'] . '</h3>'; echo '</td>
  </tr>
</table></div>';
echo '</div>';
}
?>

  </form> <?php } ?>

I can fully appreciate that the code could be cleaner – (I haven’t revised the pagination process for years now as it has always worked) – but am assuming there must be an easier way to display all the items in the table witthout having to run both the $result1 and $result2 queries.

Any help much appreciated!!

JD

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:40:22+00:00Added an answer on June 2, 2026 at 11:40 pm

    Here’s a function I made for a board I’ve programmed. I hope this helps you in some way.
    It is a bit messy (to me) since I haven’t updated in awhile.

    The 5 required variables this function asks for is the number of rows, the url of your page so that links work well, the “posts per page” which basically means how many results you want per page (default 20), the name of the link so if you want the next page to appear at the page link, and finally if you want to change what $_GET you want to change the page to. Default is $_GET[‘page’].

      function pagelinks($numrows, $primaryurl, $ppp=20, $aname="", $getpage="default")
       {
        global $baseurl; //this is for something like http://somesite.com/
    
        //If the GET method is not defined, $_GET[page] is used.
       if ($getpage == "default")
        {
         $page = intval($_GET['page']);
         $getpage = "page";
        }
       else
        {
         $page = intval($_GET[$getpage]);
        }
        $ppp = intval($ppp);
    
       if ($aname != "")
        {
         $linkname = "#$aname";
         //$linkname2 = "<a name=\"next_page\"></a>";
        }
    
        //If we don't define the posts/articles per page, we go to a postsperpage user default.
       if ($ppp == 0)
        {
         $ppp = 20;
        }
    
        //If the page is negative, we define it 1.
       if (intval($page) < 0)
        {
         $page = 1;
        }
    
        //If the GET page is higher than the highest possible, we make it the highest limiting page.
       if (($numrows / $ppp + 1) < intval($page))
        {
         $page = intval($numrows / $ppp + 1);
        }
    
        //If the url doesn't point to any page, we just set it to the first group of the query limit.
       if (!$page)
        {
         $page = 1;
        }
        $min = (($page - 1) * $ppp);
        $maxnum = floor($numrows / $ppp);
        $pages = "<center><small>Pages:</small><br>";
        //If we are on the first page, don't link to any previous pages because there are none
        //Else we link the arrow to the page one less than current.
       if (($page - 1) <= 0)
        {
         $pages .= "<img src=\"$baseurl/boardfiles/images/previous_mono.gif\" border=\"0\" alt=\"\">";
        }
       else
        {
         $pages .= "<a href=\"".$primaryurl."&amp;$getpage=".($page-1)."$linkname\"><img src=\"$baseurl/boardfiles/images/previous.gif\" border=\"0\" alt=\"<\"></a>";
        }
       for ($i = 0; $i <= $maxnum; $i++)
        {
         $i2 = $i+1;
        if ($i != $maxnum || ($numrows / $ppp) != (floor($numrows / $ppp)) || !$numrows)
         {
          //If the page number matches our page, it isn't linked.
          //Else we keep linking all existing page numbers.
         if ($page == $i2)
          {
           $pages .= " $i2";
          }
         else
          {
           $pages .= " <a href=\"".$primaryurl."&amp;$getpage=".($i + 1)."$linkname\">".($i + 1)."</a>";
          }
         }
        else
         {
          $lastcutoff = 1;
         }
        }
        //If we are on the last page, don't link to the next page because we are on the top one.
        //Else we link the arrow to the page one more than current.
       if ($page > $maxnum || ($lastcutoff && $page > $maxnum - 1))
        {
         $pages .= " <img src=\"$baseurl/boardfiles/images/next_mono.gif\" border=\"0\" alt=\"\">";
        }
       else
        {
         $pages .= " <a href=\"".$primaryurl."&amp;$getpage=".($page+1)."$linkname\"><img src=\"$baseurl/boardfiles/images/next.gif\" border=\"0\" alt=\">\"></a>";
        }
        $pages .= "</center>";
        $return['pages'] = $pages;
        $return['querylimit'] = " LIMIT $min, $ppp";
        $return['min'] = $min; //Used in users.php
        return $return;
       }
    

    Now you will need to change the code a bit to suit it to your needs but next you will need to place this in your query. Here’s some example use:

      #-- Page Stuff --#  
      $articlesperpage = 10;
      $numrows = $misc['totalarticles'];
    
      $p = pagelinks($numrows, "$baseurl/?p=news", $articlesperpage);
      $pages = $p['pages'];
      #-- Page Stuff --#  
    
      $allnews = sql_query("SELECT * FROM `news` ORDER BY `id` DESC$p[querylimit]");
    

    The important thing is to use $p[querylimit] to limit your query and use $pages for your pagination links. Hope this is useful!

    Edit: I have also recently created another pagination function you may want instead of the above as this one uses a select option list rather than a bunch of links. And I also fixed a bug I noticed an hour ago. Funny how I caught it as I was going over this again.

      function pagination2($numrows, $primaryurl, $ppp=20)
       {
        global $baseurl;
    
        $page = intval($_GET['page']);
        $ppp = intval($ppp);
    
        //If we don't define the posts/articles per page, we go to a postsperpage user default.
       if ($ppp == 0)
        {
         $ppp = 20;
        }
    
        //If the page is negative, we define it 1.
       if (intval($page) < 0)
        {
         $page = 1;
        }
    
        //If the GET page is higher than the highest possible, we make it the highest limiting page.
       if (($numrows / $ppp) < intval($page))
        {
         $page = intval($numrows / $ppp);
        }
    
        //If the url doesn't point to any page, we just set it to the first group of the query limit.
       if (!$page)
        {
         $page = 1;
        }
        $min = (($page - 1) * $ppp);
        $maxnum = floor($numrows / $ppp);
    
        $pages = "<select onchange=\"window.location.href=this.value;\" style=\"font-size: 8pt;\">";
    
       for ($i = 0; $i <= $maxnum; $i++)
        {
         $i2 = $i + 1;
        if ($i != $maxnum || ($numrows / $ppp) != (floor($numrows / $ppp)) || !$numrows)
         {
         if ($page == $i2)
          {
           $pages .= "<option value=\"$primaryurl&amp;page=$i2\" selected>Page $i2</option>";
          }
         else
          {
           $pages .= "<option value=\"$primaryurl&amp;page=$i2\">Page $i2</option>";
          }
         }
        }
    
        $pages .= "</select>";
    
    
        $return['pages'] = $pages;
        $return['querylimit'] = " LIMIT $min, $ppp";
        $return['min'] = $min; //Used in users.php
        return $return;
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been doing some customization to this jQuery paging script I found here
I have been doing some research lately over my work project, i am trying
We have been doing some research into physically isolating the secure and non-secure sections
I have been doing some research on test driven development and find it pretty
I have been doing some research for using MSMQ. Following 2 gave me fundamental
I have been doing some x86 programming in Windows with NASM and I have
To practise some more bits of python I've been having a go at the
I have been doing some reading and have found out that the Java Swing
I have been doing some research on ID3V2 tags and Video Formats such as
I have been doing some unsafe bitmap operations and have found out that increasing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.