jungkookie jungkookie - 5 months ago 16
PHP Question

Print each row with a different color PHP

So i'm exploring PHP and working on a web app. I display a table from my database. However, i want each row to be displayed in a different color.
For example: row1 in green; row2 in blue; row3 in grey... And so on!

Here's my code:

<?php

$username = 'root';
$pwd = '';
$db_name = 'fileattente';

$db = new mysqli('localhost', $username, $pwd, $db_name) or die("Unable to connect.");
?>


<table>
<tr>
<th><span class="Style1">SERVICE</span> </th>
<th><span class="Style2">NUMERO</span></th>
<th><span class="Style3">GUICHET</span></th>
<th><span class="Style4">EN ATTENTE</span></th>
</tr>


<?php

$req_service_1="SELECT `LIBESERV`, `CODESERV`, `CODEGUIC` , `NOMBATTE` FROM `v_attente_service`";
$stid1= mysqli_query($db, $req_service_1);
while ( $row = mysqli_fetch_array($stid1))
{
?>
<!--While true, do the following-->
<tr>
<td class="Style14"><span>
<?php echo $row['LIBESERV'];?>
</span></td>
<td><?php echo $row['CODESERV'];?> </td>
<td><blink><?php echo $row['CODEGUIC'];?></blink></td>
<td><?php echo $row['NOMBATTE'];?></td>

<?php
} //End of while loop
?>


Already looked through some other similar questions here, but they alternate 2 colors between rows, and that is not what i want.

Thanks in advance!

Answer

You can try this code

<?php

$username = 'root';
$pwd = '';
$db_name = 'fileattente';

$db = new mysqli('localhost', $username, $pwd, $db_name) or die("Unable to connect.");
?>
<table>
  <tr>
   <th><span class="Style1">SERVICE</span> </th>
   <th><span class="Style2">NUMERO</span></th>
   <th><span class="Style3">GUICHET</span></th>
   <th><span class="Style4">EN ATTENTE</span></th>
  </tr>

<?php
  $req_service_1="SELECT `LIBESERV`, `CODESERV`, `CODEGUIC` , `NOMBATTE` FROM `v_attente_service`";
  $stid1= mysqli_query($db, $req_service_1);
  $records = mysqli_fetch_array($stid1)
  $colors = array('red', 'green', 'blue', 'yellow', 'black');
  for($i=0; $i<$count($records); $i++){
  ?>
    <tr style="background-color: <?php echo $colors[$i]; ?>">
      <td><span><?php echo $row['LIBESERV'];?></span></td>
      <td><?php echo $row['CODESERV'];?></td>   
      <td><blink><?php echo $row['CODEGUIC'];?></blink></td>
      <td><?php echo $row['NOMBATTE'];?></td>
    </tr>
  <?php
  }
  ?>