BelowZero Posted March 10, 2012 Share Posted March 10, 2012 I'm trying to learn how to display data from my database correctly. I came across this example in my google search. <?php $array['Name'] = array('Chris', 'Jason', 'Thomas', 'John'); $array['Age'] = array('27','24','40','32'); $array['Movie'] = array('SpaceBalls','Friday 13th','Star Wars','The Matrix'); ?> <table> <tr style='border: 1px black solid;'> <th style='border: 1px black solid;'>Name</th> <th style='border: 1px black solid;'>Movie</th> <th style='border: 1px black solid;'>Age</th> </tr> <? $i= 0; while ($i <=3){ echo "<tr>"; echo "<td style='border: 1px black solid; text-align: center; width: 100px'>".$array['Name'][$i]."</td>"; echo "<td style='border: 1px black solid; text-align: center; width: 120px;'>".$array['Movie'][$i]."</td>"; echo "<td style='border: 1px black solid; text-align: center; width: 50px'>".$array['Age'][$i]."</td>"; echo "</tr>"; $i++; } ?> </table> My question is: Why did this example style the <td> inside the echo statement echo "<td style='border: 1px black solid; text-align: center; width: 100px'>".$array['Name'][$i]."</td>"; and not in the inline table styling or in a CSS stylesheet? Is this the "correct" way or just the author's choice or is there a particular reason to do it this way? I'd like to make sure I understand the programming before I write it and later discover that I'm using poor logic and technique. Any insights are appreciated. Thanks. Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted March 10, 2012 Share Posted March 10, 2012 Handling information from a database is a bit different then grabbing data from a static array. But as to your question, using inline styles should typically be avoided on large scale projects, as this will lead to alot more code then is necessary. Typically you want to use classes and id's to style elements using one ore more external style sheets. The author of this code probably just styled them inline for a quick example. Quote Link to comment Share on other sites More sharing options...
BelowZero Posted March 10, 2012 Author Share Posted March 10, 2012 Thanks AyKay47. I do plan on using an external style sheet in my project. But my real question is can I style the <td> in the stylesheet or should it be done inline? Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted March 10, 2012 Share Posted March 10, 2012 Thanks AyKay47. I do plan on using an external style sheet in my project. But my real question is can I style the <td> in the stylesheet or should it be done inline? sure, there are different ways to go about this, it depends on how many td elements you want to style. You can give each <td> a class and use the external style sheet to style them. e.g external css: td.class { height: 20px; background-color: red; } html: <td class='class'>something</td> just incorporate it into the php loop. If you want to style only one td element, you can substitute class for id. Quote Link to comment Share on other sites More sharing options...
BelowZero Posted March 10, 2012 Author Share Posted March 10, 2012 Thanks for the explanation! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.