'PHP How to color values based on its content for different columns in a function
I have a problem with my code of displaying data. First of all I want ot apologize for my title, I am not sure how best to formulate my problem.
I want to get the respective values from a table with 4 columns and display them in a table. Based on the respective value, this should be colored differently. I do this with a bootstrap button. For example
<button type="button" class="btn btn-warning" disabled>English</button>
The coloring of the buttons differs with using a different class for each value.
The table structue looks like this:
| name | q1 | q2 | q3 | q4 |
| --- | --- | --- | --- | --- |
| test | math | english| |latin
| test2 | english | |biology |french
I get the values and put them into the table like this:
<table class="table table-striped table-hover table-bordered" id="table-list">
<thead>
<tr><th>Name</th><th>Class</th><th>Course in Q1</th><th>Course in Q2</th></tr>
</thead>
<tbody>
<?php
while ($zeile = mysqli_fetch_array( $db_erg, MYSQLI_ASSOC))
{
?>
<tr>
<td><?php echo $zeile['name'];?></td>
<td><?php echo $zeile['q1'];?></td>
<td><?php echo $zeile['q2'];?></td>
<?php } ?>
</tbody>
</table>
The simplest but unattractive coding way would be for me to do a lot if IF operations...
if ($zeile['q1']=="english") {$buttonq1='<button type="button" class="btn btn-warning" disabled>English</button>';}
if ($zeile['q1']=="math") {$buttonq1='<button type="button" class="btn btn-danger" disabled>Math</button>';}
if ($zeile['q4']=="math") {$buttonq4='<button type="button" class="btn btn-danger" disabled>Math</button>';}
For all values (english, math, latin,...) and all columns (q1-q4). In the table there would be a echo $buttonq1, echo $buttonq2, ...
But there a got to be a more elegant way. Arround the construct of all IF operations the only little thing that changes is the column (q1,q2,q3,q4).
So I thought of writing a function:
function buttonmake ($quart)
{
if ($zeile[$quart]=="english") {$button='<button type="button" class="btn btn-warning" disabled>English</button>';}
if ($zeile[$quart]=="math") {$button='<button type="button" class="btn btn-danger" disabled>Math</button>';}
if ($zeile[$quart]=="Latin") {$button='<button type="button" class="btn btn-warning" disabled>Latin</button>';}
if ($zeile[$quart]=="") {$button='<a class="btn btn-primary" href="project.php?name='.$name.'&kurs=q1&class='.$class.'" role="button">+ Project</a>';}
return $button;
}
So in my table I would just use it like this:
<td><?php echo buttonmake("q1"); ?></td>
<td><?php echo buttonmake("q2"); ?></td>
But it is not working. I am sure it is because the $zeile is empty, but I do not know how to get it into my function buttonmake..
Hope someone can help me. Kind regards Daniel
Solution 1:[1]
The reason you can't access $zeile is because it has not been passed to the function. Have a read about "function scoping". You can do 2 things, you can pass $quart and $zeile to the function, or you can do global $zeile; at the start of the function.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jacob Mulquin |
