数组中的条件

时间:2018-01-16 20:22:46

标签: php html

如果值qtd大于5则显示确定,否则为OPS。 和 如果值qtd大于5,则显示图像1,否则显示图像2.

if ($aluno->qtd > 5) {
    $numeral = "OK"; 
    $image = "<imgsrc="link_da_image111" />"; 
} else {
    $numeral = "OPS";
    $image = "<img src="link_da_image222" />";
}
$table = new html_table();
$table->attributes = array("class" => "table table-hover table-condensed table-striped table-users slide_7");
foreach($alunos as $aluno) {
    $row = new html_table_row();
    $row->cells = array( $aluno->firstname . ' ' . $aluno->lastname, $aluno->user_picture, $aluno->qtd, $aluno->firstname . ' ' . $aluno->lastname, $numeral, $image, );
    $table->data[] = $row;
}
echo html_writer::table($table);

我希望结果是这样的

Joan       --- 34   ---- OK
Marr       --- 2     ---- OPS
Richar    --- 34    ---- OK 
Otvr       --- 1      ---- OPS

1 个答案:

答案 0 :(得分:1)

if语句必须位于foreach循环内,以便它可以引用为$aluno的每个元素设置的$alunos变量。

$table = new html_table();
$table->attributes = array("class" => "table table-hover table-condensed table-striped table-users slide_7");
foreach($alunos as $aluno) {
    if ($aluno->qtd > 5) {
        $numeral = "OK"; 
        $image = "<imgsrc="link_da_image111" />"; 
    } else {
        $numeral = "OPS";
        $image = "<img src="link_da_image222" />";
    }
    $row = new html_table_row();
    $row->cells = array( $aluno->firstname . ' ' . $aluno->lastname, $aluno->user_picture, $aluno->qtd, $aluno->firstname . ' ' . $aluno->lastname, $numeral, $image, );
    $table->data[] = $row;
}
echo html_writer::table($table);