You are here:  » Sort by and search within


Sort by and search within

Submitted by chrisst1 on Fri, 2012-10-12 14:02 in

Hi David

Hope you are better after your accident. I've been trying to sort the results by the $record["program_name"] and add a keyword search but getting in a right pickle. This is what i've got so far (which works fine), could you give some guidance please.

<?php
  
require("/var/www/vhosts/###########.com/httpdocs/##########/pt/includes/MagicParser.php");
  print 
"<h1>Voucher Feeds</h1>";
  print 
"<h2>Affiliate Window</h2>";
  if (isset(
$_GET["page"]))
  {
    
$page $_GET["page"];
  }
  else
  {
    
$page 1;
  }
  
$counter 0;
  
$itemsOnPage 20;
  print 
"<div class='table-list'>";
  print 
"<table>";
  print 
"<thead>";
  print 
"<tr>";
  print 
"<th style='white-space: nowrap'>Merchant</th>";
  print 
"<th style='white-space: nowrap'>ID</th>";
  print 
"<th style='white-space: nowrap'>Voucher Code</th>";
  print 
"<th style='white-space: nowrap'>Description</th>";
  print 
"<th style='white-space: nowrap'>Land URL</th>";
  print 
"<th style='white-space: nowrap'>Valid From</th>";
  print 
"<th style='white-space: nowrap'>Valid To</th>";
  print 
"<th style='white-space: nowrap'>Region</th>";
  print 
"</tr>";
  print 
"</thead>";
  
// default to the first page
  
function myRecordHandler($record)
  {
    global 
$page;
    global 
$counter;
    global 
$itemsOnPage;
    
$counter++;
    
// return false whilst parsing items on previous pages
    
if ($counter < (($page-1)*$itemsOnPage)) return false;
    
// display the item
    
print "<tr>";
    print 
"<td align='left'>".$record["program_name"]."</td>";
    print 
"<td align='left'>".$record["merchant_id"]."</td>";
    print 
"<td align='left'>".$record["code"]."</td>";
    print 
"<td align='left'>".$record["description"]."</td>";
    print 
"<td align='left'>".$record["url"]."</td>";
    print 
"<td align='left'>".$record["start_date"]."</td>";
    print 
"<td align='left'>".$record["end_date"]."</td>";
    print 
"<td align='left'>".$record["region"]."</td>";
    print 
"</tr>";
    
// return true if counter reaches current page * items on each page
    
return ($counter == ($page*$itemsOnPage));
  }
  
// parse the feed
  
MagicParser_parse("{code saved}","myRecordHandler","csv|44|1|34");
  print 
"<tfoot>";
  print 
"<tr>";
  print 
"<td colspan='8'>";
  print 
"<a href='?page=".($page-1)."'>Previous</a>&nbsp;&nbsp;";
  print 
"<a href='?page=".($page+1)."'>Next</a>";
  print 
"</td>";
  print 
"</tr>";
  print 
"</tfoot>";
  print 
"</table>";
  print 
"</div>";
?>

Thanks Chris

Submitted by support on Fri, 2012-10-12 15:01

Hi Chris,

Well on the mend thanks!

One tidy and very quick tecnique to use is to combine $record into single string using PHP's serialize() function; and then perform a substr() test to see if your search keyword is containined within the result. If so, display the record as per your existing code; otherwise simply "return" and ignore the record as it does not match the search.

Have a go with something like this:

<?php
  
require("/var/www/vhosts/###########.com/httpdocs/##########/pt/includes/MagicParser.php");
  print 
"<h1>Voucher Feeds</h1>";
  print 
"<h2>Affiliate Window</h2>";
  if (isset(
$_GET["page"]))
  {
    
$page $_GET["page"];
  }
  else
  {
    
$page 1;
  }
  if (isset(
$_GET["q"]))
  {
    
$q $_GET["q"];
  }
  else
  {
    
$q "";
  }
  
$counter 0;
  
$itemsOnPage 20;
  
// replace thisscript.php with name of this script...
  
print "<form method='GET' action='thisscript.php'>";
  print 
"Search: <input type='text' name='q' value='".htmlentities($q,ENT_QUOTES)."' />";
  print 
"<input type='submit' value='Search' />";
  print 
"</form>";
  print 
"<div class='table-list'>";
  print 
"<table>";
  print 
"<thead>";
  print 
"<tr>";
  print 
"<th style='white-space: nowrap'>Merchant</th>";
  print 
"<th style='white-space: nowrap'>ID</th>";
  print 
"<th style='white-space: nowrap'>Voucher Code</th>";
  print 
"<th style='white-space: nowrap'>Description</th>";
  print 
"<th style='white-space: nowrap'>Land URL</th>";
  print 
"<th style='white-space: nowrap'>Valid From</th>";
  print 
"<th style='white-space: nowrap'>Valid To</th>";
  print 
"<th style='white-space: nowrap'>Region</th>";
  print 
"</tr>";
  print 
"</thead>";
  
// default to the first page
  
function myRecordHandler($record)
  {
    global 
$q;
    global 
$page;
    global 
$counter;
    global 
$itemsOnPage;
    
// if form submitted with $q parameter search in $record for keyword
    // return if not found
    
if ($q)
    {
      
$search serialize($record);
      if (
stripos($search,$q)===FALSE) return;
    }
    
$counter++;
    
// return false whilst parsing items on previous pages
    
if ($counter < (($page-1)*$itemsOnPage)) return false;
    
// display the item
    
print "<tr>";
    print 
"<td align='left'>".$record["program_name"]."</td>";
    print 
"<td align='left'>".$record["merchant_id"]."</td>";
    print 
"<td align='left'>".$record["code"]."</td>";
    print 
"<td align='left'>".$record["description"]."</td>";
    print 
"<td align='left'>".$record["url"]."</td>";
    print 
"<td align='left'>".$record["start_date"]."</td>";
    print 
"<td align='left'>".$record["end_date"]."</td>";
    print 
"<td align='left'>".$record["region"]."</td>";
    print 
"</tr>";
    
// return true if counter reaches current page * items on each page
    
return ($counter == ($page*$itemsOnPage));
  }
  
// parse the feed
  
MagicParser_parse("{code saved}","myRecordHandler","csv|44|1|34");
  
// make sure q propagated to next / prev links
  
print "<tfoot>";
  print 
"<tr>";
  print 
"<td colspan='8'>";
  print 
"<a href='?page=".($page-1)."&q=".urlencode($q)."'>Previous</a>&nbsp;&nbsp;";
  print 
"<a href='?page=".($page+1)."&q=".urlencode($q)."'>Next</a>";
  print 
"</td>";
  print 
"</tr>";
  print 
"</tfoot>";
  print 
"</table>";
  print 
"</div>";
?>

Hope this helps!
Cheers,
David.

Submitted by chrisst1 on Fri, 2012-10-12 15:20

Brilliant, that work great.

Using that in conjunction with our PT admin could it be possible to add an extra dropdown field to the search form populated with registered merchants.

Thanks David

Submitted by support on Mon, 2012-10-15 08:56

Hi Chris,

If the code combined into Price Tapestry page then you could first extend as follows to incorporate merchant into the search (with option for all merchants)...:

<?php
  
require("/var/www/vhosts/###########.com/httpdocs/##########/pt/includes/MagicParser.php");
  print 
"<h1>Voucher Feeds</h1>";
  print 
"<h2>Affiliate Window</h2>";
  if (isset(
$_GET["page"]))
  {
    
$page $_GET["page"];
  }
  else
  {
    
$page 1;
  }
  if (isset(
$_GET["q"]))
  {
    
$q $_GET["q"];
  }
  else
  {
    
$q "";
  }
  if (isset(
$_GET["merchant"]))
  {
    
$merchant $_GET["merchant"];
  }
  else
  {
    
$merchant "";
  }
  
$counter 0;
  
$itemsOnPage 20;
  
// replace thisscript.php with name of this script...
  
print "<form method='GET' action='thisscript.php'>";
  print 
"Search: <input type='text' name='q' value='".htmlentities($q,ENT_QUOTES)."' />";
  print 
"<input type='submit' value='Search' />";
  
$sql "SELECT DISTINCT(merchant) FROM `".$config_databaseTablePrefix."products` ORDER BY merchant";
  
database_querySelect($sql,$rows);
  print 
"<select name='merchant'>";
  print 
"<option value=''>All merchants</option>";
  foreach(
$rows as $row)
  {
    print 
"<option value='".htmlentities($row["merchant"],ENT_QUOTES)."'>".$row["merchant"]."</option>";
  }
  print 
"</form>";
  print 
"<div class='table-list'>";
  print 
"<table>";
  print 
"<thead>";
  print 
"<tr>";
  print 
"<th style='white-space: nowrap'>Merchant</th>";
  print 
"<th style='white-space: nowrap'>ID</th>";
  print 
"<th style='white-space: nowrap'>Voucher Code</th>";
  print 
"<th style='white-space: nowrap'>Description</th>";
  print 
"<th style='white-space: nowrap'>Land URL</th>";
  print 
"<th style='white-space: nowrap'>Valid From</th>";
  print 
"<th style='white-space: nowrap'>Valid To</th>";
  print 
"<th style='white-space: nowrap'>Region</th>";
  print 
"</tr>";
  print 
"</thead>";
  
// default to the first page
  
function myRecordHandler($record)
  {
    global 
$q;
    global 
$merchant;
    global 
$page;
    global 
$counter;
    global 
$itemsOnPage;
    
// if form submitted with $q parameter search in $record for keyword
    // return if not found
    
if ($q)
    {
      
$search serialize($record);
      if (
stripos($search,$q)===FALSE) return;
    }
    if (
$merchant)
    {
      if (
$merchant <> $record["program_name"]) return;
    }
    
$counter++;
    
// return false whilst parsing items on previous pages
    
if ($counter < (($page-1)*$itemsOnPage)) return false;
    
// display the item
    
print "<tr>";
    print 
"<td align='left'>".$record["program_name"]."</td>";
    print 
"<td align='left'>".$record["merchant_id"]."</td>";
    print 
"<td align='left'>".$record["code"]."</td>";
    print 
"<td align='left'>".$record["description"]."</td>";
    print 
"<td align='left'>".$record["url"]."</td>";
    print 
"<td align='left'>".$record["start_date"]."</td>";
    print 
"<td align='left'>".$record["end_date"]."</td>";
    print 
"<td align='left'>".$record["region"]."</td>";
    print 
"</tr>";
    
// return true if counter reaches current page * items on each page
    
return ($counter == ($page*$itemsOnPage));
  }
  
// parse the feed
  
MagicParser_parse("{code saved}","myRecordHandler","csv|44|1|34");
  
// make sure q propagated to next / prev links
  
print "<tfoot>";
  print 
"<tr>";
  print 
"<td colspan='8'>";
  print 
"<a href='?page=".($page-1)."&q=".urlencode($q)."'>Previous</a>&nbsp;&nbsp;";
  print 
"<a href='?page=".($page+1)."&q=".urlencode($q)."'>Next</a>";
  print 
"</td>";
  print 
"</tr>";
  print 
"</tfoot>";
  print 
"</table>";
  print 
"</div>";
?>

Cheers,
David
--
MagicParser.com