SarasotaSam Posted July 10, 2023 Share Posted July 10, 2023 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 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted July 10, 2023 Share Posted July 10, 2023 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted July 10, 2023 Share Posted July 10, 2023 If you want to hide them, why include them in the query in the first place? 1 Quote Link to comment Share on other sites More sharing options...
Strider64 Posted July 11, 2023 Share Posted July 11, 2023 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. Quote Link to comment Share on other sites More sharing options...
SarasotaSam Posted July 11, 2023 Author Share Posted July 11, 2023 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 11, 2023 Share Posted July 11, 2023 7 minutes ago, SarasotaSam said: "Option:" does print whether it is empty or not. Not according to that code... if (trim($option_second) == "") { echo ""; } else { echo "<strong>Option:</strong> $option_second"; } Quote Link to comment Share on other sites More sharing options...
ginerjm Posted July 11, 2023 Share Posted July 11, 2023 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.