1

I am trying to hide an element from a drop down menu if the query returns false. I have searched all day and not been able to find anything to work. I do not want to "echo" from the db query. The connection and query are successful as when I test it using echo it spits out everything as it is in the database. There are no errors. Just does not work. While typing this question it says similar question asked, however, when I went to all of the suggested via link provided... they are only similar with a few key words not the appropriate solution or the problem I am looking for. This is not a css question.

    $query = "SELECT mcategory,pcategory,ecategory FROM ondash_idaccount WHERE ID = '".$_SESSION['ondashsession']."'";
    $result = mysqli_query($dbcon, $query);
    $mcategory = $row['mcategory'];
    $pcategory = $row['pcategory'];
    $ecategory = $row['ecategory'];

if( ! $result ) {
  echo mysql_error($result);
    exit;
}
else{
    //removed css on edit of my question I need to know what to put here or in my html with php?
    }
$result->free();

And for the html drop down menu:

<tr>Select One: <align="center"><select id="platform" name="platform" required><option value="">Select</option>
<label id="mcategory" style=<?php echo $mstyle?>><option value="category_m">M</option></label>
<label id="pcategory"style=<?php echo $pstyle?>><option value="category_p">P</option></label>
<label id="ecategory"style=<?php echo $estyle?>><option value="category_e">E</option></label>
</select>

Any help would be appreciated!

kristina
  • 182
  • 9
  • 1
    Possible duplicate of [How to hide a –  Nov 21 '18 at 23:20
  • @IdontDownVote... I do not believe this is a duplicate. I have edited to my question to reflect better what my issue is. The best answer is marked, below. Thank you for providing the link as another possible resource. I appreciate your assistance. – kristina Nov 23 '18 at 20:12

2 Answers2

2

Options can't be hidden with CSS. Your best bet would be to remove them from the DOM entirely. Instead of giving them visibility: hidden, just don't output them at all.

<?php if ($mcategory !== 0) { ?><option value="category_m">M</option><?php } ?>
<?php if ($pcategory !== 0) { ?><option value="category_p">P</option><?php } ?>
<?php if ($ecategory !== 0) { ?><option value="category_e">E</option><?php } ?>
Charles Stover
  • 1,132
  • 6
  • 13
1

You can try prevent the option to be printed with php instead of trying to hide the option with css.

another example:

In the php:

if( ! $result ) {
  echo mysql_error($result);
    exit;
}
else{
    if($mcategory == 0){
        $mshow = false;
    }
    if($pcategory == 0){
        $pshow = false;
    }
    if($ecategory == 0){
        $eshow = false;
    }

And in the markup:

    <select id="platform" name="platform" required><option value="">Select</option>
        <?php if($mshow):?>
            <label id="matchcategory">
                <option value="category_m">M</option>
            </label>
        <?php endif;?>

       <?php if($pshow):?>
           <label id="pcategory">
               <option value="category_p">P</option>
           </label>
       <?php endif;?>

       <?php if($eshow):?>
           <label id="ecategory">
               <option value="category_e">E</option>
           </label>
       <?php endif;?>
    </select>
lerichard_v
  • 401
  • 3
  • 8
  • I tried the code. Now the drop down only shows "select" none of the categories. Can you explain ($mshow): please. Should something follow it before the ending php ?> Thank you for your assistance! – kristina Nov 22 '18 at 00:17
  • So far this has gotten me closest. Upon further investigation it appears that the boolean value is always 0. if I say 0 is true then the element appears. I need to figure out where the query is going wrong? It was working fine and echoing the results accurately. – kristina Nov 22 '18 at 01:37
  • if the query returns 0 when the option should be shown, just change the $mshow, $pshow and $eshow to true if the category is 0. Let us know if you solve the problem. – lerichard_v Nov 22 '18 at 16:26
  • UPDATE: I fixed my query and now all operates exactly as it should. @degreerichi... Thank you! Your answer was the cleanest and functions perfectly! – kristina Nov 23 '18 at 20:06