Using category num in URL

14 posts by 4 authors in: Forums > CMS Builder
Last Post: September 15, 2009   (RSS)

I am calling this page productList?cat=1 to show all products within a particular category. I have two questions:
1) The fieldname is actually called "category". Can I use "cat" instead of "category" in the URL? How do I relate "cat" to "category" so the page only shows the category =1 products?
2) Category =1 is actually "Tents". How can I convert the num of "1" into "Tents"?

Re: [IronVictory] Using category num in URL

By Dave - June 19, 2008

Hi IronVictory,

1) The simplest way would be to rename your field from 'category' to 'cat'. Another way would be to add this line just below require_once ...

require_once "../lib/viewer_functions.php";

$FORM['category'] = @$FORM['cat'];


What that does is copy the 'cat' value to 'category'. The extra @ hides an error that would be displayed if 'cat' wasn't defined.

2) If you store categories by name instead of num, you can actually just search for ?category=Tents. The only downfall to that is if you rename the "Tents" category then all the records will not be linked up to it anymore. If that's not a problem then storing the value by name is the easiest way.

The next (more complicated) way is to use a second viewer to get all the record numbers from your category section that match "tents" and then use a 'where' to only return products in one of those category nums.

Hope that helps, let me know if you have more questions about any of that.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Using category num in URL

Hi Dave,
Sorry, could you show me how the second viewer would look?
I am confused (which doesn't take much) on how to use the variable (category) in the 'where'

Re: [IronVictory] Using category num in URL

By Dave - June 19, 2008

I can, is it possible to store the category names instead of nums though? Or do you not want to use that method?
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Using category num in URL

I prefer to use nums, so if "Tents" ever changes to "Family Tents", etc. it won't break.

Re: [IronVictory] Using category num in URL

By Dave - June 19, 2008

Ok, here's how you get the category number that matches "Tents". Just have the category fieldname and value in the url. eg: viewer.php?name=Tents If you don't want it to say name you can use the trick above.

// get category num (put this below require_once)
list($categoryRecords) = getRecords(array(
'tableName' => 'category',
'allowSearch' => true,
'requireSearchMatch' => true,
));
$categoryNum = @$categoryRecords[0]['num'];


Next, just print out the categoryNum while testing to make sure you're getting the right value:

// print categoryNum to make sure we have the right one
print "<h1>categoryNum: $categoryNum</h1>";


Finally, add a where to your viewer (this one is for news). Also, set allowSearch = false so you the products will ignore the search keywords and only show products in the selected category num.

//
list($newsRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'allowSearch' => false,
'where' => "category = '$categoryNum'",
));


Hope that helps, let me know if you have any problems with any of that.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Using category num in URL

By petejdg - August 31, 2009

I thought I was trying to do this same thing but not getting it to work. I set up a category table with num and title fields. Put in my categories and then created a products table. Made a list field to pull in my db categories. Things work okay, but I want to print out the category name at the top of the page and also, I plan on directly linking to my specific product listing per category. This works okay: http://www.jdg-siouxlandscale.com/siouxland_scale_products/index.php?category=5 but how can I use the name instead of the number? this is probably simple but after working on it for a while I am just more confused now.

Re: [petejdg] Using category num in URL

By Chris - September 1, 2009

Hi petejdg,

Can you post the PHP source code for that page please?

P.S. That URL is password-protected.
All the best,
Chris

Re: [petejdg] Using category num in URL

By Chris - September 1, 2009

Hi petejdg,


<div id="headerleft"><!-- InstanceBeginEditable name="Headerleft" --><?php echo @$_GET['category']; ?><!-- InstanceEndEditable --></div>


If you want to get the "name" of a category record and you have the "num", you'll have to look up the record in your database.

Add the following code to your STEP 1 section (adjusting the table name if necessary):

$catNum = (int) @$_GET['category'];
list($categoryRecords, $categoryMetaData) = getRecords(array(
'tableName' => 'category',
'where' => "num = $catNum",
'limit' => '1',
));
$categoryRecord = @$categoryRecords[0]; // get first record


Now you can get the category's name anywhere in your page with the following code:

<?php echo $categoryRecord['title']; ?>

I hope this helps! Please let us know if you have any questions or comments.
All the best,
Chris