user8380237
user8380237

Reputation:

how to change table cell color depending on values in php

I just started programming in php. I want to change cell color depending on some values. For example if quantity < 1000 it should display red color. I don't understand how to do it. Please help me. This is my code. This is what I have tried so far. please let me know what is wrong and what needs to be done. Thanx in advance

         <div class="c-content-panel">
                    <div class="c-label">Basic example</div>
                    <div class="c-body">
                        <div class="row">
                            <div class="col-md-12">
                                <table class="table">
                                 <caption>Optional table caption. </caption>
                                   <thead>
                                        <tr>
                                            <th>#</th>
                                            <th>Store Id</th>
                                            <th>Item Name</th>
                                            <th>Quantity</th>
                                             <th>Status</th>

                                        </tr>
                                    </thead>
                                    <tbody>   
             <?php while ($row =mysqli_fetch_array($query))                                    
                                    { 
                                       echo " <tr> 
                                       <td>--</td>
                                        <td>{$row['store_id']}
                                        </td>
                                         <td>{$row['store_name']}</td>
                                        <td>{$row['quantity']}</td>
                                         if($row['quantity']<1000)
                                            {
                         echo "<td style='background-color: #FF0000;'></td>"

                                       }
                                       else
                                       {
                        echo "< td style='background-color: #FFFF00;'></td>"
                                       }

                                         </tr>";

                                          echo " <tr> 
                                       <td>--</td>
                                        <td>{$row['store_id']}</td>
                                         <td>{$row['mphone']}</td> 

                                  <td>{$row['quantity_mphone']}</td> </tr>";

                                            echo " <tr> 
                                       <td>--</td>
                                        <td>{$row['store_id']}</td>
                                         <td>{$row['smart_devices']}</td> 

                        <td>   {$row['quantity_smart_devices']}</td> </tr>";

                                          echo " <tr> 
                                       <td>--</td>
                                        <td>{$row['store_id']}</td>
                                         <td>{$row['power']}</td> 

                                         <td>{$row['quantity_power']}</td> 
                                          </tr>";
                                            echo " <tr> 
                                       <td>--</td>
                                        <td>{$row['store_id']}</td>
                                         <td>{$row['audio']}</td> 

                                         <td>{$row['quantity_audio']}</td>    

                                          </tr>";}

                                         ?> 

                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>

Upvotes: 1

Views: 5448

Answers (2)

JYoThI
JYoThI

Reputation: 12085

1st: Simple break the previous echo using ;

2nd : simple use ternary operator

echo "<td style='background-color:".(($row['quantity'] < 1000) ? '#FF0000;' : '#FFFF00')."'></td>";

Upvotes: 3

B. Desai
B. Desai

Reputation: 16436

You can't write if in echo. You have to terminate echo before using if

  <div class="c-content-panel">
      <div class="c-label">Basic example</div>
      <div class="c-body">
          <div class="row">
              <div class="col-md-12">
                  <table class="table">
                   <caption>Optional table caption. </caption>
                     <thead>
                          <tr>
                              <th>#</th>
                              <th>Store Id</th>
                              <th>Item Name</th>
                              <th>Quantity</th>
                               <th>Status</th>

                          </tr>
                      </thead>
                      <tbody>   
<?php while ($row =mysqli_fetch_array($query))                                    
                      { 
                         echo " <tr> 
                         <td>--</td>
                          <td>{$row['store_id']}
                          </td>
                           <td>{$row['store_name']}</td>
                          <td>{$row['quantity']}</td>";
                           if($row['quantity']<1000)
                              {
           echo "<td style='background-color: #FF0000;'></td>";

                         }
                         else
                         {
          echo "< td style='background-color: #FFFF00;'></td>";
                         }

                           echo "</tr>";

                            echo " <tr> 
                         <td>--</td>
                          <td>{$row['store_id']}</td>
                           <td>{$row['mphone']}</td> 

                    <td>{$row['quantity_mphone']}</td> </tr>";

                              echo " <tr> 
                         <td>--</td>
                          <td>{$row['store_id']}</td>
                           <td>{$row['smart_devices']}</td> 

          <td>   {$row['quantity_smart_devices']}</td> </tr>";

                            echo " <tr> 
                         <td>--</td>
                          <td>{$row['store_id']}</td>
                           <td>{$row['power']}</td> 

                           <td>{$row['quantity_power']}</td> 
                            </tr>";
                              echo " <tr> 
                         <td>--</td>
                          <td>{$row['store_id']}</td>
                           <td>{$row['audio']}</td> 

                           <td>{$row['quantity_audio']}</td>    

                            </tr>";}

                           ?> 

                      </tbody>
                  </table>
              </div>
          </div>
      </div>
  </div>

Upvotes: 1

Related Questions