我已经通过PHPmyadmin创建了一个数据库,并将该数据库中的信息提取到我的wordpress网站。目前,仍然显示没有数据的行。例如,如果我没有输入start_date,标题"开始日期"即使它没有价值,它仍然会出现。
我是否知道如何重新编写代码以便在没有值的情况下隐藏标题?我希望这适用于所有标题,即。 start_date,description等。
谢谢。
<?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.";
}
?>
答案 0 :(得分:1)
您将需要一个if语句并在进行时构建echo语句。所以这样的事情会起作用:
$out = "";
if(!empty($start_date)){
$out .= "Start Date: $start_date <br />";
}
echo $out;
有更好的方法可以做到这一点,但这很简单易懂。
编辑,所有内容:
$out = array();
(!empty($rows['description']) ? $out[] = "Name: ".$rows['description'] : "";
(!empty($rows['links']) ? $out[] = "Link: <a href='".$rows['links']."'>Click here</a> " : "";
(!empty($rows['category']) ? $out[] = "Category: ".$rows['category'] : "";
(!empty($rows['eligibility']) ? $out[] = "Eligibility: ".$rows['eligibility'] : "";
(!empty($rows['start_date']) ? $out[] = "Start date: ".$rows['start_date'] : "";
(!empty($rows['end_date']) ? $out[] = "End date: ".$rows['end_date'] : "";
(!empty($rows['entry_frequency']) ? $out[] = "Entry frequency: ".$rows['entry_frequency'] : "";
(!empty($rows['prizes']) ? $out[] = "Prizes: ".empty($rows['prizes'] : "";
(!empty($rows['victory_prizes']) ? $out[] = "Victory prizes: ".$rows['victory_prizes'] : "";
(!empty($rows['additional_comments']) ? $out[] = "Additional comments: ".$rows['additional_comments'] : "";
echo implode("<br />", $out);
答案 1 :(得分:1)
只检查值是否为空,如果没有,则回显它们。您只需要对不需要的字段执行此操作,并且可以为空;在您的情况下$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 />";