info from two tables on one page

5 posts by 2 authors in: Forums > CMS Builder
Last Post: December 14, 2018   (RSS)

Hi all,

I have a small issue in that I have a table with a field called category, the value of which is populated from a drop down list from another table, using the demo code the page looks like the code below and all works as expected except! the value I want to filter against is the field "category" and I cannot get that to work.

I want to filter by test-1.php?category=History but it doesnt work, if I run the page without filters the list is produced correctly and the page shows two values for category which are in the format

Category (value): 5
Category (label): Airport Routes

If I try  test-1.php?category=History I get no results found and if I try  test-1.php?category=2 the record displayed is filtered on the "record number" not the category value

What I need to be able to do is to apply a search filter in the html link that filters against the value held in Category (label)

I suspect it is something to do the fact that the field "category" is holding a value from another table but I don't know what to do to turn it into a value I can filter against. In the field editor for category I have selected "num" for the field option values and for field option labels I have title if the helps

Thanks in advance for any help provided

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
  /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
  
  // load viewer library
  $libraryPath = 'cmsb/lib/viewer_functions.php';
  $dirsToCheck = array('/home/sites/4b/1/128ffc9a32/public_html/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

  // load record from 'information_section_headers'
  list($information_section_headersRecords, $information_section_headersMetaData) = getRecords(array(
    'tableName'   => 'information_section_headers',
    'where'       => whereRecordNumberInUrl(0),
    'loadUploads' => false,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $information_section_headersRecord = @$information_section_headersRecords[0]; // get first record
  if (!$information_section_headersRecord) { dieWith404("Record not found!"); } // show error message if no record found

?>
 </head>


<body>

  

  <!-- STEP2: Display Record (Paste this where you want your record to appear) -->
    <h1>Multiple Data List Headers - Detail Page Viewer</h1>
      Record Number: <?php echo htmlencode($information_section_headersRecord['num']) ?><br/>
      Title: <?php echo htmlencode($information_section_headersRecord['title']) ?><br/>
     
      Content: <?php echo $information_section_headersRecord['content']; ?><br/>
      Category (value): <?php echo $information_section_headersRecord['category'] ?><br/>
      Category (label): <?php echo $information_section_headersRecord['category:label'] ?><br/>
     
</body>
</html>

By daniel - December 12, 2018

Hi willydoit,

Changing the 'where' and 'allowSearch' options in your getRecords() call to something like this may get you on the right track:

  list($information_section_headersRecords, $information_section_headersMetaData) = getRecords(array(
    'tableName'   => 'information_section_headers',
    'where'       => '',
    'loadUploads' => false,
    'allowSearch' => true,
    'limit'       => '1',
  ));

With your current setup, this should allow URLs that use the num (e.g. "test-1.php?category=2") to work. If you would like to filter by the title instead, you'll also need to change the option values field to "title." However, if you do this, note that it could cause unexpected results if two categories ever share the same title, so it may be worthwhile to update the title field's settings to be "Unique" and remove that possibility.

Hope that helps! Let me know if you have any further questions.

Thanks,

Daniel
Technical Lead
interactivetools.com

By daniel - December 13, 2018

Hi Willydoit,

It looks like the code you have is generally correct for what you're trying to do, so the issue might be based on the CMS configuration.

Does Table 1 filter as you expect it to based on the category URLs? (e.g. "information-pages.php?category=Beaches") If so, then I would first double check that the category list field is set up exactly the same for Table 2 as well.

If neither table is filtering properly with the category URLs, make sure that in the list fields' option configuration (Section Editor > click "modify" for the table > click "modify" for the category field > Field Options > List Options), you have "Use this field for option values" set to "title".

Let me know if that helps!

Thanks,

Daniel
Technical Lead
interactivetools.com

Hi Daniel,

Thanks for the tips, issue is now fixed.

Thanks again