You are here:  » One result per merchant?

Support Forum



One result per merchant?

Submitted by Harvey on Sun, 2008-03-16 12:03 in

Hi,

In products.php, my line 12 is:

$sql = "SELECT * FROM `".$config_databaseTablePrefix."products` WHERE name LIKE '%".database_safe($q)."%' ORDER BY price LIMIT ".$config_resultsPerPage;

I use "WHERE name LIKE" because my feeds use unique model numbers - so rather usefully the script then includes differently formatted model numbers from different feeds.

Is there a way to edit the above code to display only one result (ideally the lowest priced) for each merchant?

Thanks

Submitted by support on Sun, 2008-03-16 14:29

Hi Stephen,

This would be best done after the query. The following line is currently:

$numRows = database_querySelect($sql,$rows);

Try changing this to the following code:

$newRows = array();
$merchants = array();
if (database_querySelect($sql,$rows))
{
  foreach($rows as $row)
  {
    if (!$merchants[$row["merchant"]])
    {
      $newRows[] = $row;
      $merchants[$row["merchant"]] = 1;
    }
  }
}
$rows = $newRows;
$numRows = count($rows);

Hope this helps!
Cheers,
David.