Using PHP and Mysql to control CSS /HTML elements

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

MikeSpider
New php-forum User
New php-forum User
Posts: 49
Joined: Sun Feb 05, 2012 12:48 pm

Using PHP and Mysql to control CSS /HTML elements

Postby MikeSpider » Mon Nov 19, 2012 3:00 pm

Hi guys,
this is something I have been doing a long time but I really don't know if this is the right path.
I'll give a basic example.
if I have a stock table, and I want the row to have a yellow background color code warning when an item reaches 2 in quantity, and to turn red when the item reaches 0 in quantity.

What I did was something like this:

Code: Select all

<?php
$stock= new Product();
$stock->GetItems();

if($stock-data > 0){
foreach($stock->data as $st){

if($st['qty'] <= 2){
      echo "<td class='bgYellow'>". $st['id']. "</td>";
      echo "<td class='bgYellow'>". $st['name']. "</td>";
      echo "<td class='bgYellow'>". $st['price']. "</td>";
      echo "<td  class='bgYellow'>". $st['qty']. "</td>";
}elseif($st['qty'] == 0){
      echo "<td  class='bgRed'>". $st['qty']. "</td>";
      echo "<td  class='bgRed'>". $st['name']. "</td>";
      echo "<td  class='bgRed'>". $st['price']. "</td>";
      echo "<td   class='bgRed'>". $st['qty']. "</td>";

}else{
echo "<td>". $st['qty']. "</td>";
      echo "<td>". $st['name']. "</td>";
      echo "<td>". $st['price']. "</td>";
      echo "<td>". $st['qty']. "</td>";
}
}
}
?>


I have also used this method but extended a bit, like creating switches on db which is nothing more than a state which is either yes/no (or true/false), I can then present certain html elements acording to those switches states. I used that for instance to make a check box default state to checked according to what the user chose earlier.

I need your opinion on this, if this is awkward, if it should really be done in client side using javascript.
Any feedback will be appreciated.

Mike

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Using PHP and Mysql to control CSS /HTML elements

Postby seandisanti » Mon Nov 19, 2012 3:25 pm

No need to do it client side (beyond the css which is still rendered client side of course).

I think you're over thinking it a bit. why not just do it like...

Code: Select all

<style type="text\css">
.
empty {
background-color:#f00;
}
.
low {
background-color:#ff0;
}
.
good {
background-color:#9f6;
}
<?
php

{
    switch ($qty) {
        case 0:
            $qty_class="empty";
            break;
        case 
1:
            
$qty_class="low";
            break;
        case 2:
            $qty_class="low";
            break;
        default:
           $qty_class="good"
    }
    echo "<td class='" . $qty_class . "'>". $st['id']. "</td>";
    echo "<td class='" . $qty_class . "'>". $st['name']. "</td>";
    echo "<td class='" . $qty_class . "'>". $st['price']. "</td>";
    echo "<td  class='" . $qty_class . "'>". $st['qty']. "</td>";    
}




***edit***
added case (1) so that case wouldn't return 'good'

MikeSpider
New php-forum User
New php-forum User
Posts: 49
Joined: Sun Feb 05, 2012 12:48 pm

Re: Using PHP and Mysql to control CSS /HTML elements

Postby MikeSpider » Mon Nov 19, 2012 6:15 pm

yes that's much easier.
Thanks

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Using PHP and Mysql to control CSS /HTML elements

Postby seandisanti » Tue Nov 20, 2012 12:35 pm

No problem, glad to help


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest