如何在多列上传播数据库记录?

时间:2011-03-13 11:26:44

标签: php mysql html html-table

我有这个代码用于从mysql打印多列表

$k="<table width='100%' border='0' cellpadding='5'><tr>"; 
$h=mysql_query("select * from news order by id desc limit 0,12");
$col=0;
while($row=mysql_fetch_assoc($h)){
    $col++; 
    $k .="
    <td>
    Text here
    </td>
    "; 
    if($col==3){
        $k .="</tr><tr>";
        $col=0;    
    }  
}
$k .="</tr></table>";
echo $k;

我想在此表中添加一个随机列,例如广告代码,我希望每列显示一次广告代码。

输出应该是这样的

<table border="1" width="100%">
    <tr>
        <td>Title :$row[name]<br>
        description : $row[full]</td>
        <td>ADV Code1</td>
        <td>Title :$row[name]<br>
        description : $row[full]</td>
    </tr>
    <tr>
        <td>ADV Code2</td>
        <td>Title :$row[name]<br>
        description : $row[full]</td>
        <td>Title :$row[name]<br>
        description : $row[full]</td>
    </tr>
    <tr>
        <td>Title :$row[name]<br>
        description : $row[full]</td>
        <td>Title :$row[name]<br>
        description : $row[full]</td>
        <td>ADV Code3</td>
    </tr>
    <tr>
        <td>ADV Code4</td>
        <td>Title :$row[name]<br>
        description : $row[full]</td>
        <td>Title :$row[name]<br>
        description : $row[full]</td>
    </tr>
</table>

我该怎么做?

此致

2 个答案:

答案 0 :(得分:1)

回答+一些建议:

  • 分开数据检索显示说明
  • 提高代码可读性:为变量指定相关名称($h$k是什么?)

添加“随机广告”后,您的代码应如下所示:

$columns_number = 3;

// data retrieving

$ar_advertisements = array('advert1', 'advert2', 'advert3', 'advert4');

$mysql_result = mysql_query("SELECT * FROM news ORDER BY id DESC LIMIT 12");
$ar_news = array();

while ($row = mysql_fetch_assoc($mysql_result))
  $ar_news[] = $row;


// display : merge news with 'random advertisements' on each table row

$html = '<table width="100%" border="0" cellpadding="5"><tr>';

$ad_displayed_on_row = false;
$advert_index = 0;
$cell_index = 1;

foreach ($ar_news as $news)
{
  // new row every X columns
  if ($cell_index > $columns_number)
  {
    $html .="</tr><tr>\n";
    $cell_index = 1;
    $ad_displayed_on_row = false;
  }

  // ensure that an advertisement is displayed only once for each table row, at a random position
  if ((mt_rand(1, $columns_number) == 1 && !$ad_displayed_on_row && $cell_index < $columns_number)
    || ($cell_index == $columns_number && !$ad_displayed_on_row))
  {
    $html .= "<td>".$ar_advertisements[$advert_index]."</td>\n";
    $ad_displayed_on_row = true;
    $cell_index++;

    $advert_index++;
    if ($advert_index >= count($ar_advertisements))
      $advert_index = 0;
  }

  // here I'm supposing that your 'news' table contains a 'text' field,
  // you should modify it to your convenience.
  if ($cell_index <= $columns_number)
  {
    $html .="<td>".$news['text']."</td>\n";
    $cell_index++;
  }
}

// complete last row with empty cells if necessary
// If you don't want them to by empty, just change &nbsp; by whatever you want
// complete last row with empty cells if necessary
if ($cell_index != 1)
{
  while($cell_index <= $columns_number)
  {
    $html .= "<td>&nbsp;</td>\n";
    $cell_index++;
  }
}

$html .="</tr></table>";

echo $html;

答案 1 :(得分:0)

我认为这是合适的

$h=mysql_query("select * from news order by id desc");

if($h){
    echo "<table width='100%' border='0' cellpadding='5'><tr>";
    while($row=mysql_fetch_assoc($h)){
        echo "<tr>";

        /**
        ...
        echo "<td>".$row['field']."</td>";
        echo other stuff
        as well as your random stuff
        ...
            **/
        echo "</tr>";
    }
    echo "</table>";
}