如何在数组中查找空值

时间:2016-08-29 07:51:10

标签: php codeigniter

我正在努力创建一个大学的比较我几乎完成它但现在我想找到空值并且放一个罢工而不是空列我知道我可以使用if语句我想要的是有更好的除了使用嵌套if else或多个if

之外的其他方式

控制器具有选择值

的功能
public function test()
    {
        $insId  = "INS20160738";
        $course = "B.Tech Electronics & Communication Engineering";
        $this->load->model('comparisonModel');
            $resFirst   = $this->comparisonModel->selFirst($insId,$course);
            foreach ($resFirst as $key => $value) {
                echo '<h6> name:</h6><p>'.$value['course_name'].'</p><h6>Duration:</h6>'.$value['duration'].'
                     <h6>eligiblity:</h6><p>'.$value['eligibility'].'</p><h6>recognition:</h6><p>'.$value['recognition'].'</p><h6>Affiliation:</h6>'.$value['affiliation'].'
                     <h6>Certification:</h6>'.$value['certificate'].'<h6>Category:</h6>'.$value['category'].'<h6>Type:</h6>'.$value['type'].'<h6>Category:</h6>'.$value['school_batch'].'';
            }
    }

选择的模型是

public function selFirst($insId,$course)
  {
    $stmt='SELECT * FROM `institute-course` WHERE `institute-id`=  '.$this->db->escape($insId).' AND `course_name`='.$this->db->escape($course);
    $res=$this->db->query($stmt);
    return $res->result_array();
  }

我得到这样的结果

name:

B.Tech Electronics & Communication Engineering

Duration:

eligiblity:

plus two

recognition:

Affiliation:

A P J Abdul Kalam Technological University
Certification:

B.Tech in Electronics & Communication Engineering
Category:

under graduate
Type:

college
Category:

我搜索的是有更好的方法来获得这样的结果

name:

B.Tech Electronics & Communication Engineering

Duration: this field is Empty or put a strike 

eligiblity:

plus two

recognition:this field is empty

Affiliation:

A P J Abdul Kalam Technological University
Certification:

B.Tech in Electronics & Communication Engineering
Category:

under graduate
Type:This Is filed Is empty
college
Category: this field is empty

没有使用多个if else语句

提前致谢

2 个答案:

答案 0 :(得分:2)

在foreach循环中尝试此操作(控制器

foreach ($resFirst as $value) 
{
?>
    <h6> name:</h6>
        <p> <?= (empty($value['course_name'])) ? 'this field is empty' : $value['course_name'] ; ?> </p>
    <h6> Duration:</h6>
        <p> <?= (empty($value['duration'])) ? 'this field is empty' : $value['duration'] ;?> </p>
    <h6> eligiblity:</h6>
        <p> <?= (empty($value['eligiblity'])) ? 'this field is empty' : $value['eligiblity'] ;?> </p>
    <h6> recognition:</h6>
        <p> <?= (empty($value['recognition'])) ? 'this field is empty' : $value['recognition'] ;?> </p>
    <h6> Affiliation:</h6>
        <p> <?= (empty($value['affiliation'])) ? 'this field is empty' : $value['affiliation'] ; ?> </p>
    <h6> Certification:</h6>
        <p> <?= (empty($value['certificate'])) ? 'this field is empty' : $value['certificate'] ; ?> </p>
    <h6> Category:</h6>
        <p> <?= (empty($value['category'])) ? 'this field is empty' : $value['category'] ; ?> </p>
    <h6> Type:</h6>
        <p> <?= (empty($value['type'])) ? 'this field is empty' : $value['type'] ; ?> </p>
    <h6> Category:</h6>
        <p> <?= (empty($value['school_batch'])) ? 'this field is empty' : $value['school_batch'] ; ?> </p>
<?php
}

答案 1 :(得分:0)

您可以测试该值是否为空

if (empty($value['type'])) {
    $value['type'] = 'This field is empty';
}

或者,如果数据库中的值为NULL,则可以在select - 子句中指定它:

SELECT *, IFNULL(type, 'This field is empty') ...