Category A to Z modification
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.
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.
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?
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
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.
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
Hi Fred,
To make a rewritten URL to the category index for each letter, look for the following line in .htaccess:
RewriteRule ^category/$ categories.phpand add the following line immediately afterwards:
RewriteRule ^category/([A-Z]{1})/$ categories.php?letter=$1Whilst 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=$1You 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.
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
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.
Hi David.
Iv done this modification and iv also done the modification in categories.php to show the amount(count) of catergories listed.
Though...
categories.php : woks fine.
categories.php?letter=A : does not, i only get the brackets "()"
I would like the categoriesAZ.php to show the amount of categories under each letter and categories.php?letter=A, to show
the amount of categories under each category.
could you advice :)
Tnx in advance &
Many tnx for a great script :)
//Kimi
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.
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?
Hi Mikey,
Yes - you will need to add the .htaccess ReWrite rules if you are using search engine friendly URLs...
Cheers,
David.
David,
I turned rewrite off and still the links don't appear.
Have a look www.dutchshops.nl
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.
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 Elcombe
Online Shopping For
The Big Business Directory
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.
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
Hi Stephen,
Got it - you need to change:
RewriteRule ^brand/([A-Z]{1})/$ brands.php?letter=$1to...
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.
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:
<?phprequire("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=BThe 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.