How can I output php through CMSB

5 posts by 3 authors in: Forums > CMS Builder
Last Post: September 7, 2014   (RSS)

By JeffC - August 28, 2014

The code below is from an archived thread from Jason to use a drop down list to output code through CMSB. Essentially I would like to use this code to allow the user of CMSB to choose a template.

My drop down field is called style, my multi-record style section is called styles, and my path text field is called path.

I receive the following error: 'Notice: Undefined variable: record in [filename]'

<?php if($record['style']): ?>

<?php
//get the selected style record 
$query = "SELECT * FROM `{$TABLE_PREFIX}styles` WHERE num = '".intval($record['num'])."'";
$styleRecord = mysql_query_fetch_row_assoc($query);

//include style path

include($styleRecord['path']);
?>

<?php endif ?>

Any help would be appreciated!

Jeff

By Dave - August 29, 2014

Hi Jeff, 

Here's some simplified code: 

<?php
  if (@$record['style']) { 
    $styleRecord = mysql_get('styles', $record['num']); 
    if (@$styleRecord['path']) { include($styleRecord['path']); }
  }
?>

If you're getting an error that says "undefined variable 'record' " it means that $record is undefined, either when you're checking $record['style'] or $record['num'].  So PHP is assuming $record is an array and it's looking for the 'style' key of the array, but $record isn't defined, so it's giving you an error to let you know that.

I've added @ to suppress error message and added some code to check each variable has a value before we use it, but you might want to just add some debug code above to check if $record is set, such as <?php showme($record); ?>.  And if that gives an error, look higher up in the file to try and determine where and when $record is suppose to be getting set.

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

By JeffC - August 30, 2014

Thanks Dave. Please could you give me some further help. I have altered the code to the below, which has solved the "undefined variable 'record'" error. However I have now another problem. The code selects only the first record from the 'style' list despite which record is selected.

I am using the Instant Website plugin. The table name for each page is: cms_iw_pages however elsewhere in the code it is referred to as $page – so I am assuming the same should work here?

Within the iw_pages table there is a field called 'style'. This list draws from a related record called 'styles'. Within the 'styles' record there are three fields, num, title, and path. I was hoping that the user chooses a title from a drop down list and the mysql picks the relevant num of the record and outputs the .php file in the path associated with it.

<?php
  if ($page['style']) { 
    $stylesRecord = mysql_get('styles', $stylesRecord['num']); 
    if ($stylesRecord['path']) { include($stylesRecord['path']); }
  }
?>

Any help greatly appreciated.

Thanks

Jeff

By gregThomas - September 5, 2014

Hi Jeff,

I think I can see the problem with the code in the previous post:

<?php  if ($page['style']) {
     $stylesRecord = mysql_get('styles', $page['style']);
     if ($stylesRecord['path']) { include($stylesRecord['path']); }
  }
?>

This should work if the drop down file you have for the style is using the num as the value (see attached image for an example of how the field might be set out). 

So I think the issue with the code in the previous post is that you're mysql_get function isn't getting the style record that is linked to page record, but is sending an empty value (I assume  $stylesRecord['num'] isn't set at that point). As no num value is sent, it's pulling the first record from the section instead.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com
Attachments:

CMS Builder.png 79K