php表格内的随机单元格
我有这段代码用于从 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]</td>
<td>ADV Code1</td>
<td>Title :$row[name]</td>
</tr>
<tr>
<td>ADV Code2</td>
<td>Title :$row[name]</td>
<td>Title :$row[name]</td>
</tr>
<tr>
<td>Title :$row[name]</td>
<td>Title :$row[name]</td>
<td>ADV Code3</td>
</tr>
<tr>
<td>ADV Code4</td>
<td>Title :$row[name]</td>
<td>Title :$row[name]</td>
</tr>
</table>
我该怎么做?
问候 皮尼
I have this code for print a multi columns table from 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;
I want to add a random cells inside this table like ad-sense codes and I want the ad-sense code to display once per column.
the output should be like this
<table border="1" width="100%">
<tr>
<td>Title :$row[name]</td>
<td>ADV Code1</td>
<td>Title :$row[name]</td>
</tr>
<tr>
<td>ADV Code2</td>
<td>Title :$row[name]</td>
<td>Title :$row[name]</td>
</tr>
<tr>
<td>Title :$row[name]</td>
<td>Title :$row[name]</td>
<td>ADV Code3</td>
</tr>
<tr>
<td>ADV Code4</td>
<td>Title :$row[name]</td>
<td>Title :$row[name]</td>
</tr>
</table>
How can i do this?
Regards
Piny
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 rand(0,2) 它将给你一个 0 到 2 之间的随机数。每当有一个新行时,你就创建一个随机数,就像
因为你正在计算 $col 中的列,所以你可以去
然后,如果创建新行(以及在循环之前),您分配一个新的随机数:
或者类似的东西。
Use rand(0,2) which will give you a random number between 0 and 2. Whenever there is a new row, you create a random number, like
Since you are counting the columns in $col, you can go
Then, if a new row is created (as well as before the loop), you assign a new random number:
Or something like this.