本程序将每个数据打印在一个新行中。我希望它在一行中打印3个数据,然后更改行并在新行中打印其他3个数据。我试过使用for循环,但它没有给我所需的输出。请告诉我们如何在这个程序中执行此操作
<table width="550" border="0" align="center" cellpadding="2" cellspacing="2" bordercolor="#000000">
<tr>
<th align= "left"><a href="#"><table width="160" border="0" align="center" cellpadding="0" cellspacing="0">
<?php
/*Inner Page- Products exists in Certain Category!!
======================================================*/
$sub2_query = "select * from sub_categories where category_id=$category_id ";
$sub2_query_run = mysql_query($sub2_query);
while ($sub2_query_fetch = mysql_fetch_array($sub2_query_run))
{
$sub2_category_id = $sub2_query_fetch['sub_category_id'];
$inner_query = "select * from products inner join product_description
on products.product_id= product_description.product_id
where sub_category_id= $sub2_category_id";
if($inner_query_run = mysql_query ($inner_query))
{
while($inner_query_fetch = mysql_fetch_array($inner_query_run))
{
$product_id = $inner_query_fetch['product_id'];
$inner_image_query = "select * from product_images
where product_id=$product_id";
$inner_image_query_run = mysql_query($inner_image_query);
//PRINTING imagesss
echo "</th><tr><td align='center'>";
echo "<div id='border'>";
echo "<td>";
echo "<a href='index_product_details2.php?product_id=$product_id' >";
$inner_image_query_fetch = mysql_fetch_array($inner_image_query_run);
echo "<img src=http://localhost/cms/".
$inner_image_query_fetch['images']." height='150' width='150' alt=\"\" />
</td>
</tr>
</div>";
?>
<tr>
<td align="center">
<?php
//PRINTING NAMES
print_r($inner_query_fetch['name']);
echo "<br> Price: ";
print_r($inner_query_fetch['actual_price']);
echo "</a>";
echo "<br><br>";
}
}else{
echo mysql_error();
}
}
}
?>
</td>
</tr>
</table>
答案 0 :(得分:0)
尝试这只是我给出的例子
<tr><?php $i=0;
while($inner_query_fetch= mysql_fetch_array($inner_query_run))
{
if($i%3=="0"){ echo "</tr><tr>"; }?>
<td> your html</td><?php $i++;}?></tr>
答案 1 :(得分:0)
我会给你一些伪代码,这样你就可以找到逻辑:
$columns = 3;
$counter = 0;
while($row = mysql_fetch_array($result)){
if($counter%$columns == 0) echo "<tr>";
echo "<td>the things you want to output</td>";
if($counter%$columns == 0) echo "</tr>";
$counter++;
}
模块(%符号)表示如果其余部分获得除以两个数字,10/3 = 3(+ 1),则1将是此模块中的模块,因此您只能在行为倍数时打印tr 3.这是一种非常常见的做法,例如也用于替代行颜色(使用$ rowNumber%2);
备注!! 强>:
您不应再使用mysql_函数,它们已被弃用且不安全(对于sql Injections), 你应该看一下mysqli函数或PDO,我个人建议学习PDO,因为查询是参数化的,而且它非常安全,我喜欢你用它获得的逻辑和可读性。链接: