Sorting by Category Field in Different Template Files

8 posts by 2 authors in: Forums > CMS Builder
Last Post: March 1, 2010   (RSS)

By mark99 - February 25, 2010 - edited: February 25, 2010

In the Listing Database that I am currently working on I have several different product types under one section. I do this because to create a new section for each category of product would cause too much repetition of details that are common, such as product manufacturer, website etc. It also saves time if I put everything in its own section.

Now what I want to do is output a separate listings page for each of these different product types, complete with a different style because some products have different fields of data that need to be displayed. I can create the styles but then what I find is that the system will naturally try to output the common details (manufacturer, website etc.) even though they may not have products listed in my specific category.

One way I have to get around this is by using an SEO search URL alongside a custom listing page with its own template, for example:

http://www.website.com/database/list1.php/category-dogs

or

http://www.website.com/database/list2.php/category-cats

However what I would really like to know is if it's possible to define the category (field) sorting inside the file itself, so I could just call list1.php or list2.php without the /category-??? bit in my URL. I was thinking it might be possible to alter the records listing output via this line? Anybody know?

<?php foreach ($pet_listRecords as $record): ?>

Re: [mark99] Sorting by Category Field in Different Template Files

By Dave - February 25, 2010

Hi mark99,

>I have several different product types under one section. I do
>this because to create a new section for each category of
>product would cause too much repetition

This is a great way to set it up and exactly as we would recommend.

I'm not totally clear on what you're trying to do so I'm going to make a few guesses. If none of those are a match please post more details or examples urls is possible.

Here's a mix of simple to more advanced ways to do it:

To have a list page only show records matching a certain category you can specify that in the url:
list.php?category=parts

or specify it in the where options:
'where' = " category = 'parts' ",

or put it in a variable at the top of the page:
$thisCategory = 'parts';
...
'where' = " category = '$thisCategory' ",

or detect it based on the filename
$thisCategory = 'default';
if (strstr($_SERVER['SCRIPT_NAME'], 'parts.php')) { $thisCategory = 'parts'; }


Then, if you want to show a different style of page based on the selected category you can have your list page include an other more designed page:

if ($thisCategory == 'parts') { include "list_parts.php"; exit; }
if ($thisCategory == 'widgets') { include "list_widgets.php"; exit; }

Or you could load different headers based on the category:

if ($thisCategory == 'parts') { include "header_parts.php"; }
if ($thisCategory == 'widgets') { include "header_widgets.php"; }

Or just show different HTML:

if ($thisCategory == 'parts') { print "<h1>Parts</h1>"; }
if ($thisCategory == 'widgets') { print "<h1>Widgets</h1>"; }

If there's certain fields you only want displayed if they are not blank you can test to see if they have a value:

<?php if ($record['manufacturer']): ?>
Manufacturer: <?php echo $record['manufacturer'] ?>
<?php endif ?>

And if you wanted to sort the records differently based on category, you could do that like this:

$orderBy = 'price'; // default sorting
if ($thisCategory == 'parts') { $orderBy = 'size DESC'; }
if ($thisCategory == 'widgets') { $orderBy = 'price'; }
...
'orderBy' => $orderBy,


I hope some of that made sense - and was helpful!

Let me know, if not we'll keep trying! :)
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Sorting by Category Field in Different Template Files

By mark99 - February 26, 2010 - edited: February 26, 2010

I tried this (the where call) but had no luck, it just returned no records even though there are:

<?php

require_once "viewer_functions.php";
$thisCategory = 'dogs with tails';
list($isp_listRecords, $isp_listMetaData) = getRecords(array(
'tableName' => 'isp_list',
'limit' => '5',
'where' => " category = '$thisCategory' ",
));
?>

"category" is a field in my new section and 'dogs with tails' is one of the category options. So I just want my Viewer.php to output reports that have 'dog' as a category and then I can make a customer .php viewer template file for each category to keep it simple. I think the problem is probably with the syntax you gave me for the 'where' line but I'm not sure, PHP is not my stong point :) .

Re: [mark99] Sorting by Category Field in Different Template Files

By Dave - February 26, 2010

Hi mark99,

Is category a multi-value field? Does it let you select multiple categories?

If so, use this line:
'where' => " category LIKE 'dogs with tails' ",

Otherwise, you can use this debug code _after_ the get records code to temporarily see what the category values are and why they are not matching:

showme( $isp_listRecords );
exit;

Let me know what you find out.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Sorting by Category Field in Different Template Files

By mark99 - February 26, 2010

Yes "category" is a multi-value field selected by checkboxes (some products have several categories selected and others just one), my cat/dog stuff is just an example.

I get no output if I use that 'where' call you mention with the debug code, the array is obviously blank. Here's a sample of the returned array without the "where" call.

[1] => Array
(
[num] => 1
[createdDate] => 2009-12-10 14:13:40
[createdByUserNum] => 1
[updatedDate] => 2010-02-25 15:29:46
[updatedByUserNum] => 1
[dragSortOrder] => 1260454420
[title] => Zen Internet
[notes] =>
[website] => http://www.zen.co.uk
[category] => Fixed Line Broadband
[ispa_member] => Yes
[ofcom_adr_scheme] => CISAS
[suppliers] =>
[land_line_broadband_type] => ADSL ADSL2+ Fibre to the Cabinet (FTTC)
[review_id] => 270
[telephone_support] => 08450589000
[land_line_price] => 17.99
[land_line_setup] => 0.00
[land_line_dl_speed] => 20Mbps


Now if I add this..

'where' => " category LIKE 'Fixed Line Broadband' ",

Under this..

'tableName' => 'isp_list',
'limit' => '5',

Then I just get no output.

But I know it works because this works fine:

http://www.site.com/viewer_test.php/category-Fixed_Line_Broadband[/#336699][/url]

Re: [mark99] Sorting by Category Field in Different Template Files

By Dave - February 26, 2010

Ahh, I see. My mistake. Try this:

'where' => " category LIKE '%Fixed Line Broadband%' ",

or

'where' => " category LIKE '%$thisCategory%' ",
Dave Edis - Senior Developer
interactivetools.com

Re: [mark99] Sorting by Category Field in Different Template Files

By Dave - March 1, 2010

Great, glad to hear you got it working.

Let us know if you need anything else. :)
Dave Edis - Senior Developer
interactivetools.com