Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/258655-table-styling-question/
Share on other sites

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.