Jump to content

Do not echo empty field from Mysql


SarasotaSam

Recommended Posts

I have the following for not printing an empty field in the MYSQL table:

if (trim($option_second) == "") { echo ""; } else { echo "<strong>Option:</strong> $option_second"; }

I have a search page now that displays the records for the MYSQL table, but I do not know how to hide the records that are empty like the above code. Here is what I have:

Option: <?= $items['option_second']; ?>

I would like this to not print if empty like the first code.

Thank you

 

Link to comment
Share on other sites

Why not post the code that is not working and let us see what you are trying?

PS - the better way of doing this is:

echo "Option: " . $items['option_second']; 

Or:

echo "Option: {$items['option_second']}";

That way you can avoid using php tags all over the place

Link to comment
Share on other sites

2 hours ago, Barand said:

If you want to hide them, why include them in the query in the first place?

There are times that it needs to be done, for example if a game has multiple choices and there are only 2 possible answers.

This is a JavaScript pulling data from a database table using fetch ->

        // This line sets the text content of the button to the corresponding answer (ans1, ans2, ans3, ans4)
        // with a "📷" character at the beginning.
        button.textContent = `📷 ${[ans1, ans2, ans3, ans4][index]}` || "";

        // If there's no corresponding answer, the button is disabled (its pointer events are set to "none").
        // Otherwise, the button is enabled (its pointer events are set to "auto").
        if (![ans1, ans2, ans3, ans4][index]) {
            button.style.pointerEvents = "none";
        } else {
            button.style.pointerEvents = "auto";
        }

Though I have of admit most of the time is a waste of time coding for empty fields.

Link to comment
Share on other sites

I need to include the Option because this is an order and many times it is not selected, so I need it not to print.

This is what does not work:

if (trim($option_second) == "") { echo ""; } else { echo "<strong>Option:</strong> $option_second"; }

Though the contents of the field does not print if it is empty. "Option:" does print whether it is empty or not.

I am using (trim($option_second) == "") because this is a result of search in MYSQL.

Thank you.

Link to comment
Share on other sites

I agree with Barand.  The code you gave us only shows something if the value is not empty.

Here is your code setup to do a couple of tests:

echo "Test #1:<br>";
$option_second = '';
show_option($option_second);
echo "Test #2:<br>";
$option_second = 'abc';
show_option($option_second);
//******************
function show_option($option_second)
{
  echo "option second is now '$option_second'<br>";
  if (trim($option_second) == "")
      echo "";
  else
      echo "<strong>Option:</strong> $option_second<br>";
}

When you run it you will see one test result only

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.