Listing Output from Two 'Category' Fields

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

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.

By gregThomas - June 6, 2016

Hi Mark, 

Could you do something like 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']));
$text .= ", ". join(', ', getListLabels('isp_list', 'category2', $record['category2']));
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 -->

So the new code above appends the options from the options2 field to the end of the text field, then the code you've currently got will replace the options where required.

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com