simple variable issue (hopefully)

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

By willydoit - April 25, 2018

Hi all,

I am creating a page to display different models and sub models of vehicles most of which works but I have run into a slight issue in that I can display a listing page of sub models but I want to use one of the variables ie "model" as a part of the page title  ie

listing of the variants of the  <?php echo htmlencode($record['model']) ?>

I  have a foreach model routine to create the list.  However because my use of the variable in the title comes before my foreach statement to produce the list I get an unknown variable error. My question is, is there a simple way to take the "model" value just from the first record and use it before the main foreach statement used to produce the listing.

My assumption is that I would have to create a separate for each loop prior to the main listings which would

a) create a counter, set at 0

b) take the "model" value from just the first record, and display it in the page title <?php echo htmlencode($record['model']) ?>

c) add to the counter to equal 1

d) create a routine to exit the for each loop if the counter = 1

e) proceed to the next foreach loop for the listings section with <?php foreach ($fiat_modelsRecords as $record): ?>

Unfortunately I am not a programmer so am not sure whether

a) my idea would work

b) whether using the first record for the title would in any way affect its ability to be contained in the main listing and

c) if it can be done how best to implement it.

Any help/advice would be gratefully appreciated.

Thanks in advance.

By willydoit - April 26, 2018

Hi Leo,

thanks for your help from what I understand it would seem that by using

 <?php echo htmlencode($records[0]['model'])?> 

it should display the value in the model variable from the first record.  I have tried this but unfortunately I get the same error as before of undefined variable. i also get the same error if I place 

<?php echo htmlencode($record['model']) ?>

in the meta tags which makes me think that the problem is due to it being a list page not a detail page and therefor at this stage the variable either has no value or multiple values.. I know that what I want to do will work on a detail page. As the value of this variable will be the same in all the selected records it doesnt matter which record produces the value, i just thought the first record would be the most obvious one to use.

In case it helps I have copied my head element below as I think I need to edit something in that section with it being a listing page but that is purely guesswork on my part based on no knowledge whatsoever.

<?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/1a/b/b2c00d7c80/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 records from 'fiat_models'
list($fiat_modelsRecords, $fiat_modelsMetaData) = getRecords(array(
'tableName' => 'fiat_models',
'where' => ' model_overview = "0" ', 
'loadUploads' => true,
'allowSearch' => true,

));

?>

By leo - April 26, 2018

Hi,

If that is your php code on the list page, $fiat_modelsRecords will be the variable you want to use. So the first record will be $fiat_modelsRecords[0].

Leo - PHP Programmer (in training)
interactivetools.com

By willydoit - April 26, 2018

Huge thanks Leo, that sorted it.