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!
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

Harry Fox
5,571 PointsDifferent 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

Andreas cormack
Python Web Development Techdegree Graduate 33,011 PointsHi 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;
}

Joseph Wagner
Android Development Techdegree Graduate 27,137 PointsI'm thinking you could do this three ways:
- On the individual query foreach's
- On the form
- 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
28,147 PointsSounds 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.
Harry Fox
5,571 PointsHarry Fox
5,571 PointsThanks Andreas, I understand what you've said and I'll give it a whirl!