Listing Output from Two 'Category' Fields

4 posts by 3 authors in: Forums > CMS Builder
Last Post: June 6, 2016   (RSS)

By mark99 - May 24, 2016 - edited: May 24, 2016

On my website I have a simple category system, which consists of a text field called 'category' and that contains a text list of different categories (e.g. "Cars", "Trucks and Vans" etc.). Now when I want to output which categories apply to a particular product then I usually do this:

  <!-- STEP2: Display Records (Paste this where you want your records to be listed) -->
    <?php foreach ($isp_listRecords as $record): ?>
<li><a name="<?php echo $record['num'] ?>"></a><b style="line-height:22px;color:#716f64;font-size: 14px;"><?php echo $record['title'] ?></b> (<?php echo $record['location_hq'] ?>)<br />-- <?php 
$numx = $record['num'];
$test = $record['title'] = preg_replace("/[ ]/", "-", $record['title']);
$test2 = "-";
$replace = array();
$replace['Cars'] = "<a href=ISP_Detail_Cars.php?"."$test$test2$numx".">"."Cars"."</a>";
$replace['Trucks and Vans'] = "<a href=ISP_Detail_Trucks.php?"."$test$test2$numx".">"."Trucks and Small Vans"."</a>";
$text = join(', ', getListLabels('isp_list', 'category', $record['category']));
foreach ($replace as $k=>$v)
{
   $text = str_replace($k, $v, $text);
}
echo $text;
?></li><br />

    <?php endforeach ?>
</ul>

    <?php if (!$isp_listRecords): ?>
      <br /><br /><b><u>No records were found!</u></b><br/><br/>
    <?php endif ?>
  <!-- /STEP2: Display Records -->

Generally this works fine, but recently my products database has become more complicated and I've needed to create a second 'category2' field to add a particular sub-set of products (e.g. "Modified Trucks"). Now I'd like to output the "Modified Trucks" category using the same code above, so I need to check against both the data / category lists inside fields 'category' and 'catergory2' and I think that requires me to edit this line:

$text = join(', ', getListLabels('isp_list', 'category', $record['category']));

Any ideas how I can tweak this so I'm searching / checking against categories for both the 'category' (already used above) and the new 'category2' field?

By Damon - May 30, 2016

Hi,

I looked at your code but it is hard to follow without seeing the templates and knowing how things are setup in CMS Builder.

Can you post the URL to your template?
Also, are the category lists single select?

Let me know and we can look at possible putting together more simplified code and what is needed for adding a sub-category.

Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By mark99 - May 31, 2016 - edited: May 31, 2016

Ah.. sorry I completely forgot to include a sample of the opening code. Right at the top the relevant PHP page, which essentially just lists all of the categories (from the 'category' field) associated with a single product and then generates links to display related pages for them, we have the following code.

<?php
  /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
  require_once "/*********/system/lib/viewer_functions.php";

  list($isp_listRecords, $isp_listMetaData) = getRecords(array(
    'tableName'   => 'isp_list',
    'where'       => whereRecordNumberInUrl(0),
    'limit'       => '1',
    'loadCreatedBy' => false,
  ));
  $isp_listRecord = @$isp_listRecords[0]; // get first record

  // show error message if no matching record is found
  if (!$isp_listRecord) {
//    header("HTTP/1.0 404 Not Found");
    print "Missing Record";
    exit;
  }
?>

Here's an example of another page where I use a similar approach:

http://www.ispreview.co.uk/isp_list/ISP_Detail_Fixed_Line_Optional_Broadband.php?AAISP-14

The code essentially outputs the list of linked categories below where it says "Other AAISP Service Categories." As I say this works fine if I just use one field called 'category' to include a selectable list of all my categories for that one product, but what I want to do is also check against the list of special sub-categories in my 'category2' field and output links accordingly. So this bit of code from my first post needs to be checking 'category' and also 'category2'.

$text = join(', ', getListLabels('isp_list', 'category', $record['category']));

PS - Before you ask, no.. category and category2 cannot simply be merged into a single field :) , they exist separately to handle a very specific issue with some unusual products in our database but that's a separate thing.