Page 1 of 1

Re: change td bgcolor based on mysql value

Posted: Sat Jan 16, 2016 8:13 am
by Celauran
You mean something like this? http://codepen.io/anon/pen/yezvqg

Re: change td bgcolor based on mysql value

Posted: Sat Jan 16, 2016 8:30 am
by Celauran
Right, I understand. I don't have any values of anything because it's all static. I was just showing that it's easily done with a simple CSS class. I don't see any logic in your code above, but adding a class to the parent element if a given value is > 3 should be trivial. What have you tried? What's tripping you up?

Re: change td bgcolor based on mysql value

Posted: Sat Jan 16, 2016 8:37 am
by Celauran
I don't see any switch statements or comparisons at all in your code above, though. That's why I was asking.

Re: change td bgcolor based on mysql value

Posted: Sat Jan 16, 2016 8:55 am
by Celauran
Something like this should do the trick

Code: Select all

<?php

// connect to the database
include('connect-db.php');

// get the records from the database
$result = $mysqli->query("SELECT id, software_title, software_number, times_used, customers_name FROM software ORDER BY id");

?>

<style>
.red {
    background-color: #ff0000;
}
.grey {
    background-color: #d4d4d4;
}
</style>

<?php if ($result && $result->num_rows > 0): ?>
    <table class="records">
        <tr>
            <th>ID</th>
            <th>Software Title</th>
            <th>Product_key</th>
            <th>Times Used</th>
            <th>Customers Name</th>
            <th colspan="1">Actions</th>
        </tr>

        <?php while ($row = $result->fetch_object()): ?>
            <?php $class = $row->times_used == 3 ? 'red' : 'grey'; ?>
            <tr>
                <td><?= $row->id; ?></td>
                <td><?= $row->software_title; ?></td>
                <td><?= $row->software_number; ?></td>
                <td class="<?= $class; ?>"><?= $row->times_used; ?></td>
                <td><?= $row->customers_name; ?></td>
                <td><a href="add-edit-software-used.php?id=<?= $row->id; ?>">Edit</a></td>
            </tr>
        <?php endwhile; ?>

    </table>
<?php else: ?>
    <p>No results to display!</p>
<?php endif; ?>

Re: change td bgcolor based on mysql value

Posted: Sat Jan 16, 2016 9:00 am
by Celauran
Note that it's generally a bad practice to have style declarations in your markup like that. I simply included them to provide a more complete example.

I think where you were running into trouble is your function was echoing a string rather than returning it, and was being called inside an echo. Neither of those should be the case. You could rewrite it something like this:

Code: Select all

<?php
function switchColor($rowValue) {

    //Define the colors first
    $color1 = '#e2e2e2';
    $color2 = '#00ff00';

    $selected = $color2;
    /*Change the 'cases' to whatever you want them to be,  
    so if you want to change the color according to  
    occupation, write down the possible occupations or if  
    the color changes according to gender, name the gender
    names that come out of the database (eg. case 'male':).*/
    switch ($rowValue) {
        case '3':
            $selected = $color1;
            break;  
        default:
            $selected = $color2;
    }

    return $selected;
}

Re: change td bgcolor based on mysql value

Posted: Sat Jan 16, 2016 9:13 am
by Celauran

Code: Select all

                        if ($result = $mysqli->query("SELECT id, software_title, software_number, times_used, customers_name FROM software ORDER BY id"))
                                               
                        {
                                // display records if there are records to display
                                if ($result->num_rows > 0)
                                {
                                                                       
                                                                        if ($result && $result->num_rows > 0);
Lots of duplication there. Also ; after an if statement.

Code: Select all

                                        while ($row = $result->fetch_object())
                                                                                $class = $row->times_used == 3 ? 'red' : 'grey';
                                                                                                                       
                                                                                {
Your { should be after the while and before the $class

Code: Select all

echo "<td class='<?= $class; ?>'>"
You're calling echo inside an echo statement. Just use

Code: Select all

echo "<td class='$class'>";
Although, really, you shouldn't be echoing HTML like that. I had cleaned it up for a reason.