Change CSS display with PHP variable

Asked

Viewed 1,412 times

0

I’m hoping that when $total comes in and goes from 40 to div="te" and the li class="ta" stay with display:block.

CSS:

.foo {
    width: 50px;
    height: 50px;
    background-color: red;
    float: left;
    display:block;
}

.hide {
    display: none;
 }

HTML

   <!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Meu Contator</title>
    <link href="css/estilo.css" rel="stylesheet">

    </head>
    <body>

    <?php
        $servidor = 'localhost';
        $banco      = 'test';
        $usuario  = 'root';
        $senha    = '';
        $link     = @mysql_connect($servidor, $usuario, $senha);
        $db          = mysql_select_db($banco,$link);
        $idUltimoItem = 0;
        if(!$link)
        {
            echo "erro ao conectar ao banco de dados!";exit();
        }

$sql = "SELECT * FROM tabela";
$query = mysql_query($sql);
$total = mysql_num_rows($query);
while($sql = mysql_fetch_array($query)){
}
echo "$total";
?> <br />

   <div id="te" class="<?php echo $total >=40 ? 'foo' : 'hide'; ?>"></div>

 <ul>
   <li class="<?php echo $total >=3 ? 'foo' : 'hide'; ?>;"> <a href="#"><p>INGRESSO</p></a> </li>
 </ul>
    </body>
</html>
  • Wouldn’t it be better to leave everything in . css ? And use only the attribute class=?

  • yes yes it goes in css the styles and Talz... but I want to know how I do the function

  • It’s just that I found the use of style="..." I thought that’s what you wanted I’ll try to work out an answer.

2 answers

1

Follows possible solution:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Meu Contator</title>
    <link href="css/estilo.css" rel="stylesheet">

    </head>
    <body>

    <?php
        $servidor = 'localhost';
        $banco      = 'test';
        $usuario  = 'root';
        $senha    = '';
        $link     = @mysql_connect($servidor, $usuario, $senha);
        $db          = mysql_select_db($banco,$link);
        $idUltimoItem = 0;
        if(!$link)
        {
            echo "erro ao conectar ao banco de dados!";exit();
        }

        $sql = "SELECT * FROM tabela";
        $query = mysql_query($sql);
        $total = mysql_num_rows($query);
        while($sql = mysql_fetch_array($query)){
        }
        echo "$total";

        $limiteExcedido = $total >= 40;
        $display = "display:none";

        if($limiteExcedido){
            $display = "display:block";
        }

    ?> 
<br />
       <div id="te" style="width: 50px;height: 50px;background-color: red;float: left; <?php echo "$display"; ?>"></div>
        <li class="ta" style="<?php echo "$display"; ?>"> <a href="#"><p>INGRESSO</p></a> </li>
    </body>
</html>
  • gave the following error "( ! ) Parse error: syntax error, Unexpected 'var' (T_VAR) in C: wamp www test_contar_registries index.php on line 32" which is this part: var $limiteExcedido = $total >= 40;

  • Take off the var, I confused the languages

  • worked friend thanks :)

1


I didn’t quite understand your empty loop and also your <li> is loose, it should go inside UL, but if I understand the problem is only in the HTML part, so I’ll just talk about it, do so:

$sql = "SELECT * FROM tabela";
$query = mysql_query($sql);
$total = mysql_num_rows($query);
while($sql = mysql_fetch_array($query)){
}
echo "$total";
?> <br />

   <div id="te" style="width: 50px;height: 50px;background-color: red;float: left; display: <?php echo $total >=40 ? 'block' : 'none'; ?>;"></div>

 <ul>
     <li style="display: <?php echo $total >=40 ? 'block' : 'none'; ?>;"> <a href="#"><p>INGRESSO</p></a> </li>
 </ul>

Or you can put everything in css like this:

css style.:

.foo {
    width: 50px;
    height: 50px;
    background-color: red;
    float: left;
    display:block;
}

.hide {
    display: none;
 }

And php:

$sql = "SELECT * FROM tabela";
$query = mysql_query($sql);
$total = mysql_num_rows($query);
while($sql = mysql_fetch_array($query)){
}
echo "$total";
?> <br />

   <div id="te" class="<?php echo $total >=40 ? 'foo' : 'hide'; ?>"></div>

 <ul>
     <li class="<?php echo $total >=40 ? 'foo' : 'hide'; ?>"> <a href="#"><p>INGRESSO</p></a> </li>
 </ul>
  • friend the DIV TE worked but in ul li it is visible even if you are under 40

  • yes... <ul> <li style="display: <?php echo $total >=3 ? 'foo' 'Hide'; ? >;"> <a href="#"><p>TICKET</p></a> </li> </ul>

  • thanks friend :)

  • stranger here still keeps popping :(... I’ll edit up on the question how is my code

  • @kaiquemix forgot the ; now I think it’s okay: <li class="<?php echo $total >=40 ? 'foo' : 'hide'; ?>"> <a href="#"><p>INGRESSO</p></a> </li>

  • 1

    Hurru now was hihi... thank you

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.