Change color of a DIV with PHP

1

Good, I have a div in Bootstrap in blue and a% code in%. I would like to change the color of PHP automatically to red if the content is less than div .

<div class="col-lg-3 col-md-6">
                <div class='<?php echo ($registros <95)? panel panel-red: panel panel-blue;?>'>
                    <div class="panel-heading">
                        <div class="row">
                            <div class="col-xs-3">
                                <i class="fa fa-percent fa-5x"></i>
                            </div>
                            <div class="col-xs-9 text-right" style="font-size:55px;">
                                 <?php
         $sql = "SELECT ANSMensual1 FROM cauctti.datosglobales where Ambito like 'SSU Intern'";
         $result = mysqli_query($connection,$sql);
         while ($registros = mysqli_fetch_array($result))
        {
        ?>
            <?php echo $registros["ANSMensual1"] ?>
        <?php
        }
        ?>

                            </div>
                        </div>
                    </div>
                    <a href="detallmensual.php">
                        <div class="panel-footer">
                            <span class="pull-left">ANS MENSUAL SSU Intern</span>
                            <div class="clearfix"></div>
                        </div>
                    </a>
                </div>
    </div>      
    
asked by Jordi Carmona 16.03.2016 в 13:27
source

3 answers

2

This is how it should work, I notice that it has an error:

  • You must have obtained the registration value by (scope) before verify its value to assign the corresponding CSS class.

  • What the query returns is an array (array):

    while ($registros = mysqli_fetch_array($result))

    ... and you should use count($result) > 95 .

  • It stays like this:

    <div class="<?php echo ($registros < 95)?'panel panel-red':'panel panel-blue';?>">
    
        
    answered by 23.10.2016 в 02:03
    1

    I think you're using the condition poorly. It should be something like this:

    <div <?php if ($registros <95) { echo 'class="panel panel-red"'; } else { echo 'class="panel panel-blue"'; } ?>>
    

    In any case, if you want to use the ternary operator, you could do it like this:

    <div class="<?php echo $registros < 95 ? 'panel panel-red' : 'panel panel-blue'; ?>">
    
        
    answered by 16.03.2016 в 13:40
    0

    Try this

    <div class='<?php ($registros <95)? echo 'panel panel-red': echo 'panel panel-blue';?>'>
    

    The point is that the formulation is wrong, remember that a PHP script executes from 'top down' and your field $registro define it after in your SQL query, that is, It does not yet exist when you call it in a ternary above.

    I recommend you do something like this (I did not check the script and it may have errors, but for you to understand the idea)

    <?php
    
    $sql = "SELECT ANSMensual1 FROM cauctti.datosglobales where Ambito like 'SSU Intern'";
    $result = mysqli_query($connection,$sql);
    
    $registroGuardado = 0;
    $registros = 0;
    
    while ($registros = mysqli_fetch_array($result))
    {
        $registroGuardado = $registros["ANSMensual1"];
    }
    
    ?>
    
    <div class="col-lg-3 col-md-6">
        <div class='<?php ($registros < 95)? echo 'panel panel-red'; : echo 'panel panel-blue';?>'>
            <div class="panel-heading">
                <div class="row">
                    <div class="col-xs-3">
                        <i class="fa fa-percent fa-5x"></i>
                    </div>
                    <div class="col-xs-9 text-right" style="font-size:55px;">
                         <?php echo $registroGuardado; ?>
                    </div>
                </div>
            </div>
            <a href="detallmensual.php">
                <div class="panel-footer">
                    <span class="pull-left">ANS MENSUAL SSU Intern</span>
                    <div class="clearfix"></div>
                </div>
            </a>
        </div>
    </div>   
    

    greetings

        
    answered by 17.03.2016 в 14:42