You are here:  » Listing Brands in 2 columns

Support Forum



Listing Brands in 2 columns

Submitted by paddyman on Thu, 2008-08-28 23:33 in

Hi David,

I have all of my brands listed in a table on the left of my page as follows.

<?php
        $sql 
"SELECT DISTINCT(brand) as brand FROM `".$config_databaseTablePrefix."products` ORDER BY brand";
        if (
database_querySelect($sql,$rows))
        {
          foreach(
$rows as $row)
          {
            if (
$config_useRewrite)
            {
              
$href $config_baseHREF."brand/".tapestry_hyphenate($row["brand"])."/";
            }
            else
            {
              
$href $config_baseHREF."search.php?q=brand:".urlencode($row["brand"]).":";
            }
            print 
"<a href='".$href."'>".$row["brand"]."</a><br />";
          }
        }
?>

So Currently have

Brand1
Brand2
Brand3
Brand4

Trying to save space so wondering if its possible to list the brands in 2 columns within this table, having

Brand1 Brand3
Brand2 Brand4

Any ideas?

Thanks

Adrian

Submitted by support on Fri, 2008-08-29 07:33

Hi Adrian,

Have a go with this;

<?php
        $sql 
"SELECT DISTINCT(brand) as brand FROM `".$config_databaseTablePrefix."products` ORDER BY brand";
        if (
database_querySelect($sql,$rows))
        {
          print 
"<table border='0'>";
          print 
"<tr>";
          
$column 0;
          foreach(
$rows as $row)
          {
            if (
$config_useRewrite)
            {
              
$href $config_baseHREF."brand/".tapestry_hyphenate($row["brand"])."/";
            }
            else
            {
              
$href $config_baseHREF."search.php?q=brand:".urlencode($row["brand"]).":";
            }
            print 
"<td><a href='".$href."'>".$row["brand"]."</a></td>";
            
$column++;
            if (
$column==2)
            {
              print 
"</tr>";
              print 
"<tr>";
              
$column 0;
            }
          }
          print 
"</tr>";
          print 
"</table>";
        }
?>

Cheers,
David.

Submitted by paddyman on Fri, 2008-08-29 07:47

Hi David,

Thanks for that, works great :)

Cheers

Adrian

Submitted by paddyman on Mon, 2008-10-27 15:44

Hi David,

Do you know is there anyway to exclude a certain brand from being displayed. Lets say the above code displys

Sony
Canon
Fuji
Panasonic

Is there anyway I could just disokay Canon and Fuji and leave out the other 2 brands?

Thanks

Adrian

Submitted by support on Mon, 2008-10-27 16:04

Hi Adrian,

Sure - you can just skip each loop iteration conditionally. Where you have:

  foreach($rows as $row)
  {

..replace this with:

  foreach($rows as $row)
  {
    if (
       $row["brand"]=="Sony"
       ||
       $row["brand"]=="Panasonic"
       ) continue;

Cheers,
David.

Submitted by paddyman on Mon, 2008-10-27 16:14

Thanks David,

If I had a large number of brands and still only wanted to display two brands, is there anyway I could specify which brands to include rather that the brands to be excluded?

Thanks

Adran

Submitted by support on Mon, 2008-10-27 16:18

Hi Adrian,

Assuming that you need to be sure products for those brands exist in the database (otherwise it could be done without any database code of course) - then the SQL could be changed as follows:

        $sql = "SELECT DISTINCT(brand) as brand FROM `".$config_databaseTablePrefix."products` WHERE BRAND IN ('Canon','Fuji') ORDER BY brand";

(and you wouldn't need any other mod, so could undo the code version described above)

Cheers,
David.