Question about GetlistLabels

6 posts by 2 authors in: Forums > CMS Builder
Last Post: January 20, 2020   (RSS)

By mbareara - January 2, 2020 - edited: January 2, 2020

UPDATE

i found a solution on this forum...



<?php foreach ($restaurantsRecord['cuisine:labels'] as $tag ) : ?>

<?php echo $tag; ?><?php endforeach; ?>

THis is a good solution, but, what can i do if i would have a link for each tag with cuisine NUM ?

By Steve99 - January 2, 2020

Hello,

It sounds like you have a multi-record table for Restaurants and a multi-record table for Cuisines. Cuisines records being available to choose from as a multi-value list field for a Restaurant entry. Would this be correct?

If yes, the following method should help.

There is an internal cmsb function mysql_escapeCSV() that you can use to process/prepare the Restaurant record selected cuisine values (array) for a MySQL WHERE IN query on your cuisines table. (just change the example table names to your actual table names)

<?php
  // load record from 'restaurants'
  list($restaurantsRecords, $restaurantsMetaData) = getRecords(array(
    'tableName'   => 'restaurants',
    'where'       => whereRecordNumberInUrl(0),
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $restaurantsRecord = @$restaurantsRecords[0]; // get first record

  // load records from 'yourCuisineTable'
  list($yourCuisineTableRecords, $yourCuisineTableMetaData) = getRecords(array(
    'tableName'   => 'yourCuisineTable',
    'where'       => '`num` IN('.mysql_escapeCSV($restaurantsRecord['cuisine:values']).')',
    'loadUploads' => true,
    'allowSearch' => false,
  ));
?>

Then you can loop through the "yourCuisineTable" results (shown in default cmsb code generator fashion):

  <?php foreach ($yourCuisineTableRecords as $record): ?>
      Record Number: <?php echo htmlencode($record['num']) ?><br>
      Title: <?php echo htmlencode($record['title']) ?><br>
      _link : <a href="<?php echo $record['_link'] ?>"><?php echo $record['_link'] ?></a><br>
  <?php endforeach ?>

Here is the developers note of the mysql_escapeCSV() function:

// return comma separated list of escaped values for construction WHERE ... IN('val1','val2','val3') queries
// if array is empty $defaultValue or 0 is returned so the query will always be value (and not ... IN() which is invalid) MySQL
// Usage: $where = "myfield IN (" .mysql_escapeCSV($values). ")";

Hope this helps!

Best,
Steve

By mbareara - January 2, 2020

yesssss!!! great solution!

By mbareara - January 17, 2020

Hi Steve, it was very helpful for detailed page but it doesn.'t work on a list page.

Does it need some change on code?

Thank you in advance

Orazio 

By Steve99 - January 20, 2020

Glad it helped! Yes, there would be some changes needed for use on the list page.

You could loop through the restaurant records and query the cuisine records inside that loop. This could be made more elegant, and this hasn't been tested, but you can do something like this:

<?php
  // load records from 'restaurants'
  list($restaurantsRecords, $restaurantsMetaData) = getRecords(array(
    'tableName'   => 'restaurants',
    'loadUploads' => true,
    'allowSearch' => false,
  ));
?>

<?php foreach ($restaurantsRecords as $record): ?>

  Record Number: <?php echo htmlencode($record['num']) ?><br>
  Title: <?php echo htmlencode($record['title']) ?><br>
  _link : <a href="<?php echo $record['_link'] ?>"><?php echo $record['_link'] ?></a><br>  
  
  <?php
    // load records from 'yourCuisineTable'
    list($yourCuisineTableRecords, $yourCuisineTableMetaData) = getRecords(array(
      'tableName'   => 'yourCuisineTable',
      'where'       => '`num` IN('.mysql_escapeCSV($record['cuisine:values']).')',
      'loadUploads' => true,
      'allowSearch' => false,
    )); 
  ?>
  
  <?php foreach ($yourCuisineTableRecords as $record2): ?>
   Record Number: <?php echo htmlencode($record2['num']) ?><br>
   Title: <?php echo htmlencode($record2['title']) ?><br>
   _link : <a href="<?php echo $record2['_link'] ?>"><?php echo $record2['_link'] ?></a><br>  
  <?php endforeach ?>
  
<?php endforeach ?>

Best,
Steve