简体   繁体   English

如何显示从数据库 codeigniter 获取的数组中的特定值

[英]how to display specific value from array fetched from database codeigniter

I'm new to Codeignitor.我是 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. 

Then break out of the foreach loop after 1 item.然后在 1 项break跳出 foreach 循环。

<?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;
            }
        } 
    } 
?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM