PHP MySQL Echo计数

时间:2014-04-03 19:00:50

标签: php mysql echo

我试图让按钮每行回显5次,并且对于每一行,它们将被一个元素包裹。

我遇到的问题是在回声时,回声没有发生我的想法。

所需结果(每5 span_1_of_5)包裹<div class="section group"></div>

<div class="section group">   

    <div class="col span_1_of_5">
        <div class='button_epos_a button_epos_a-border'>
            <div class='button_epos_a_content'>
                Button
            </div>
        </div>
    </div>

    <div class="col span_1_of_5">
        <div class='button_epos_a button_epos_a-border'>
            <div class='button_epos_a_content'>
                Button
            </div>
        </div>
    </div>

    <div class="col span_1_of_5">
        <div class='button_epos_a button_epos_a-border'>
            <div class='button_epos_a_content'>
                Button
            </div>
        </div>
    </div>

    <div class="col span_1_of_5">
        <div class='button_epos_a button_epos_a-border'>
            <div class='button_epos_a_content'>
                Button
            </div>
        </div>
    </div>

    <div class="col span_1_of_5">
        <div class='button_epos_a button_epos_a-border'>
            <div class='button_epos_a_content'>
                Button
            </div>
        </div>
    </div>

</div>


<div class="section group">   

    <div class="col span_1_of_5">
        <div class='button_epos_a button_epos_a-border'>
            <div class='button_epos_a_content'>
                Button
            </div>
        </div>
    </div>

    <div class="col span_1_of_5">
        <div class='button_epos_a button_epos_a-border'>
            <div class='button_epos_a_content'>
                Button
            </div>
        </div>
    </div>

    <div class="col span_1_of_5">
        <div class='button_epos_a button_epos_a-border'>
            <div class='button_epos_a_content'>
                Button
            </div>
        </div>
    </div>

    <div class="col span_1_of_5">
        <div class='button_epos_a button_epos_a-border'>
            <div class='button_epos_a_content'>
                Button
            </div>
        </div>
    </div>

    <div class="col span_1_of_5">
        <div class='button_epos_a button_epos_a-border'>
            <div class='button_epos_a_content'>
                Button
            </div>
        </div>
    </div>

</div>

PHP

$itemCount = 0;     

    while ($row5x = mysql_fetch_array($result5x)) { 

    $item_id = $row5x[item_id]; 
    $item_title = $row5x[item_title];

       if ($itemCount==0 || $itemCount%5===0) { echo "<div class='section group'>"; }     
    ?>

    <div class="col span_1_of_5">
        <div class='button_epos_a button_epos_a-border'>
            <div class='button_epos_a_content'>
                <? echo $item_title; ?>
            </div>
        </div>
    </div>

    <?
    if ($itemCount%5===0) { echo "</div>"; }    
    $itemCount++;
    }
    ?>

PHP的输出(不是所需的结果)

<div class='section group'>     

    <div class="col span_1_of_5">
        <div class='button_epos_a button_epos_a-border'>
            <div class='button_epos_a_content'>
                Coffee          
            </div>
        </div>
    </div>

</div>     

<div class="col span_1_of_5">
    <div class='button_epos_a button_epos_a-border'>
        <div class='button_epos_a_content'>
            sdsd            
        </div>
    </div>
</div>


<div class="col span_1_of_5">
    <div class='button_epos_a button_epos_a-border'>
        <div class='button_epos_a_content'>
            cccc            
        </div>
    </div>
</div>


<div class="col span_1_of_5">
    <div class='button_epos_a button_epos_a-border'>
        <div class='button_epos_a_content'>
            BBBB            
        </div>
    </div>
</div>


<div class="col span_1_of_5">
    <div class='button_epos_a button_epos_a-border'>
        <div class='button_epos_a_content'>
            juice           
        </div>
    </div>
</div>

<div class='section group'>     

    <div class="col span_1_of_5">
        <div class='button_epos_a button_epos_a-border'>
            <div class='button_epos_a_content'>
                juice2          
            </div>
        </div>
    </div>

</div>

我不明白为什么会这样,任何启蒙都会很棒。感谢

2 个答案:

答案 0 :(得分:3)

变化:

if ($itemCount==0 || $itemCount%5===0) { echo "<div class='section group'>"; }    

要:

if ($itemCount==0) { echo "<div class='section group'>"; }

同样改变:

<?
    if ($itemCount%5===0) { echo "</div>"; }    
    $itemCount++;
}
?>

要:

<?php
    if ($itemCount===4) { echo "</div>"; $itemCount = 0;} else { $itemCount++;}
}
if ($itemCount !== 0) { echo '</div>';}
?>

我认为这会让事情变得更容易。几个旁注:

  1. 如果这是新代码,最好使用mysqli / Pdo。
  2. 始终使用<?php代替短代码<?
  3. 始终制作索引字符串:$row5x['item_id'];

答案 1 :(得分:0)

$itemCount = 0;     

    while ($row5x = mysql_fetch_array($result5x)) { 

    $item_id = $row5x[item_id]; 
    $item_title = $row5x[item_title];

       if ($itemCount==0 || $itemCount==5) { echo "<div class='section group'>"; }     
    ?>

    <div class="col span_1_of_5">
        <div class='button_epos_a button_epos_a-border'>
            <div class='button_epos_a_content'>
                <? echo $item_title; ?>
            </div>
        </div>
    </div>

    <?
    if ($itemCount==4 || $itemCount==9) { echo "</div>"; }    
    $itemCount++;
    }
    ?>