You are here:  » Category A to Z modification

Active Forum Topics


Category A to Z modification

Submitted by london on Sun, 2006-05-07 18:26 in

I was hoping I could get some advice on what changes would be needed to do the following as I have many categories, I don't want them to all appear on one page.

Instead I'd like category.php to contain an alphabetical list e.g

A
B
C
E
etc

When you click on a letter it would take you to a list of categories beginning with that letter.

Also, one other question I have a feed where the brand name is included as the first word within the product name, is there a filter to extract the first word of the product name and use this to populate the brand field?

Any advice much appreciated.

Submitted by support on Sun, 2006-05-07 20:40

Hi,

A Category index by letter is reasonably straight forward. I've had a quick go at this, and the easiest implementation is probably a new file to display the initial A-Z, and then a minor modification to the existing categories.php to select only categories beginning with a specified letter.

To try this out, create a new file called categoriesAZ.php:

<?php
  
require("includes/common.php");
  
$banner["h2"] = "<strong>".translate("Category")." A-Z</strong>";
  require(
"html/header.php");
  require(
"html/menu.php");
  require(
"html/searchform.php");
  require(
"html/banner.php");
  
$sql "SELECT DISTINCT(UCASE(SUBSTRING(category,1,1))) as letter FROM `".$config_databaseTablePrefix."products` ORDER BY category";
  if (
database_querySelect($sql,$rows))
  {
    foreach(
$rows as $letters)
    {
      if (
$config_useRewrite)
      {
        print 
"<p><a href='category/?letter=".$letters["letter"]."'>".$letters["letter"]."</a></p>";
      }
      else
      {
        print 
"<p><a href='categories.php?letter=".$letters["letter"]."'>".$letters["letter"]."</a></p>";
      }
    }
  }
  require(
"html/footer.php");
?>

Then, in categories.php, change the following line:

$sql = "SELECT DISTINCT(category) as category FROM `".$config_databaseTablePrefix."products` ORDER BY category";

to:

  if ($_GET["letter"])
  {
    $sql = "SELECT DISTINCT(category) as category FROM `".$config_databaseTablePrefix."products` WHERE category LIKE '".database_safe($_GET["letter"])."%' ORDER BY category";
  }
  else
  {
    $sql = "SELECT DISTINCT(category) as category FROM `".$config_databaseTablePrefix."products` ORDER BY category";
  }

The first script (the new one) will link to categories.php with the selected letter in the query string, for example:

categories.php?letter=B

The modification to categories.php will check for "letter" and use the modified query if it exists.

Regarding the extraction of the first word in the product name into the brand field, yes - that would be possible but slightly tricker to implement on anything other than an all or nothing basis. I'll have a think about the easiest way for you to do this...

Cheers,
David.

Submitted by london on Sun, 2006-05-07 23:48

Excellent, thanks again for your prompt and helpful response. Any advice on the first word product name extraction for particular feeds would be much appreciated.

Submitted by support on Mon, 2006-05-08 10:49

It is probably be possible to write a filter to do this; but the main complication I can think of would be where the brand was 2 words, like "Morphy Richards" for example.

Is this the case for any of the brands in your situation; or would it be possible to split the field at the first SPACE character?

Submitted by cwsteam on Mon, 2006-05-22 00:24

I tried to do the same thing, created the new file and made the said changes in categories.php

But when I open categoriesAZ.php

it says:
Parse error: syntax error, unexpected T_ELSE in /home/compare/public_html/categoriesAZ.php on line 16

Submitted by support on Mon, 2006-05-22 06:27

Hi,

There is a missing } from the code above - sorry about that! The corrected version is as follows:

<?php
  
require("includes/common.php");
  
$banner["h2"] = "<strong>".translate("Category")." A-Z</strong>";
  require(
"html/header.php");
  require(
"html/menu.php");
  require(
"html/searchform.php");
  require(
"html/banner.php");
  
$sql "SELECT DISTINCT(UCASE(SUBSTRING(category,1,1))) as letter FROM `".$config_databaseTablePrefix."products` ORDER BY category";
  if (
database_querySelect($sql,$rows))
  {
    foreach(
$rows as $letters)
    {
      if (
$config_useRewrite)
      {
        print 
"<p><a href='category/?letter=".$letters["letter"]."'>".$letters["letter"]."</a></p>";
      }
      else
      {
        print 
"<p><a href='categories.php?letter=".$letters["letter"]."'>".$letters["letter"]."</a></p>";
      }
    }
  }  
  require(
"html/footer.php");
?>

Cheers,
David.

Submitted by argof on Sat, 2006-09-30 06:33

Dear David
Thankyou for this script.

Two Requests:

1. How can I treat the above results in the htaccess file not to show eg. ?letter=A at the end of the url but only show say (modrewrite to) /A/

2. Could you please if possible give me an example of how I can achieve the above effect with the "brands" page (I have so many brands that my server can't handle the "brands" page size.

Thanks

Fred

Submitted by support on Sat, 2006-09-30 06:52

Hi Fred,

To make a rewritten URL to the category index for each letter, look for the following line in .htaccess:

RewriteRule ^category/$ categories.php

and add the following line immediately afterwards:

RewriteRule ^category/([A-Z]{1})/$ categories.php?letter=$1

Whilst you're in .htaccess, you might as well add the required code for the brands version as well:

RewriteRule ^brand/([A-Z]{1})/$ brands.php?letter=$1

You would also then want to change the URL generated by categoryAZ.php to use the new format, so change the following line:

        print "<p><a href='category/?letter=".$letters["letter"]."'>".$letters["letter"]."</a></p>";

to...

        print "<p><a href='category/".$letters["letter"]."/'>".$letters["letter"]."</a></p>";

To complete the brands version of this, use the following script (which includes the mod described above)...

brandsAZ.php:

<?php
  
require("includes/common.php");
  
$banner["h2"] = "<strong>".translate("Brand")." A-Z</strong>";
  require(
"html/header.php");
  require(
"html/menu.php");
  require(
"html/searchform.php");
  require(
"html/banner.php");
  
$sql "SELECT DISTINCT(UCASE(SUBSTRING(brand,1,1))) as letter FROM `".$config_databaseTablePrefix."products` ORDER BY brand";
  if (
database_querySelect($sql,$rows))
  {
    foreach(
$rows as $letters)
    {
      if (
$config_useRewrite)
      {
        print 
"<p><a href='brand/".$letters["letter"]."/'>".$letters["letter"]."</a></p>";
      }
      else
      {
        print 
"<p><a href='brands.php?letter=".$letters["letter"]."'>".$letters["letter"]."</a></p>";
      }
    }
  }
  require(
"html/footer.php");
?>

Finally, the similar modifications to brands.php must be made if the letter parameter is present in the URL, so change the following line (line 6 in the distribution):

$sql = "SELECT DISTINCT(brand) as brand FROM `".$config_databaseTablePrefix."products` ORDER BY brand";

to:

  if ($_GET["letter"])
  {
    $sql = "SELECT DISTINCT(brand) as brand FROM `".$config_databaseTablePrefix."products` WHERE brand LIKE '".database_safe($_GET["letter"])."%' ORDER BY brand";
  }
  else
  {
    $sql = "SELECT DISTINCT(brand) as brand FROM `".$config_databaseTablePrefix."products` ORDER BY brand";
  }

Hope this helps!
Cheers,
David.

Submitted by argof on Sat, 2006-09-30 09:21

David Thanks

I am getting there.
Now I am getting eg.
/category/F/ - this works
but the links on this page link to eg. /category/F/Family/
BUT when clicking on /category/F/Family/ I get (no results found)

I need the links on eg. page /category/F/
to link to /category/Family/ and not to /category/F/Family/

The same applies to the links on page /brand/A/
eg. /brand/A/Azzaro/ needs to be /brand/Azzaro/

Additional Request
The aplphateical list on categoryAZ.php and brandsAZ.php are in one colomn I would like to be able to adjust this how do I do this.

Thank you very much for the great support.

Fred

Submitted by support on Sat, 2006-09-30 09:47

Ah yes - that's because when rewrite is used the follow on links expect you to be in the /category/ or /brand/ directory.

To fix this, look for the following line in categories.php:

$item["href"] = tapestry_hyphenate($product["category"])."/";

...and change it to:

$item["href"] = "../".tapestry_hyphenate($product["category"])."/";

And in brands.php, find:

$item["href"] = tapestry_hyphenate($product["brand"])."/";

...and change it to:

$item["href"] = "../".tapestry_hyphenate($product["brand"])."/";

All that does is add the ../ to the URL which will make it go back up a level.

To create columns for the A-Z, you can do the same thing as atoz.php, similar to this:

<?php
  
require("includes/common.php");
  
$banner["h2"] = "<strong>".translate("Category")." A-Z</strong>";
  require(
"html/header.php");
  require(
"html/menu.php");
  require(
"html/searchform.php");
  require(
"html/banner.php");
  
$sql "SELECT DISTINCT(UCASE(SUBSTRING(category,1,1))) as letter FROM `".$config_databaseTablePrefix."products` ORDER BY category";
  
// set the number of columns you want here
  
$columns 2;
  
$currentColumn 0;
  print 
"<table>";
  print 
"<tr>";
  if (
database_querySelect($sql,$rows))
  {
    foreach(
$rows as $letters)
    {
      if (
$currentColumn == $columns)
      {
        print 
"</tr>";
        print 
"<tr>";
        
$currentColumn 0;
      }
      print 
"<td>";
      if (
$config_useRewrite)
      {
        print 
"<p><a href='category/?letter=".$letters["letter"]."'>".$letters["letter"]."</a></p>";
      }
      else
      {
        print 
"<p><a href='categories.php?letter=".$letters["letter"]."'>".$letters["letter"]."</a></p>";
      }
      print 
"</td>";
      
$currentColumn++;
    }
  }
  print 
"</td>";
  print 
"</tr>";
  print 
"</table>";
  require(
"html/footer.php");
?>

Cheers,
David.

Submitted by ka on Thu, 2006-12-14 17:10

Hi David.

Submitted by support on Thu, 2006-12-14 17:27

Hello Kimi,

Can you email me the URL to your site and also your modified categories.php and your new categoriesAZ.php and i'll take a look for you....

If you reply to your registration code or forum reg email is the easiest way to reach me!

Cheers,
David.

Submitted by MikeyC on Mon, 2007-09-03 19:51

Hi David,

I made these files and included them into merchants.php, categories.php and brands.php, accept that not all the links show up on the requested letter page.
In brands.php the letters show up, but none of the links does. The other pages some show and some don't.
Is this due to the fact i am using rewrite and i still need to add the rewrite url to .htaccess for the letter pages?

Submitted by support on Mon, 2007-09-03 19:54

Hi Mikey,

Yes - you will need to add the .htaccess ReWrite rules if you are using search engine friendly URLs...

Cheers,
David.

Submitted by MikeyC on Mon, 2007-09-03 20:08

David,

I turned rewrite off and still the links don't appear.
Have a look www.dutchshops.nl

Submitted by support on Mon, 2007-09-03 20:10

Hi Mike,

It looks like the conditional code is missing from the top of the files.

Can you email me your modified categories.php and i'll take a quick look for you....

(reply to your reg code or forum registration email is the easiest way to get me)

Cheers,
David.

Submitted by tbbd2007 on Sat, 2008-03-08 17:14

David,

I have used all the code as above, but have some odd results. All the letter links work on all the pages, but on the merchant numbers I get no results and on the brand numbers I get products. Please go to http://www.online-shopping-for.co.uk/merchantsaz.php and http://www.online-shopping-for.co.uk/brandsaz.php.

Thanks

Stephen
Online Shopping For
The Big Business Directory

Submitted by support on Sat, 2008-03-08 17:20

Hi Stephen,

This looks like a rewrite issue, because the URL is correct. For some reason the numeric ones are being rewritten to search.php instead of (for example) brands.php.

Can you post your modified .htaccess and I'll take a look...

Cheers,
David.

Submitted by tbbd2007 on Sat, 2008-03-08 17:33

David,

Thanks, .htaccess is:

Options -MultiViews
ErrorDocument 300 http://www.online-shopping-for.co.uk/index.php
ErrorDocument 400 http://www.online-shopping-for.co.uk/index.php
ErrorDocument 403 http://www.online-shopping-for.co.uk/index.php
ErrorDocument 404 http://www.online-shopping-for.co.uk/index.php
ErrorDocument 500 http://www.online-shopping-for.co.uk/index.php
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.online-shopping-for.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.online-shopping-for.co.uk/$1 [L,R=301]
RewriteRule ^product/(.*).html$ products.php?q=$1&rewrite=1&%{QUERY_STRING} [L]
RewriteRule ^review/(.*).html$ reviews.php?q=$1&rewrite=1&%{QUERY_STRING} [L]
RewriteRule ^merchant/$ merchants.php
RewriteRule ^merchant/([A-Z]{1})/$ merchants.php?letter=$1
RewriteRule ^merchant/(.*)/$ search.php?q=merchant:$1:&rewrite=1%{QUERY_STRING} [L]
RewriteRule ^merchant/(.*)/(.*).html$ search.php?q=merchant:$1:&page=$2&rewrite=1%{QUERY_STRING} [L]
RewriteRule ^category/$ categories.php
RewriteRule ^category/([A-Z]{1})/$ categories.php?letter=$1
RewriteRule ^category/(.*)/$ search.php?q=category:$1:&rewrite=1%{QUERY_STRING} [L]
RewriteRule ^category/(.*)/(.*).html$ search.php?q=category:$1:&page=$2&rewrite=1%{QUERY_STRING} [L]
RewriteRule ^brand/$ brands.php
RewriteRule ^brand/([A-Z]{1})/$ brands.php?letter=$1
RewriteRule ^brand/(.*)/$ search.php?q=brand:$1:&rewrite=1%{QUERY_STRING} [L]
RewriteRule ^brand/(.*)/(.*).html$ search.php?q=brand:$1:&page=$2&rewrite=1%{QUERY_STRING} [L]
RewriteRule ^search/(relevance|priceAsc|priceDesc|rating)/(.*)/$ search.php?sort=$1&q=$2&rewrite=1&%{QUERY_STRING} [L]
RewriteRule ^search/(relevance|priceAsc|priceDesc|rating)/(.*)/(.*).html$ search.php?sort=$1&q=$2&page=$3&rewrite=1&%{QUERY_STRING} [L]

Thanks

Stephen

Submitted by support on Sat, 2008-03-08 17:37

Hi Stephen,

Got it - you need to change:

RewriteRule ^brand/([A-Z]{1})/$ brands.php?letter=$1

to...

RewriteRule ^brand/([0-9A-Z]{1})/$ brands.php?letter=$1

(and the same change for merchant and category)

Because 0-9 was not in the range being matched is was not being picked up by the rewrite and therefore falling through the search rewrite lower down!

Cheers,
David.

Submitted by tbbd2007 on Sat, 2008-03-08 17:38

Thanks!

Stephen

Submitted by paddyman on Sat, 2010-03-06 12:06

Hi David,

Can you advise how to add the A to Z characters in a row at the top of the categories page. Have tried to include categoriesAZ.php in categories.php but getting errors !!

Any ideas ?

Thanks

Adrian

Submitted by support on Sat, 2010-03-06 13:18

Hi Adrian,

Would you like to email me your combined file and let me know what error is being displayed and I'll check it out for you...

Cheers,
David.

Submitted by slider1683 on Wed, 2014-02-26 10:27

Hi David,

It's possible to display the first 10 results for each letter on the page http://www.example.com/brands/? With a link : "see more results for this letter".

Like this site for example: {link saved}

Thank you in advance

Submitted by support on Wed, 2014-02-26 11:00

Hi,

The code should be straight forward, but as they're part of the same module would you like each of the A-Z indexes (Merchant / Category / Brand) to have this functionality, or brands only?

Cheers,
David.
--
PriceTapestry.com

Submitted by slider1683 on Wed, 2014-02-26 11:09

For all.

Thanks

Submitted by support on Wed, 2014-02-26 12:41

Hi,

I've worked out the code but requires changes in a few placed to be done tidily, I know you've been using the script for many years so if you'd like to email me your current html/atoz.php I'll merge the modification for you...

Cheers,
David.
--
PriceTapestry.com