PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Nov 23, 2017 1:43 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Wed Oct 12, 2016 5:36 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4359
Location: United Kingdom
$numrows here for one particular keyword is showing 33 rows.
Yet, it is only display One page.
Surely it should show 33 pages??
Any ideas why?
Syntax: [ Download ] [ Hide ]
 if ($category == "all")
  {
        $query   = "SELECT count(id) AS numrows FROM products WHERE (title LIKE :search OR description LIKE :search OR metakeywords LIKE :search OR romancode LIKE :search OR MATCH(title) AGAINST (:searchmatch)) AND pause <> 'on'";
$result  = $pdo->prepare($query);
  $result->execute(array(':search' => $search, ':searchmatch' => $search));
        }
        else
        {
        $query   = "SELECT count(id) AS numrows FROM products WHERE (title LIKE :search OR description LIKE :search OR metakeywords LIKE :search OR romancode LIKE :search OR MATCH(title) AGAINST (:searchmatch)) AND pause <> 'on' AND catname =:category";
        $result = $pdo->prepare($query);
  $result->execute(array(':search' => $search, ':searchmatch' => $search, ':category' => $category));
        }
  $numrows = $result->fetchColumn();
  echo "<font color='#ffffff'>$numrows</font>";
$maxPage = ceil($numrows/$rowsPerPage);

$category = str_replace(" ", "+", $category);

// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav  = '';
 
for($page = 1; $page <= $maxPage; $page++)
{
   if ($page == $pageNum)
   {
      $nav .= " <div class='pagelinkactive'>$page</div> "; // no need to create a link to current page
   }
   else
   {
      $nav .= " <a href=\"/index.php?page=search&search=$search&category=$category&pagenum=$page\" class='pagelink'>$page</a>";
   }
}
 
// creating previous and next link
// plus the link to go straight to
// the first and last page
 
if ($pageNum > 1)
{
   $page  = $pageNum - 1;
   $prev  = " <a href=\"/index.php?page=search&search=$search&category=$category&pagenum=$page\" class='pagelink'>Prev</a> ";
 
   $first = " <a href=\"/index.php?page=search&search=$search&category=$category\" class='pagelink'>First Page</a>";
}
else
{
   $prev  = '&nbsp;'; // we're on page one, don't print previous link
   $first = '&nbsp;'; // nor the first page link
}
 
if ($pageNum < $maxPage)
{
   $page = $pageNum + 1;
   $next = " <a href=\"/index.php?page=search&search=$search&category=$category&pagenum=$page\" class='pagelink'>Next</a>";
 
   $last = " <a href=\"/index.php?page=search&search=$search&category=$category&pagenum=$maxPage\" class='pagelink'>Last Page</a>";
}
else
{
   $next = '&nbsp;'; // we're on the last page, don't print next link
   $last = '&nbsp;'; // nor the last page link
}
if (isset($search))
{
// print the navigation link
echo "<div class='navpages'>" . $first . $prev . $nav . $next . $last . "</div>";    
}

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Wed Oct 12, 2016 5:44 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4359
Location: United Kingdom
The script at the top is generating the products, 32, 64 or 128 per page (currently set at 128).
the one at the bottom is for the page numbering. And yet. if you enter just three characters on which to search by, it only brings up one page.

If you search for a bigger one, it brings up many more.

Why is that?

Syntax: [ Download ] [ Hide ]
<script>
function toggleDiv(divId) {
   $("#"+divId).toggle();
}

$(document).click(function() {
   
});
$("#txthint").click(function(e) {
    e.stopPropagation(); // This is the preferred method.
    return false;        // This should not be used unless you do not want
                         // any click events registering inside the div
});

</script>
<?php
include "unveil.php";
$search = isset($_GET['search']) ? $_GET['search'] : null;
$category = isset($_GET['category']) ? $_GET['category'] : null;
$categorysearch = str_replace("+", " ", $category);
$detect = new Mobile_Detect;

// by default we show first page
$pageNum = 1;
 
// if $_GET['pagenum'] defined, use it as page number
if(isset($_GET['pagenum']))
{
    $pageNum = $_GET['pagenum'];
}

if ( $detect->isMobile() && !$detect->isTablet() ) {
          // how many rows to show per page
$rowsPerPage = 12;
}
else
{
          // how many rows to show per page
$rowsPerPage = 128;
}

$offset = ($pageNum - 1) * $rowsPerPage;       

echo "<div class='head'><h1>Search: ";?><?= htmlspecialchars($search); ?><?php echo " <i class='fa fa-search' aria-hidden='true' style='color: #ff5500'></i></h1>";
if (isset($category) && $category <> "all") { echo "<h2>";?><?= htmlspecialchars($categorysearch); ?><?php echo "</h2>";}
echo "</div>
<div class='home-search'>
<div class='home-search-inner'>
<form method='GET' action='/index.php'>
<input type='hidden' name='page' value='search'>
<div class='home-search-icon'><i class='fa fa-search' aria-hidden='true'></i></div>
<div class='home-search-input'> <input type='text' name='search' autocomplete='off' placeholder='Search the site'"
;
if (isset($search)) { echo " value='";
?><?= htmlspecialchars($search); ?><?php
echo "'";}
echo ">
<div id='txthint'></div>
</div>
<div class='home-search-cat'><select name='category'>
<option value='all'>All Categories</option>"
;

  $query = "SELECT DISTINCT catname FROM products WHERE pause = 'off' ORDER BY catname";
  $result = $pdo->query($query);
  while ($row = $result->fetch(PDO::FETCH_OBJ))
      {
     
      echo "<option value='$row->catname'";
      if (isset($categorysearch))
      {
      if ($row->catname == $categorysearch) { echo " selected='selected'";}
      }
      echo ">$row->catname</option>";
      }

echo "</select></div>
<div class='home-search-submit'><input type='submit' value='search'></div>
</form>
<div style='clear: both'></div>
</div></div>"
;

if (isset($category))
{
  if ($category == "all")
  {
  $query = "SELECT * FROM products WHERE (title LIKE :search OR description LIKE :search OR metakeywords LIKE :search OR romancode LIKE :search OR MATCH(title) AGAINST (:searchmatch)) AND pause <> 'on' ORDER BY rcstock = 'in stock' DESC,  rcstock = '' DESC, title LIMIT $offset, $rowsPerPage;";
  $result = $pdo->prepare($query);
  $result->execute(array(':search' => "%{$search}%", ':searchmatch' => $search));
  }
  else
  {
  $category = str_replace("+", " ", $category);
  $query = "SELECT * FROM products WHERE (title LIKE :search OR description LIKE :search OR metakeywords LIKE :search OR romancode LIKE :search OR MATCH(title) AGAINST (:searchmatch)) AND pause <> 'on' AND catname =:category ORDER BY rcstock = 'in stock' DESC,  rcstock = 'out of stock' DESC, title LIMIT $offset, $rowsPerPage;";
  $result = $pdo->prepare($query);
  $result->execute(array(':search' => "%{$search}%", ':searchmatch' => $search, ':category' => $category));
  }
}

$num_rows = $result->rowCount();
echo "<font color='#ffffff'>$num_rows</font>";
$count = 0;
$todaydate = date('Y-m-d');
echo "<div class='thumbs-container'>";
while ($row = $result->fetch(PDO::FETCH_OBJ))
      {
       $count ++;
      $todaydate = date('Y-m-d');
      $newdate = strtotime("$todaydate");
      $dateenduse = strtotime("$row->datepricedrop");
      $datebackinstock = strtotime("$row->datebackinstock");
      $i=30;
      $checkBackinstock = strtotime(date("Y-m-d", strtotime($row->datebackinstock)) . " +".$i."days");

      if ($checkBackinstock >= $newdate) { $backinstock = "enable";}
      if ($row->comingsoon != "yes")
        {
        if ($row->pricedrop != '' && $newdate < $dateenduse) { $pricedrop = "enable";}
        }
           
      $titlereplace = str_replace(" ", "-", $row->title);
      $categreplace = str_replace(" ", "-", $row->catname);
      $subcategreplace = str_replace(" ", "-", $row->subname);  
      echo "<div class='thumbs-tile'><a href='/product/$categreplace/$subcategreplace/$row->id/$titlereplace'>
      <div class='thumbs-tile-colored'>
      <div class='thumbs-tile-colored-inner'>
      "
;
 
  if($row->preorder == "yes" && isset($row->preorderdate))
    {
    if ($row->preorderdate > $todaydate)
      {
      $preorderdateyear = date( 'Y', strtotime($row->preorderdate));
      $preorderdatemonth = date( 'F', strtotime($row->preorderdate));
echo "<div class='thumb-tile-preorder'><i class='fa fa-clock-o'></i> DUE IN $preorderdatemonth $preorderdateyear</div>";
      }
    }

    if(isset($row->video) && $row->video <> '')
    {
      echo "<div class='thumbs-tile-video'><i class='fa fa-youtube-square fa-2x' aria-hidden='true' title='This product has a video presentation'></i></div>";
    }
     
    if(isset($backinstock) && $row->rcstock == "in stock")
    {
    if ($backinstock == "enable")
      {
      echo "<div class='thumb-tile-backinstock'>&#10004; BACK IN STOCK</div>";
      }
    }
   

  if(isset($pricedrop))
    {
    if ($pricedrop == "enable")
      {
echo "<div class='thumb-tile-pricedrop'><i class='fa fa-caret-down'></i>&nbsp; PRICE DROP</div>";
      }
    }
     echo "</div></div>";  
    if (isset($row->bundleroman1) && $row->bundleroman1 != '')
{
if (isset($row->photoprimary) && $row->photoprimary != '')
{
echo "<img src='/images/unveil.jpg'  alt='$row->title' data-src='/images/productphotos/small/$row->photoprimary' />";
}
else
{
echo "<img src='/images/blank_bundle.jpg'   alt='no image available' />";
}
}
else
{
if ($row->photoprimary == "" || $row->photoprimary == NULL)
        {
        echo "<img src='/images/blank.jpg'>";
        }
        else
        {
        echo "<img src='/images/unveil.jpg'  alt='$row->title' data-src='/images/productphotos/small/$row->photoprimary' />";
        }
        }
       
      echo "<div class='thumbs-tile-overlay'>
      <div class='thumbs-tile-overlay-inner'>
      <div class='thumbs-tile-title'>$row->title</div>
     
     <div class='thumbs-tile-price'><div class='thumbs-tile-stock'>"
;
     
      if ($row->preorder == "yes") { echo "pre-order only";}
else if ($row->comingsoon == "yes") { echo "coming soon";}
else if ($row->storeonly == "yes") { echo "In-Shop Only";}
else if ($row->bundleroman1 != NULL)
    {  

   $query1   = "SELECT rcstock FROM products WHERE romancode = :bundleroman1 AND rcstock = 'out of stock'";
        $result1  = $pdo->prepare($query1);
  $result1->execute(array(':bundleroman1' => $row->bundleroman1));
  $num_rows1 = $result1->rowCount();
    if ($num_rows1 != 0)
      {
      $bundlestock1 = "sold";
      }
      else { $bundlestock1 = NULL; }
     
     
     
        $query2   = "SELECT rcstock FROM products WHERE romancode = :bundleroman2 AND rcstock = 'out of stock'";
        $result2  = $pdo->prepare($query2);
  $result2->execute(array(':bundleroman2' => $row->bundleroman2));
  $num_rows2 = $result2->rowCount();
    if ($num_rows2 != 0)
      {
      $bundlestock2 = "sold";
      }
      else { $bundlestock2 = NULL; }
     
     
     
        $query3   = "SELECT rcstock FROM products WHERE romancode = :bundleroman3 AND rcstock = 'out of stock'";
        $result3  = $pdo->prepare($query3);
    $result3->execute(array(':bundleroman3' => $row->bundleroman3));
  $num_rows3 = $result3->rowCount();
    if ($num_rows3 != 0)
      {
      $bundlestock3 = "sold";
      }
      else { $bundlestock3 = NULL; }
     
     
     
        $query4   = "SELECT rcstock FROM products WHERE romancode = :bundleroman4 AND rcstock = 'out of stock'";
        $result4  = $pdo->prepare($query4);
  $result4->execute(array(':bundleroman4' => $row->bundleroman4));
  $num_rows4 = $result4->rowCount();
    if ($num_rows4 != 0)
      {
      $bundlestock4 = "sold";
      }
      else { $bundlestock4 = NULL; }
        $query5   = "SELECT rcstock FROM products WHERE romancode = :bundleroman5 AND rcstock = 'out of stock'";
        $result5  = $pdo->prepare($query5);
  $result5->execute(array(':bundleroman5' => $row->bundleroman5));
  $num_rows5 = $result5->rowCount();
    if ($num_rows5 != 0)
      {
      $bundlestock5 = "sold";
      }
      else { $bundlestock5 = NULL; }
     
    if ($bundlestock1 == "sold" || $bundlestock2 == "sold" || $bundlestock3 == "sold" || $bundlestock4 == "sold" || $bundlestock5 == "sold")
    {
    echo "out of stock";
    $bundlestock1 = null;
    $bundlestock2 = null;
    $bundlestock3 = null;
    $bundlestock4 = null;
    $bundlestock5 = null;
    }
    else
    {
    echo "in stock";
    }
    }
else
{
echo "$row->rcstock";
}

echo "</div>";
     
      if ($row->comingsoon != "yes")
  {    
  if(isset($pricedrop))
    {
    if ($pricedrop == "enable")
      {
      echo "<span class='fontred'><i class='fa fa-caret-down'></i>&nbsp;</span> Only &pound;";
      printf ("%.2f", $row->price);
      echo " &nbsp; Was <s>&pound;";
      printf ("%.2f", $row->pricedrop);
      echo "</s>";
      }
    }
    else
    {  
    if ($row->photoloadout == "")
      {
      echo "Only &pound;";
      printf ("%.2f", $row->price);
      }
    else
      {
      echo "Calculate on page";
      }
    }
  }
  else
  {
  echo "Price n/a";
  }
                       
      echo "</div></div>
      </div></a>
      </div>"
;
     
      // if not on a mobile, ensure a clear line after ever four items, incase of dodgy image heights
      if ( $detect->isMobile() && !$detect->isTablet() ) {
}
else
{
 if (($count % 4) == 0)
      {
      echo "<div style='clear:both'></div>";
      }
}
$pricedrop = NULL;
$backinstock = NULL;
     }
     echo "<div style='clear:both'></div></div>";
     if ($num_rows == 0)
     {
     echo "<div class='search-welcome'><h2>Nothing found....</h2>
     Sorry we couldn't find a result for you.<br/>Please try again.</div>"
;
     }
     if (!isset($search))
     {
     echo "<div class='search-welcome'><h2>Look for the best airsoft here....</h2>
     Search by category above.<br/>Enter your keyword into the box above, and opt for a category and 'Search'.</div>"
;
     
     
     $query = "SELECT catname, photoprimary FROM products WHERE pause = 'off' GROUP BY catname";
  $result = $pdo->query($query);

$count = 0;
while ($row = $result->fetch(PDO::FETCH_OBJ))
      {
      $categreplace = str_replace(" ", "-", $row->catname);      
      $count ++;
      echo "<div class='thumbs-tile'><a href='/categ/$categreplace'><img src='/images/unveil.jpg'  alt='$row->catname' data-src='/images/productphotos/small/$row->photoprimary' />
      <div class='thumbs-tile-overlay'>
        <div class='thumbs-tile-overlay-inner'>
      <div class='thumbs-tile-title'>$row->catname</div>
     </div>
      </div></a>
      </div>"
;
     
      // if not on a mobile, ensure a clear line after ever four items, incase of dodgy image heights
      if ( $detect->isMobile() && !$detect->isTablet() ) {
}
else
{
 if (($count % 4) == 0)
      {
      echo "<div style='clear:both'></div>";
      }
}}
     echo "<div style='clear:both'></div><br/>";
     
     }
     echo "<div style='clear:both'></div>";
     if (isset($search) && $num_rows > 0)
     {
     echo "<Br/>";
     }

$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
 
  if ($category == "all")
  {
        $query   = "SELECT count(id) AS numrows FROM products WHERE (title LIKE :search OR description LIKE :search OR metakeywords LIKE :search OR romancode LIKE :search OR MATCH(title) AGAINST (:searchmatch)) AND pause <> 'on'";
$result  = $pdo->prepare($query);
  $result->execute(array(':search' => $search, ':searchmatch' => $search));
        }
        else
        {
        $query   = "SELECT count(id) AS numrows FROM products WHERE (title LIKE :search OR description LIKE :search OR metakeywords LIKE :search OR romancode LIKE :search OR MATCH(title) AGAINST (:searchmatch)) AND pause <> 'on' AND catname =:category";
        $result = $pdo->prepare($query);
  $result->execute(array(':search' => $search, ':searchmatch' => $search, ':category' => $category));
        }
  $numrows = $result->fetchColumn();
 
$maxPage = ceil($numrows/$rowsPerPage);
echo "<font color='#ffffff'>$numrows | $maxPage</font>";
$category = str_replace(" ", "+", $category);

// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav  = '';
 
for($page = 1; $page <= $maxPage; $page++)
{
   if ($page == $pageNum)
   {
      $nav .= " <div class='pagelinkactive'>$page</div> "; // no need to create a link to current page
   }
   else
   {
      $nav .= " <a href=\"/index.php?page=search&search=$search&category=$category&pagenum=$page\" class='pagelink'>$page</a>";
   }
}
 
// creating previous and next link
// plus the link to go straight to
// the first and last page
 
if ($pageNum > 1)
{
   $page  = $pageNum - 1;
   $prev  = " <a href=\"/index.php?page=search&search=$search&category=$category&pagenum=$page\" class='pagelink'>Prev</a> ";
 
   $first = " <a href=\"/index.php?page=search&search=$search&category=$category\" class='pagelink'>First Page</a>";
}
else
{
   $prev  = '&nbsp;'; // we're on page one, don't print previous link
   $first = '&nbsp;'; // nor the first page link
}
 
if ($pageNum < $maxPage)
{
   $page = $pageNum + 1;
   $next = " <a href=\"/index.php?page=search&search=$search&category=$category&pagenum=$page\" class='pagelink'>Next</a>";
 
   $last = " <a href=\"/index.php?page=search&search=$search&category=$category&pagenum=$maxPage\" class='pagelink'>Last Page</a>";
}
else
{
   $next = '&nbsp;'; // we're on the last page, don't print next link
   $last = '&nbsp;'; // nor the last page link
}
if (isset($search))
{
// print the navigation link
echo "<div class='navpages'>" . $first . $prev . $nav . $next . $last . "</div>";    
}
                        ?>

<?php
if ( $detect->isMobile() && !$detect->isTablet() ) {
}
else
{
echo "<script>
// This really belongs in a separate JS file, not embedded alongside PHP or HTML
$(document).ready(function() {
$('.home-search-input input').on('keyup', function(event) {
  var search_string = $(this).val();

  // Don't fire on very short strings
  if (search_string.length > 2) {
    var category = $('.home-search-cat select').val();
    $.ajax({
                url: '/ajax-search.php?search=' + search_string + '&category=' + category,
                method: 'GET'
            }).done(function(response) {
                $('#txthint').html(response);
            });
  }
});

$('.home-search-cat select').change(function(event) {
        var category = $(this).val();
  var search_string = $('.home-search-input input').val();
    $.ajax({
                url: '/ajax-search.php?search=' + search_string + '&category=' + category,
                method: 'GET'
            }).done(function(response) {
                $('#txthint').html(response);
            });
});
});
</script>"
;
}
?>

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Wed Oct 12, 2016 6:02 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4359
Location: United Kingdom
More info - if I search for 3-4 characters of a word in the system, it comes up with results, but only one page. If I search for the full work, which is 6 characters, you get all the page numbers appear.

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Wed Oct 12, 2016 7:54 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4359
Location: United Kingdom
What a Muppet - missing Wildcard in lower query!!!!

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group