如果数据库字段为空,则隐藏文本 [英] Hide text if Database field is empty

查看:81
本文介绍了如果数据库字段为空,则隐藏文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过PHPmyadmin创建了一个数据库,并将信息从该数据库中提取到我的wordpress网站中.当前,仍然显示没有数据的行.例如,如果我不输入起始日期,即使没有值,标题开始日期"仍会出现.

I have created a database through PHPmyadmin and am pulling information from that database into my wordpress site. Currently, rows that have no data in them still show up. For example, if I do not enter a start_date, the heading "Start date" would still appear even though it has no value.

如果没有值,我可以知道如何重新编写代码以隐藏标题吗?我希望这适用于所有标题.开始日期,说明等.

May I know how to re-write the code so as to hide the headings if there is no value? I would like this to be applicable to all headings ie. start_date, description etc.

谢谢.

<?php

$mysqli = NEW mysqli('localhost','surviboj_wp156','p365SJ@37)','surviboj_wp156');

require('/home/surviboj/public_html/wp-load.php');
$id = get_the_ID();

$resultSet = $mysqli->query("SELECT * FROM sweepstake_data WHERE item_id = $id");

if($resultSet->num_rows !=0){

    while($rows = $resultSet->fetch_assoc())
    {
        $description = $rows['description'];
        $links = $rows['links'];
        $category = $rows['category'];
        $eligibility = $rows['eligibility'];
        $start_date = $rows['start_date'];
        $end_date = $rows['end_date'];
        $entry_frequency = $rows['entry_frequency'];
        $prizes = $rows['prizes'];
        $victory_prizes = $rows['victory_prizes'];
        $additional_comments = $rows['additional_comments'];

        echo "<p>Name: $description<br /> Link: <a href=$links>Click here</a> <br /> Category: $category<br /> Eligibility: $eligibility<br /> Start date:$start_date<br /> End date: $end_date<br /> Entry frequency: $entry_frequency<br /> Prizes: $prizes<br /> Victory prizes: $victory_prizes<br /> Additional comments: $additional_comments<br />";
    }
}else {
    echo "No results.";
}

?>

推荐答案

只需检查该值是否为空,如果没有则回显它们.您只需要对不需要的字段执行此操作,这些字段可以为空,在您的情况下为$start_date:

Just check if the value is empty or not and echo them if not. You will only need to do this for fields that are not required and can be blank, in your case $start_date:

echo "<p>Name: $description<br /> Link: <a href=$links>Click here</a> <br /> Category: $category<br /> Eligibility: $eligibility<br />";

echo !empty($start_date) ? "Start date:$start_date<br />" : "" ;

echo "End date: $end_date<br /> Entry frequency: $entry_frequency<br /> Prizes: $prizes<br /> Victory prizes: $victory_prizes<br /> Additional comments: $additional_comments<br />";

这篇关于如果数据库字段为空,则隐藏文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆