我有这段代码用于从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>


我怎样才能做到这一点?

问候

最佳答案

答案+一些建议:


单独的数据检索和显示说明。
提高代码的可读性:为变量赋予相关名称($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;

关于php - 如何将数据库记录分布在多列上?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5289053/

10-11 06:07