Displaying dynamic content

6 posts by 2 authors in: Forums > CMS Builder
Last Post: March 21, 2013   (RSS)

By CommonSenseDesign - March 18, 2013 - edited: March 18, 2013

I've set up CMSB so that my client can add a series of promotional insets, such as the four seen near the bottom of this homepage: http://kathiejordandesign.com.previewdns.com/index.php (Window Treatments, etc). These are stored in a table in the database called "inset-panels", which has a field called "title".

Is there a way that I can set up other pages in CMSB so the client can choose one inset panel from a drop down menu, and have their selection appear on other live pages? Here's a hard coded example of how I'd like that to look: http://kathiejordandesign.com.previewdns.com/services.php.

I've set up the CMSB for the Services page so that the user can choose which record in "inset-tables" they want to use (see attached), but how do I get the corresponding panel to actually appear on the page? I.e. if they choose, say, "Antiques" from the drop down menu, the corresponding Antiques panel appears on the page.

Attachments:

cmsb_002.jpg 69K

Hi Nigel,

The easiest way to do it would be to make two separate MySQL calls to get the full data from your inset-panels:

First, I would change your list field so that the value that is stored is the num value for the record (see attached image).  Then your code on the page should look something like this:

  //Use getRecords to retrieve your pages data
  list($pages, $pagesMetaData) = getRecords(array(
    'tableName'   => 'pages',
    'where'       => whereRecordNumberInUrl(0),
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $pages= @$pages[0]; // get first record
  if (!$pages) { dieWith404("Record not found!"); } // show error message if no record found


  $inset = mysql_get('inset-panels', $pages['name_of_list_field']);

This is just example code, so you'll need to make some changes to get it to work with your site.

So the getRecords function is being used to retrieve the general content for the page, and contains the drop down list field that allows the user to select an inset-panel. 

The $pages['name_of_list_field] will contain the relevant num value for the record in your inset-panels section. Then the mysql_get function is used to retrieve the contents of the record from the inset-panels section.

The returned array will be in the same format as a getRecords array, and will contain the details of the inset-panels record. For example, you could display the title of the inset panel like this:

<?php echo $inset['title']; ?>

The only problem you might have is the mysql_get function doesn't return meta data (eg, images, who created the record, etc), let me know if you need that data and I'll show you the changes you need to make to get it.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com
Attachments:

example_007.png 17K

By CommonSenseDesign - March 19, 2013 - edited: March 19, 2013

Thanks for your advice, Greg.

I've attached one of the sample pages where I would like the inset panel to appear, like a "dynamic" include, as it were. This is how it should look: http://kathiejordandesign.com.previewdns.com/services.php

In CMSB, I've changed the corresponding list field to num, as you suggested. The code I'm using to retrieve the relevant table is as follows:

  list($insets_panelsRecords, $insets_panelsMetaData) = getRecords(array(
    'tableName'   => 'insets-panels',
    'loadUploads' => true,
    'allowSearch' => false,
  ));

You advised changing this to:

  //Use getRecords to retrieve your pages data
  list($pages, $pagesMetaData) = getRecords(array(
    'tableName'   => 'pages',
    'where'       => whereRecordNumberInUrl(0),
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $pages= @$pages[0]; // get first record
  if (!$pages) { dieWith404("Record not found!"); } // show error message if no record found


  $inset = mysql_get('inset-panels', $pages['name_of_list_field']);

Sorry, but I'm not sure which values I should be changing in your version, and what should remain untouched?

Also, should I be placing

<?php echo $inset['title']; ?>

where I want the panel to appear on the page?

Thanks again.

Attachments:

services_004.php 7K

Hi Nigel, 

Sorry, I didn't make that very clear. You don't need to add the $pages getRecords functions to your code, that was just an example of how the code could be implemented into a site. 

If your trying to implement this into the services.php page, then your the code that at the top of the page should probably look like this:

 <?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 = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('/home/content/63/10722363/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 'homepage'
  list($homepageRecords, $homepageMetaData) = getRecords(array(
    'tableName'   => 'homepage',
    'where'       => '', // load first record
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $homepageRecord = @$homepageRecords[0]; // get first record
  if (!$homepageRecord) { dieWith404("Record not found!"); } // show error message if no record found

  list($footer_contentRecords, $footer_contentMetaData) = getRecords(array(
    'tableName'   => 'footer-content',
    'where'       => '', // load first record
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $footer_contentRecord = @$footer_contentRecords[0]; // get first record
  if (!$footer_contentRecord) { dieWith404("Record not found!"); } // show error message if no record found


  list($servicesRecords, $servicesMetaData) = getRecords(array(
    'tableName'   => 'services',
    'where'       => '', // load first record
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $servicesRecord = @$servicesRecords[0]; // get first record
  if (!$servicesRecord) { dieWith404("Record not found!"); } // show error message if no record found


  // load record from 'insets-panels' uses the selected panel from the services section.
  list($insets_panelsRecords, $insets_panelsMetaData) = getRecords(array(
    'tableName'   => 'insets-panels',
    'where'       => 'num = '.$servicesRecord['name_of_list_field'],
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $insets_panel = @$insets_panelsRecords[0]; // get first record
  if (!$insets_panel) { dieWith404("Record not found!"); } // show error message if no record found

?>

You will need to change the name_of_list_field variable to the name of the field that contains the list that stores the inset-panels section num variable.

I think the html to display the inset panel record should look like this:

<link href="styles.css" rel="stylesheet" type="text/css" />
        <table width="100" border="0" cellpadding="0" cellspacing="0">
          <tr>
          
  <?php foreach ($insets_panel['image'] as $index => $upload): ?>
<td width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" valign="baseline" background="<?php echo $upload['urlPath'] ?>" id="tdHomeInset245">
<a href="<?php echo htmlencode($insets_panel['link']) ?>">
<img src="images/single-pixel.gif" width="245" height="198" border="0">
</a>
</td>
<?php endforeach ?> 
            </tr>
          <tr>
            <td><img src="images/single-pixel.gif" width="253" height="10" /></td>
          </tr>
          <tr>
            <td><h2><a href="<?php echo htmlencode($insets_panel['link']) ?>"><?php echo htmlencode($insets_panel['title']) ?></a></h2>
<?php echo $insets_panelsRecord['content']; ?>
              <p> <strong><a href="<?php echo htmlencode($insets_panel['link']) ?>">More »</a></strong></p></td>
            </tr>
      </table>

I copied this from insetDetail.php page and changed the name of the variables to use the $insets_panel instead. You can use whatever html layout you want to use. So just copy these variables where you want the inset panel to appear.

Hopefully I'm a bit clearer this time, let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com