Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Different CSS depending on which MySQL table data is taken from?

Hello Treehousers!

I'm using PHP to take data from 2 MySQL database tables. I want to be able to signify on the front-end which table the data is coming from.

I want to be in a position where:

If result is from table X, style it like this,

If result is from table Y, style it like this

Is it possible to apply a class to the data as it is retrieved so that I can use css to style just the results of that class? I'm really unsure on how to approach the problem and any help or suggestions would be very gratefully received!

Regards,

Harry

3 Answers

Hi Harry

why dont you have like a variable called $class; and depending on which table it is initialise the $class;

if($table=="tablex"){
  $class="tablex";
}
else{
 $class="tabley";
}
<table class="<?php if(isset($class)){echo $class;}  ?>">
</table>
css

.tablex{
  background-color:blue;
}
.tabley{
    background-color:grey;
}

Thanks Andreas, I understand what you've said and I'll give it a whirl!

Joseph Wagner
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Joseph Wagner
Android Development Techdegree Graduate 27,137 Points

I'm thinking you could do this three ways:

  1. On the individual query foreach's
  2. On the form
  3. On the foreach's based on differentiating factor in the data

For example you have query1 and query2.

PDO Fetchall like usual, as $results1.

foreach($results1 as $result1)
{echo "<p class='infield'></p>";}

PDO Fetchall like usual, as $results2.

foreach($results2 as $result2)
{echo "<p class='outfield'></p>";}

Thus said, that's separating based on the fact they are different queries and I'm not sure how you want to format this.

Some other thinking, you could put an extra hidden input field with some data in it if the tables collect data from different forms. This would connote that it belongs to that table.

Otherwise if the data contains differentiating info you could add the class with an if/else statement in the loop. For example you have a table of infield, and table of outfield,

foreach($positions as $position)
{
    if ($position['position'] == "1st" OR "2ND" OR "3RD") 
        {$posclass = "infield";}
    else 
        {$posclass = "outfield";}

    echo "<p class=' '.$posclass.' '> </p>"; 
}

Hope this helps :)

Kevin Korte
Kevin Korte
28,148 Points

Sounds like it really depends on how your deciding which data to pull from. There is not much details here but it sounds like whatever conditional logic you are using to decided which table to pull from, you could use the same or similar conditional logic to append a class to it's container element before being sent to the browser. Than your CSS would apply based on that class.