0

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.";
}

?>

2 Answers 2

1

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 />";
Sign up to request clarification or add additional context in comments.

Comments

1

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);

3 Comments

Hi Tom, thanks for the reply. I tried your code out link and the issue is that if one field is empty, the entire table does not show up. Can you please help me to have a look?
@firefirehelphelp You said that you wanted the heading for "Start date" to be hidden if that column was empty. This answer tells you how, but in your code, you've wrapped all values in the same if. Only wrap the columns you want to hide in the if. And why are you appending everything to different $out-variables instead of the same?
Hi Magnus, apologies if I wasn't clear enough. But hiding the "Start date" was only an example. I wanted to code it such that if any items didn't have values, then I would hide the row. I have amending my original question to better reflect this. Thus, I believe I should be placing everything within the if. I would try changing everything to a single $out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.