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: [chris] Using category num in URL

sorry - forgot I put password on the directory:
-----------------------------------

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php

require_once "/home/jdgsi0/public_html/cmsAdmin/lib/viewer_functions.php";

list($productsRecords, $productsMetaData) = getRecords(array(
'tableName' => 'products',
));

list($productsBigRecords, $productsBigMetaData) = getRecords(array(
'tableName' => 'products',
'orderBy' => 'RAND()',
'limit' => '1',
));

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/main_template.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Untitled Document</title>
<!-- InstanceEndEditable -->
<link href="../_css/main.css" rel="stylesheet" type="text/css" />
<script src="../AC_RunActiveContent.js" type="text/javascript"></script>


</head>

<body>
<div id="header">
<div id="headerWrapper">
<div id="headerleft"><!-- InstanceBeginEditable name="Headerleft" --><?php echo @$_GET['category']; ?><!-- InstanceEndEditable --></div>
<div id="headerright">Service Hotline 1-800-728-8053</div>
</div>
</div>
<div id="contentWrapper">
<div id="contentLeft"><table id="contentLefttable"><tr><td id="contentLefttabletd"><!-- InstanceBeginEditable name="contentLeft" --> <!-- STEP 2a: Display Uploads for field 'lgimage' (Paste this anywhere inside STEP2 to display uploads) -->
<!-- Upload Fields: num, createdTime, tableName, fieldName, recordNum, preSaveTempId, filePath, filename, extension, thumbFilePath, isImage, hasThumbnail, urlPath, width, height, thumbUrlPath, thumbWidth, thumbHeight, info1, info2, info3, info4, info5 -->
<?php foreach ($productsBigRecords as $record): ?>
<?php foreach ($record['lgimage'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt='' /><br/>

<?php elseif ($upload['isImage']): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt='' /><br/>

<?php else: ?>
<a href="<?php echo $upload['urlPath'] ?>">Download <?php echo $upload['filename'] ?></a><br/>

<?php endif ?>
<?php endforeach ?>
<!-- STEP2a: /Display Uploads -->
<p><span class="productBigheader"><?php echo $record['title'] ?></span><br />
<?php echo $record['content'] ?></p>
<?php endforeach ?>
<!-- InstanceEndEditable --></td></tr></table></div>
<div id="contentRight"><table id="contentRighttable"><tr><td id="contentRighttabletd"><!-- InstanceBeginEditable name="contentRight" -->
<!-- STEP2: Display Records (Paste this where you want your records to be listed) -->

<?php foreach ($productsRecords as $record): ?>
<div id="productTable"><?php foreach ($record['thimage'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" alt="" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" border="0" class="productimage" /><br/>

<?php elseif ($upload['isImage']): ?>
<img src="<?php echo $upload['urlPath'] ?>" alt="" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" class="productimage" /><br/>

<?php else: ?>
<a href="<?php echo $upload['urlPath'] ?>">Download <?php echo $upload['filename'] ?></a><?php endif ?>
<?php endforeach ?><h4><a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a></h4></div>
<?php endforeach; ?>
<!-- InstanceEndEditable --></td></tr></table></div>
</div>
<div id="divider"></div>
<div id="navBar"><div id="navBarwrapper"><div id="menu"><!-- QuickMenu Structure [Menu 0] -->
<ul id="qm0" class="qmmc">

<li><a href="../index.php">Home</a></li>
<li><a href="../about_siouxland_scale/index.php">About Us</a></li>
<li><a class="qmparent" href="javascript:void(0);">Services</a>

<ul style="margin:-47px 0px 0px 21px;">
<li><a href="../siouxland_scale_services/certified_services.php">Certified Services/Calibration</a></li>
<li><a href="../siouxland_scale_services/installations.php">Installations</a></li>
<li><a href="../siouxland_scale_services/repairs.php">Repairs</a></li>
</ul></li>

<li><a class="qmparent" href="javascript:void(0);">Products</a>

<ul style="margin:-72px 0px 0px 21px;">
<li><a href="index.php?category=Bench Scales">Balances</a></li>
<li><a class="qmparent" href="javascript:void(0);">Scales</a>

<ul style="margin:-17px 0px 0px 31px;">
<li><a class="qmparent" href="javascript:void(0);">Truck Scales</a>

<ul style="margin:1px 0px 0px 35px;">
<li><a href="index.php?category=Truck Pit Scales">Pit Type</a></li>
<li><a href="index.php?category=Truck Above Ground Scales">Above Ground</a></li>
</ul></li>

<li><a href="index.php?category=">Railroad/Truck Scales</a></li>
<li><a href="floor_scales.php">Floor Scales</a></li>
<li><a href="bench_scales.php">Bench Scales</a></li>
<li><a href="cattle_scales.php">Cattle Scales</a></li>
<li><a href="grain_sample_scales.php">Grain Sample Scales</a></li>
<li><a href="counting_scales.php">Counting Scales</a></li>
<li><a href="remote_displays.php">Remote Displays</a></li>
</ul></li>

<li><a href="instrinsically_safe.php">Intrinsically Safe</a></li>
<li><a class="qmparent" href="javascript:void(0);">Printers</a>

<ul style="margin:-53px 0px 0px 31px;">
<li><a href="label.php">Label</a></li>
<li><a href="ticket.php">Ticket</a></li>
</ul></li>

<li><a href="batch_controllers.php">Batch Controllers</a></li>
<li><a href="indicators.php">Indicators</a></li>
<li><a href="bulk_weighing_systems.php">Bulk Weighing Systems</a></li>
</ul></li>

<li><a href="../siouxland_scale_news/index.php">News</a></li>
<li><a href="../contact_siouxland_scale/index.php">Contact Us</a></li>
<li class="qmclear">&nbsp;</li></ul>

<!-- Create Menu Settings: (Menu ID, Is Vertical, Show Timer, Hide Timer, On Click (options: 'all' * 'all-always-open' * 'main' * 'lev2'), Right to Left, Horizontal Subs, Flush Left, Flush Top) -->
<script type="text/javascript">qm_create(0,true,0,0,'all',false,false,false,false);</script><!--[END-QM0]--></div></div></div>
</body>
<!-- InstanceEnd --></html>