3
votes

I have a database in MySQL using Php. I fetch it and display it in table form.

I was trying to highlight the data with specific color with two conditions:

Condition 1:- whose age is grater than 24 (color is #FF7676) red

Condition 2:- whose age less than 20 (color is #F8FF00) Yellow

but when my all data is in yellow color

<html>
<head>
    <title>DataBase of Age</title>
    <link rel="stylesheet" href="bootstrap-3.3.1-dist/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="bootstrap-3.3.1-dist/dist/css/bootstrap-theme.min.css">
    <script src="bootstrap-3.3.1-dist/dist/js/bootstrap.min.js">
    </script>
    <style>
        .grater
        {
            background-color:#FF7676;
        }
        .small
        {
            background-color:#F8FF00;
        }
    </style>
</head>
<body>
    <table class="table">
        <tr>
            <td>#</td>
            <td>Name</td>
            <td>Age</td>
            <td>City</td>
            <td>Post</td>
            <td>Salary</td>
        </tr>
        <?php
        $connection=mysqli_connect('localhost','root','','db_employee') or die(mysqli_connect_error());
        echo"DataBase Connection Establish Successfully...!!!";
        $q=mysqli_query($connection,'select * from tbl_information');
        echo"Total number of record found are:-".mysqli_num_rows($q);
        $age=1;
        while($table=mysqli_fetch_assoc($q))
        {
            extract($table);
            $class=null;
            if($age>24)
            {
                $class='grater';
            }
            if($age<20)
            {
                $class='small';
            }
            $age++;
        ?>
        <tr class="<?=$class?>">
            <td><?=$emp_id?></td>
            <td><?=$emp_name?></td>
            <td><?=$emp_age?></td>
            <td><?=$emp_city?></td>
            <td><?=$emp_post?></td>
            <td><?=$emp_salary?></td>
        </tr>
        <?php
        }
        ?>
    </table>
</body>

Please help me the code is not working.

3
$age=1; whats is this ? - Hendra Nucleo
try <tr class="<?echo $class;?>"> - vidhya
How many rows does your output have? As per the current code, the first 20 rows should be yellow. I think you are trying to fetch age from DB but the code actually isn't doing that. - Harry
@VidhyaRaju <?= equal to <? echo thats not a problem. - Hendra Nucleo
@Harry Yes, i think he trying $emp_age > 20 - Hendra Nucleo

3 Answers

1
votes
if($age>24)
{
   $class='grater';
}
if($age<20)
{
   $class='small';
}

It will be

if($emp_age>24)
{
   $class='grater';
}
else if($emp_age<20)
{
  $class='small';
}
0
votes

Your problem here

if($age>24)
{
   $class='grater';
}
if($age<20)
{
   $class='small';
}

It will be

if($emp_age>24)
{
   $class='grater';
}
else if($emp_age<20)
{
  $class='small';
}
0
votes

Try this:

    echo"Total number of record found are:-".mysqli_num_rows($q);

    while($table=mysqli_fetch_assoc($q))
    {
        extract($table);
        $class=null;
        if($emp_age>24)
        {
            $class='grater';
        }
        if($emp_age<20)
        {
            $class='small';
        }

    ?>
    <tr class="<?=$class?>">
        <td><?=$emp_id?></td>
        <td><?=$emp_name?></td>
        <td><?=$emp_age?></td>
        <td><?=$emp_city?></td>
        <td><?=$emp_post?></td>
        <td><?=$emp_salary?></td>
    </tr>
    <?php
    }
    ?>
</table>