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.
Thank you.
<?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.";
}
?>
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 />";
You are going to need an if statement and build up the echo statement as you go along. So something like this would work:
$out = "";
if(!empty($start_date)){
$out .= "Start Date: $start_date <br />";
}
echo $out;
There are better ways to do this, but this is simple and easy to understand.
Edit, for everything:
$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);