Change CSS display with PHP variable

0

I'm wanting that when total $ arrive and go from 40 to div and li class="ta" go 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>
    
asked by anonymous 24.02.2016 / 19:26

2 answers

1

I do not understand your empty loop well 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, so do: / p>

$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:

style.css:

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

.hide {
    display: none;
 }

And the 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>
    
24.02.2016 / 20:04
1

A possible solution follows:

<!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>
    
24.02.2016 / 20:01