Dynamic content combining 2 sections

5 posts by 2 authors in: Forums > CMS Builder
Last Post: February 26, 2020   (RSS)

Help please…

I have 2 sections

  • Products – which lists all of the products – called products.php   ($productsRecord)
  • Categories – basically, some brief intro to each category, and a series of generic header images for each category.

Within products, each product is assigned to a category (a drop down from a list linked to the categories), and a respective category image is assigned as the header (limit 1, random shuffle)

Where I am stuck is assigning the category “on the fly” rather than having to create a different product page for each category.  I am currently using this:

  // load record from 'categories'
  list($categoriesRecords, $categoriesMetaData) = getRecords(array(
    'tableName'   => 'categories',
    'where' => ' title="Janitorial" ',
    'orderBy'     => 'RAND()',
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $categoriesRecord = @$categoriesRecords[0]; // get first record
  if (!$categoriesRecord) { dieWith404("Record not found!"); } // show error message if no record found
  
  

But is there a way to generate this dynamically?

Where I am trying to insert this is somewhere in the below section if possible, rather than having to rely on ' 'where' => ' title="Janitorial" ', - is this possible? 

                  <!--*-*-*-*-*-*-*-*-*-*- BOOTSTRAP CAROUSEL *-*-*-*-*-*-*-*-*-*-->
         
         <div id="bg-img"><?php shuffle($categoriesRecord['banner_image_upload']) ?><?php foreach ($categoriesRecord['banner_image_upload'] as $index => $upload): ?> <?php if($index >= 1) { continue; } // limit uploads shown ?>
         
            <div class="parallax-bg pad-t-150 pad-b-150 pad-section-50 overlay-dark" style="background-image:url(<?php echo htmlencode($upload['urlPath']) ?>)" 
               data-center="background-position: 50% 0px;" 
               data-top-bottom="background-position: 50% -100px;" 
               data-anchor-target="#bg-img">
               <div class="parallax-container">
                  <div class="parallax-content" 
                     data-206-top="opacity: 1"
                     data-0-top="opacity: 0"
                     data-anchor-target="#bg-img h1">
                     <h1 class="text-uppercase txt-dark"><?php echo htmlencode($productsRecord['title']) ?></h1>
                     <p class="txt-dark"><?php echo htmlencode($productsRecord['subhead']) ?> |  <?php echo $productsRecord['category'] ?></p>
                  </div>
               </div>
            </div>        <?php endforeach ?>

         </div>
         
         <!--*-*-*-*-*-*-*-*-*-*- END BOOTSTRAP CAROUSEL *-*-*-*-*-*-*-*-*-*-->          

The category title (eg. Janitorial) works as a drop down, but if I add a link for banner_image_download from the gallery section I get a fatal error?

Thanks in advance

Cheers

Carole

By robin - February 24, 2020

HI Carole,

You can use the product record to help get the right category automatically.  In the example below i used your product category variable ($productsRecord['category']) to search for the category.

 // load record from 'categories'
  list($categoriesRecords, $categoriesMetaData) = getRecords(array(
    'tableName'   => 'categories',
    'where' => " title='" . mysql_escape($productsRecord['category']) . "' ",
    'orderBy'     => 'RAND()',
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $categoriesRecord = @$categoriesRecords[0]; // get first record
  if (!$categoriesRecord) { dieWith404("Record not found!"); } // show error message if no record found

Hope that helps!  Please let me know any questions.  Thanks,

Robin

Robin
Programmer
interactivetools.com

Thanks, but that doesn't work.  It comes back to say "Record not found!"

By robin - February 25, 2020

Hi Carole,

hmmm so something isn't matching up here.  Is $productsRecord['category'] the variable that contains the category name?  If so - in the record that shows the category as not found, what's the value of $productsRecord['category']?

Thanks!
Robin

Robin
Programmer
interactivetools.com