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 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