AppleTattooGuy
AppleTattooGuy

Reputation: 1175

Show content if equal to entered value in PHP and MySQL

Ok so basically what I am trying to do is show a div if MySQL row data is equal to an entered value for example if the user type column says admin-full it needs to show some content.

I am quite new to PHP so I really appreciate your help on this my code is as follows.

<?php

$row_user['type'] = $user_type;

if ($user_type == admin_full)
{
    <!-- START DIV DROPDOWN BOX--> 
  <div class="dropbox">
            <a id="myHeader1" href="javascript:showonlyone('newboxes1');" >show this one only</a>
  </div>   
  <div class="newboxes" id="newboxes1">Div #1</div>
  <!-- END DIV DROPDOWN BOX-->
}
  ?> 

Upvotes: 0

Views: 497

Answers (3)

Anthony Hatzopoulos
Anthony Hatzopoulos

Reputation: 10537

<?php

Like this

<?php
    // you probably have this line wrong. 
    //$row_user['type'] = $user_type; 
    // and you want
    $user_type = $row_user['type'];

    // I am assuming admin_full is not a constant, otherwise remove the single quotes 
    if ($user_type === 'admin_full') 
    {
    ?>
        <!-- START DIV DROPDOWN BOX--> 
            <div class="dropbox">
                <a id="myHeader1" href="javascript:showonlyone('newboxes1');" >show this one only</a>
            </div>   
            <div class="newboxes" id="newboxes1">Div #1</div>
        <!-- END DIV DROPDOWN BOX-->
    <?php
    }
    else {
        ?>

        Oops, what the duce are you doing dude!

        <?php
    }
    ?> 

I am assuming admin_full is not a constant and you wanted a string otherwise replace that line with

if ($user_type == admin_full)

Upvotes: 2

Welling
Welling

Reputation: 556

You need to close php tag before use HTML code or echo out thoses HTML code.

Two ways

<?php
$row_user['type'] = $user_type;

if ($user_type == admin_full)
{
?>
<!-- START DIV DROPDOWN BOX--> 
    <div class="dropbox">
    <a id="myHeader1" href="javascript:showonlyone('newboxes1');" >show this one only</a>
    </div>   
    <div class="newboxes" id="newboxes1">Div #1</div>
    <!-- END DIV DROPDOWN BOX-->
<?php
}
?>

OR:

<?php

$row_user['type'] = $user_type;

if ($user_type == admin_full)
{
echo "
    <!-- START DIV DROPDOWN BOX--> 
    <div class=\"dropbox\">
        <a id=\"myHeader1\" href=\"javascript:showonlyone('newboxes1');\" >show this one only</a>
    </div>   
    <div class=\"newboxes\" id=\"newboxes1\">Div #1</div>
    <!-- END DIV DROPDOWN BOX-->";
  }
 ?>

Upvotes: 1

Marc B
Marc B

Reputation: 360602

You can't embed html in PHP as you are there. html is just text as far as PHP is concerned. It is not executable code. Either switch to using an echo/print structure to output the html, or break out of PHP mode, e.g.

echo/print using a HEREDOC:

if ($user_type == admin_full) {
   echo <<<EOL
your html here

EOL;
}

break out of php mode:

if ($user_type == admin_full) { ?>

html goes here

<? }

Upvotes: 0

Related Questions