字段仅在值时回显

时间:2016-05-30 17:17:36

标签: php mysql mysqli

为了避免在屏幕上打开行,我怎样才能达到5个vip字段,只有在有值时才会回显?

谢谢,Benny

$db = new mysqli('host', 'user', 'pass', 'dbase');
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$query = "
    SELECT
        user.FName, 
        user.LName, 
        user.HerbalifeID, 
        user.UplineS,
        registratie.PartnerFName as pfn, 
        registratie.PartnerLName as pln, 
        registratie.NaamVIP1 as vip1, 
        registratie.NaamVIP2 as vip2, 
        rgistratie.NaamVIP3 as vip3, 
        registratie.NaamVIP4 as vip4, 
        registratie.NaamVIP5 as vip5 
    FROM
        registratie 

    INNER JOIN user 
        ON registratie.userID = user.UserID 
        AND registratie.eventID =127;
";
$result = $db->query($query) or die($db->error.__LINE__);

if ($result->num_rows) {
    while ($row = $result->fetch_object()) {
        echo "{$row->FName} {$row->LName} {$row->HerbalifeID} {$row->pfn} {$row->pln}<br>
        {$row->vip1}{$row->vip2}{$row->vip3}{$row->vip4}{$row->vip5})<br><br>"; //only
    }
} else {
    echo 'No Results';
}

2 个答案:

答案 0 :(得分:1)

if(!empty({$row->vip1}) and !empty({$row->vip2}) and !empty({$row->vip3}) and !empty({$row->vip4}) and !empty({$row->vip5}) ){
    echo "{$row->FName} {$row->LName} {$row->HerbalifeID} {$row->pfn} {$row->pln}<br>{$row->vip1}{$row->vip2}{$row->vip3}{$row->vip4}{$row->vip5})<br><br>";
}else{
    echo "{$row->FName} {$row->LName} {$row->HerbalifeID} {$row->pfn} {$row->pln})<br><br>";

}

答案 1 :(得分:0)

我不知道我是否得到它。 如果只想在填充所有vips时进行打印,请使用以下代码:

//...
$range = range(1,5);
while ($row = $result->fetch_object()) {

    //This will be printed in every iteration
    echo "{$row->FName} {$row->LName} {$row->HerbalifeID} {$row->pfn} {$row->pln}";

    //if one of the vips has no value, go to the next $row
    foreach($range as $r){
        $vipColumn = "vip$r";
        if(empty($row->$vipColumn)){
            continue 2; //breaks both 'foreach' and 'while' loops 
        }
    }

    //This only echo when all vips has values
    echo "<br>{$row->vip1}{$row->vip2}{$row->vip3}{$row->vip4}{$row->vip5})<br><br>";

}
//...