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

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

By JeffC - September 7, 2014

Hi gregThomas

Thanks for helping me fix the problem

Jeff

Jeff