开发者

PHP/HTML/CSS: Styling an array of data

When importing an array of data from a database (i'm using php/mysql), how do I go about styling this information with css? I'm assuming I add html tags to each separate piece of data 开发者_StackOverflow中文版from the array? but if so at which stage would I assign these tags?

I'm a bit new to php but am ok with css and xhtml.

At the moment my array contains:

Business_Name  |  Address  |  Tel | Service  | Url | Email | Recommendations | Image_Url

Any help would be much appreciated.

Edit: A picture paints a thousand words :0)

PHP/HTML/CSS: Styling an array of data


try doing a for loop width you array

something like

<?php
  echo "<ul>";
 foreach($array as $arrkey){
     echo "<li>".$arrkey."</li>";   

 }
 echo "</ul>";
?>

and do some css on the <li>.

edit:

 <?php

   $cont=0;

      echo '<ul>';
     foreach($array as $arrkey){
         echo '<li class="yourclass-.$cont.'">'.$arrkey.'</li>';   
        $cont++;
     }
     echo "</ul>";
    ?>

then style each <li>

like

li.yourclass-0{

}


This is quite an open ended question, but I'll endeavour to give you some general pointers.

There are a few options open to you. The easiest and most straightforward is to just start spitting out HTML from your PHP script, like so:

<table><tr><td><?php echo $arr['Business_Name']; ?></td>...<td><img src="<?php echo $arr['Image_Url']; ?>"></td></tr></table>

In short, echo is your friend.

That's not always the best option though - if you have anything more than a very simple script, this kind of approach can get very messy and hard to follow very quickly. Your beautifully formatted PHP get all gummed up with HTML, and it's not pretty, and becomes hard to maintain.

What you want to do in this case is separate your concerns - keep "display" logic separate from "business" logic.

This can be done in a few different ways - I suggest you look at some templating tools like Smarty or some frameworks such as Zend - these go a long way towards separating business logic from display logic.


I'm guessing you are inquiring only about "designing" the output. If so, the question become more vague.

Designing depends on how you wish to show these data to your viewers. If you want a tabular structure, use <table>. Or if they are to be shown as a list, use <ul> or <ol> and use CSS to style it. Adding style properties inline is a very bad approach. Avoid that.

Like Alex said, this question is very open ended and vague. Try refining your question a little more.


if we suppose $res is your result set then ,

$str = "table width='100%'";

while($row = mysql_fetch_array($res)){ $str .= "tr td class='class_name'".$row['col_name']." /td /tr"; }

$str .= "/table";

echo $str;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜