0

I'm new to Codeignitor. My senior had previously used the following code to retrieve an array from a database. It retrieves all of the items in the database, but I only want the first one, Please Help:

 <?php $custom_fields = get_custom_fields('projects');
         if(count($custom_fields) > 0){ ?>
         <?php foreach($custom_fields as $field){ ?>
         <?php $value = get_custom_field_value($project->id,$field['id'],'projects');
         if($value == ''){continue;} ?>
         <tr>
            <td class="bold"><?php echo ucfirst($field['name']); ?></td>
            <td><?php echo $value; ?></td>
         </tr>
         <?php } ?>
         <?php } ?>  ```

**I attempted to retrieve the data using index key [1], but I failed miserably.**

``<?php $custom_fields = get_custom_fields('projects');
         if(count($custom_fields) > 0){ ?>
         <?php foreach($custom_fields as $field){ ?>
         <?php $value = get_custom_field_value($project->id,$field['id'],'projects');
         if($value == ''){continue;} ?>
         <tr>
            <td class="bold"><?php echo ucfirst($field[0 &&'name']); ?></td>
            <td><?php echo $value[0]; ?></td>
         </tr>
         <?php } ?>
         <?php } ?> ```

I tried using index key [0] but i got miserably failed. 

1 Answers1

0

Then break out of the foreach loop after 1 item.

<?php 
    $custom_fields = get_custom_fields('projects');
    $count = 0;
    $stop_after = 3;
    if(count($custom_fields) > 0) { 
        foreach($custom_fields as $field){ 
            $value = get_custom_field_value($project->id,$field['id'],'projects');
            if($value == ''){continue;} ?>
            <tr>
                <td class="bold"><?php echo ucfirst($field['name']); ?></td>
                <td><?php echo $value; ?></td>
            </tr>
            <?php 
            $count++;
            if ($count == $stop_after) {
                break;
            }
        } 
    } 
?> 
IT goldman
  • 14,885
  • 2
  • 14
  • 28