【问题标题】:How to spread database records on multiple columns?如何将数据库记录分布在多列上?
【发布时间】:2011-03-13 21:52:16
【问题描述】:

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

我该怎么做?

问候

【问题讨论】:

  • “添加随机列”到底是什么意思?您想在包含广告的 3 列表格中添加“随机单元格”吗?最初显示多少个单元格?您要添加多少个“随机广告单元”?...
  • 原始单元格为 3,我想随机广告单元格在原始 3 单元格中为 1
  • @Pin 你的问题到底是什么?您看到的是&lt;td&gt;,是什么阻止您向它们注入您喜欢的任何内容?
  • 我有 3 个单元格显示.. 我需要在 3 个单元格内添加一个随机单元格(广告)
  • @Pin 那么是什么阻止你添加它?

标签: php mysql html html-table


【解决方案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;

【讨论】:

  • 我在行(for($ar_results as $result_index => $result){)中遇到了这个错误(解析错误:语法错误,意外的 T_AS,期待 ';)
  • Aww 抱歉,没有看到这个“我想在此表中添加一个随机列,如广告感知代码,并且我希望广告感知代码每列显示一次。”...我的回答与您的规格不完全相符
  • 我修复了将 (for) 替换为 (foreach)
  • 我看到了你的代码,但这不是我要找的......我想在我原来的 3 个单元格中添加一个单元格用于广告。通过您显示的代码,但 3 个原始代码的排列有时是错误的,有时显示 2 列,有时每行显示 1 列。我需要的是我的 3 个原始单元格中的一个广告单元格,因此每行的总单元格为 (3)。
  • 好的,我已经测试并修复了它。现在应该可以工作了(只需检查您要从 news 表中显示的“文本”字段的名称是什么)。
【解决方案2】:

我觉得这样比较合适

$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>";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    相关资源
    最近更新 更多