Show Label instead of Value

8 posts by 3 authors in: Forums > CMS Builder
Last Post: November 24, 2017   (RSS)

By JeffC - November 17, 2017

Line 4 of the code below shows the value of job_title. I would like to show job_title:label.

I thought <h3><?php echo htmlencode($job_title['job_title:label']) ?></h3> would work, but doesn't. there's no error, but nothing appears. I am sure I am missing something simple. Any help appreciated!

<?php foreach (range(1,12) as $job_title): ?>
<div class="row equal">
<div class="col-xs-12">
<h3><?php echo htmlencode($job_title) ?></h3>

<?php foreach ($operations_teamRecords as $record): ?>
<?php if ($record['job_title'] != $job_title) { continue; } ?>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3">
<div class="thumbnail">
<?php foreach ($record['photos'] as $index => $upload): ?>
       <?php if ($index >= 1) { continue; } ?>
<img src="<?php echo htmlencode($upload['urlPath']) ?>" width="100%" alt="<?php echo htmlencode($upload['info3']) ?>" />
         <?php endforeach ?>

<div class="panel panel-default">
<div class="panel-body">
<?php // output team member details
$details = ""; 
  if ($record['title'])  { $details .= "<strong>".htmlencode($record['title']).  "</strong><br/>\n"; }
if ($record['job_title'])  { $details .= "".htmlencode($record['job_title:label']).  "<br/>\n"; }
  if ($record['content_wysiwyg'])  { $details .= "".$record['content_wysiwyg'].  "\n"; }
if ($details) {
print "<div class='caption'>$details</div>\n";
}
   ?>
</div>


</div>
</div>
</div>
<?php endforeach ?>

</div>
</div>
<?php endforeach ?>

Jeff

By leo - November 17, 2017

Hi,

The value of $job_title is going to be a number from 1 to 12 given by the foreach loop. It doesn't contain job title label or anything else. If you want to get the full list of job labels and display them you will need to get the records from its table.

Let me know if you have questions!

Leo - PHP Programmer (in training)
interactivetools.com

By JeffC - November 17, 2017

Hi Leo

I understand what you are saying, not sure how to solve it though. If I were to move the line to within the foreach statement it would work. But outside of the foreach I would get a undefined index error. How do I define it without the foreach? Thx

Jeff

By Dave - November 21, 2017

Jeff, 

Also, how are you loading the records?  The getRecords() function usually automatically loads labels for you.  You can see all the variables you have available for display with this code: 

<?php showme($record); ?>

Dave Edis - Senior Developer
interactivetools.com

By JeffC - November 23, 2017

Hi Dave, Hi Lee

Thanks for your assistance so far.

Lee: I'm with you on how to set up the arrays so that 1=> Job1, etc. But I still can't get the value to output.

Dave The getRecords() function isn't working here because I need to show the value outside of the foreach.

Here's what I am trying to achive:

I have a section called operations_teamRecords.
Each record has a  name, job title, bio and photo.
On my output page I would like to display all of the records, grouped under a job title heading. For example, lets say we have the the following names in the Operations_teamRecords and there are two different job titles:

Dave (title), Programmer (job_title)
Jeff (title), Designer (job_title)
Lee (title), Programmer (job_title)

My job_titles are set up as a pull down list, with value and label. i.e.:

1|Designer
2|Progammer

In the front end I would like it to display this as:

Designer
Jeff

Programmer
Dave
Lee

As things stand I have each record grouped under the correct heading. But instead of displaying the label it is displaying the value. i.e.:

1
Jeff

2
Dave
Lee

Here's my code again.

$jobTitleLabels = array(
1 => 'Designer',
2 => 'Programmer');

<?php foreach (range(1,12) as $job_title): ?>
<div class="row equal">
<div class="col-xs-12">

<h3><?php echo htmlencode($job_title) ?></h3> THIS IS THE PART THAT SHOWS THE VALUE. I WOULD LIKE TO DISPLAY THE LABEL

<?php foreach ($operations_teamRecords as $record): ?>
<?php if ($record['job_title'] != $job_title) { continue; } ?>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3">
<div class="thumbnail">
<?php foreach ($record['photos'] as $index => $upload): ?>
       <?php if ($index >= 1) { continue; } ?>
<img src="<?php echo htmlencode($upload['urlPath']) ?>" width="100%" alt="<?php echo htmlencode($upload['info3']) ?>" />
         <?php endforeach ?>

<div class="panel panel-default">
<div class="panel-body">
<?php // output team member details
$details = ""; 
  if ($record['title'])  { $details .= "<strong>".htmlencode($record['title']).  "</strong><br/>\n"; }
if ($record['job_title'])  { $details .= "".htmlencode($record['job_title:label']).  "<br/>\n"; }
  if ($record['content_wysiwyg'])  { $details .= "".$record['content_wysiwyg'].  "\n"; }
if ($details) {
print "<div class='caption'>$details</div>\n";
}
   ?>
</div>


</div>
</div>
</div>
<?php endforeach ?>

</div>
</div>
<?php endforeach ?>

Thanks

Jeff

By leo - November 23, 2017

Hi Jeff,

If you have already set up the list field in the section, you can use this function getListOptions($tablename, $fieldname) to get list options array, and the labels will be the values of the array.

Here are the steps:

// Get list options array
$jobTitleArray = getListOptions('operations_teamRecords', 'job_titles');

// The foreach loop
foreach(range(1,12) as $job_title){
  $label = $jobTitleArray[$job_title]
}

Let me know if you have any questions!

Leo - PHP Programmer (in training)
interactivetools.com

By JeffC - November 24, 2017

Thanks Leo

Got it :)

I made a few alterations to your code. Here's what I did for anyone else using this thread: 

// Get list options array
$jobTitleArray = getListOptions('operations_teamRecord', 'job_titles');

The curly quotes were producing an error for me, so I wrote it in two separate statements:

// The foreach loop
<?php foreach(range(1,12) as $job_title) ?>
<?php $label = $jobTitleArray[$job_title] ?>

Lastly, to display the label:

<h3><?php echo htmlencode($label) ?></h3>

Thanks for your help. I couldn't have got here on my own

Jeff