Product list by tick box

3 posts by 2 authors in: Forums > CMS Builder
Last Post: February 1, 2014   (RSS)

By crystal - January 30, 2014

HI, can you help?

from a list of products i am trying to only list the products that have a tick against a certain field

ie i have a checkboxes (multi value) list

with bakery, chocolate, supermarket  ect...

the code below works if i only have a single item list

can i make it work for a multi item list

Code:-

  <?php foreach ($testRecords as $record): ?>
  <?php if ($record['retail'] != "supermarket") { continue; } ?>
 
      Record Number: <?php echo htmlencode($record['num']) ?><br/>
      Title: <?php echo htmlencode($record['title']) ?><br/>
      Content: <?php echo $record['content']; ?><br/>
      
     

      _link : <a href="<?php echo $record['_link'] ?>"><?php echo $record['_link'] ?></a><br/>
      <hr/>
    <?php endforeach ?>

    <?php if (!$testRecords): ?>
      No records were found!<br/><br/>
    <?php endif ?>

CT

Hi Crystal, 

When you have a single list item, CMS Builder stores the value without any formatting, for example "1" or "supermarket". But if you have a multi checkbox list, it stores the values in a tab separated list for example "    supermarket     " or "    supermarket     house    ". So instead of using equal to, you should search for the value instead:

  <?php foreach ($testRecords as $record): ?>
  <?php if (strpos($record['retail'],"\tsupermarket\t") === false){ continue; ?>
 
      Record Number: <?php echo htmlencode($record['num']) ?><br/>
      Title: <?php echo htmlencode($record['title']) ?><br/>
      Content: <?php echo $record['content']; ?><br/>
      

So the strpos (http://ca2.php.net/strpos) function is used to search a string for the contents of another string. In this case we're searching the $record['retail'] field to see if it contains supermarket, if it doesn't then the item is skipped.

The \t values ensure that supermarket starts and ends with tab spaces, to ensure we only return the value supemarket and not supermarketer, or another value that contains supermarket.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com