dtgvl48608 2014-03-17 18:03
浏览 25
已采纳

如何每隔2个查询添加一行

So the code below is creating a 2 boxes next to each other once on the right and one on the left. I need to start a new row now but can't figure out how. MySQL does a new line for each box but I need it every 2nd box... any thoughts?

Image shows what I need repeated on each line (what the code makes right now): http://auction.sabinalcanyon.org/screenshot_05.jpg

CODE:

 <table width="90%" height="166" border="1" align="center" cellpadding="3" cellspacing="3">
  <tr>

<?
if (isset($_GET['id'])) {
    $proid = $_GET['id'];
    $result = mysqli_query($con,"SELECT * FROM auction_products WHERE id = $proid");
} else {
    $result = mysqli_query($con,"SELECT * FROM auction_products");  
}
while($row = mysqli_fetch_array($result))
  {
?>
<td width="50%">
<TABLE BORDER="0" CELLPADDING="3" CELLSPACING="3" align="center">
<TD>
<p>Input : <? echo $row['barcode'] ?><br />
Barcode : Codabar<br />
Check Digit : N.A.<br /><br />

</p>
 </TD>
 <TD>
 |<br />
 |<br />
 |<br />
 |

</TD>
<TD>
Winner #______<br />
Amount $______
</TD>
</TABLE>
<center>
 <div id="barcodecontainer" style="width:5in">
 <div id="barcode<? echo $row['id']?>" ><? echo $row['barcode']?></div>
 </div>
<br />
<script type="text/javascript">
/* <![CDATA[ */
function get_object(id) {
    var object = null;
    if (document.layers) {
        object = document.layers[id];
    } else if (document.all) {
        object = document.all[id];
    } else if (document.getElementById) {
        object = document.getElementById(id);
    }
return object;
}
 get_object("barcode<? echo $row['id']?>").innerHTML=DrawHTMLBarcode_Code128B(get_object("barcode<? echo $row['id']?>").innerHTML,"yes","in",0,2.5,1,"bottom","center","","black","white");
/* ]]> */
</script>
</center>
</td>
<? } ?> 
</tr>
</table>
  • 写回答

1条回答 默认 最新

  • dongqin8652 2014-03-17 18:12
    关注

    The simplefied answer is something like this:

    $i=0;
    while($row = mysqli_fetch_array($result)){
        echo '<td>';
        /* Some code here */
        echo '</td>';
    
        if( $i++ &1==0 ){ echo '</tr><tr>' ;} // Start new line
    }
    

    The magic happends in the if-statement, that is a bitwise comparison, it checkes if the 'ones' in a binary notation equal 0. That only happends to even times of $i.

    1&1 == 1 (1 in binairy is 001)
    2&1 == 0 (2 in binairy is 010)
    3&1 == 1 (3 in binairy is 011)
    4&1 == 0 (4 in binairy is 100)
    5&1 == 1 (5 in binairy is 101)
    etc :)                      ^--- we use this to check
    

    For odd/even this is the fastest method I know (I'd love to hear if there are any faster methods).


    This method works very easy for odd/even, but not for each 3rd, 7th, 21st, whatever. For that we have modulus:

    if( $i%7===0 ){ /* ... */ }
    

    That test how much whole integer is left if you deduct the maximum amount of 7's. Again, examples:

    7 % 2 === 1 (7-2-2-2 = 1, cant subtract another 2)
    9 % 6 === 3 (9-6 = 3, cant subtract another 6)
    9 % 2 === 1 (9-2-2-2-2 = 1, cant subtract another 2)
    123 % 8 === 3 (123 -(15*8)=3, cant subtract another 8)
    

    You can use modules like $i%2 for odd/even, but modulus is an expensive piece of code, only use when you need it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法