Hybreeder Hybreeder - 3 months ago 11
CSS Question

How to set background color for specific id in php?

I have to set background color for id.I have 100 records in my table.I have to check balance from database if balance value is 0 than ok but if balance value is not zero than set background color of that row.

php

$sql = "SELECT Balance AS bal FROM dailyentry where id=$id";
$result = $conn->query($sql);
$a=$result->fetch_object()->bal;


if ($a==0) {
echo "no bal";
$query="SELECT * FROM dailyentry WHERE id=$id AND DATE(LoginTime)=CURDATE()";
$result = $conn->query($query);
}
else{
echo " bal pending";
$query1="SELECT * FROM dailyentry WHERE id=$id AND DATE(LoginTime)=CURDATE()";
$result1 = $conn->query($query1);
echo"<link href='css/a.css' rel='stylesheet' type='text/css'/>";
}
//retrive records
$query="SELECT * FROM dailyentry WHERE DATE(LoginTime)=CURDATE()";
$result = $conn->query($query);


html

<form action="" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<input type="text" name="userid">
<input type="submit" name="submit" value="Login">
</form>

<table border="1">
<thead>
<tr>
<th>SrNo</th>
<th>Name</th>
<th>Payment</th>
<th>Balance</th>
</tr>
</thead>
<tbody class="abc red-exp">
<?php

if (isset($result->num_rows) > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>{$row['SrNo']} </td>
<td>{$row['Name']}</td>
<td>{$row['Payment']}</td>
<td>{$row['Balance']}</td>

</tr>
";

}
}

?>
</tbody>


</table>


Getting output like this.
enter image description here

I need output link this
enter image description here

Answer

In single php page (like userbalance.php) You can do it like below:-

<form enctype="multipart/form-data" method="post" accept-charset="utf-8">
    <input type="text" name="userid">
    <input type="submit" name="submit" value="Login">
</form>
<?php
$user_data = array(); // create an array
if(isset($_POST['userid'])){
    $id = $_POST['userid'];
    $conn = new mysqli("localhost", "root", "", "abc");

    /* check connection */
    if ($conn->connect_errno) {
        printf("Connect failed: %s\n", $conn->connect_error);
        exit();
    }
    $sql = "SELECT * FROM dailyentry WHERE id = $id";
    $result = $conn->query($sql);
    while ($row = $result->fetch_assoc()) { // i have used fetch_assoc instead of fetch_row
        $user_data[] = $row; // assign all data to the array
    }
}else{
    $conn = new mysqli("localhost", "root", "", "abc");

    /* check connection */
    if ($conn->connect_errno) {
        printf("Connect failed: %s\n", $conn->connect_error);
        exit();
    }
    $sql = "SELECT * FROM dailyentry";
    $result = $conn->query($sql);
    while ($row = $result->fetch_assoc()) { // i have used fetch_assoc instead of fetch_row
        $user_data[] = $row; // assign all data to the array
    }

}
    ?>
    <table>
    <thead>
      <tr>
        <th>SrNo</th>
        <th>Name</th>
        <th>Payment</th>
        <th>Balance</th>
      </tr>
    </thead>
    <tbody>
      <?php $i=1; if(count($user_data) >0) {foreach ($user_data as $user_dat){ // iterate over the array ?>
        <?php if($user_dat['Balance'] == 0){ // if balance is 0 then row with some other class?>
          <tr class="no_background">
            <td><?php echo $i ;?></td>
            <td><?php echo $user_dat['Name'];?></td>
            <td><?php echo $user_dat['Payment'];?></td>
            <td><?php echo $user_dat['Balance'];?></td>
          </tr>
        <?php }else{// if balance is not 0 then row with some other class?>
            <tr class="some_background">
                <td><?php echo $i ;?></td>
                <td><?php echo $user_dat['Name'];?></td>
                <td><?php echo $user_dat['Payment'];?></td>
                <td><?php echo $user_dat['Balance'];?></td>
              </tr>

        <?php }?>
    <?php $i++;}}else{ echo "<tr><td>sorry no data</td></tr>";} ?>
    </tbody>
    </table>

    <style>
    .some_background{background-color:green;} 
    /* assign background color on class */
    </style>

Note:- if you want all record then you have to remove WHERE id =$id from query.

Comments