gpt4 book ai didi

php - 如何将数据库记录分布在多个列上?

转载 作者:行者123 更新时间:2023-11-29 14:53:14 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com