Search code examples
cakephpechorecord

CakePHP How to echo just one record


I want to echo a full name from my MySQL database in my header. When that name is clicked in a list it filters all the records and displays all the records related to that name only. I managed to get the filter working, but not able to display the name in header.

<? $this->read('$jobs as $row'); ?>
 <h1><?=$row['Employee']['first_name']?> <?=$row['Employee']['last_name']?>'s Jobs</h1>
<? $this->end(); ?>

Solution

  • If I'm not wrong, you are trying to retrieve this array, I'm assuing $jobs contains single row.

    try this

    <?php 
    if (isset($jobs)) {
    foreach($jobs as $row){ 
    
    if (isset($row['Employee']['last_name']))
         $last = $row['Employee']['last_name'];
    
    
    $first = 'N/A';
    if (isset($row['Employee']['first_name']))
        $first = $row['Employee']['first_name'];
    ?>
    
     <h1><?php echo $first.' '. $last?>'s Jobs</h1>    
    <?php } }?>
    

    OR

      <h1><?php isset($jobs[0]['Employee']['first_name']) ? $jobs[0]['Employee']['first_name'] : 'N/A' .' '. isset($jobs[0]['Employee']['last_name']) ? $jobs[0]['Employee']['last_name'] : 'N/A'?>'s Jobs</h1>