水平Div内部While循环显示相同的产品PHP

时间:2013-10-22 09:30:56

标签: php html while-loop

我正在尝试从while循环创建一个水平div,但结果是我在列div中获得了相同的产品。这是我的代码。

<div class="ui-grid-a">
<?php $query = mysql_query("select * from table"); 
while($sdeals = mysql_fetch_array($query){ ?>

<div class="ui-block-a">
                <a href="">
                <div  style="height:120px;   text-align: center;">
                    <div style="font-size:60%;margin-top:5px;"><?php echo $sdeals['productname']; ?></div>
                     <img style="margin-top:5px;" height="67" width=50" src="../thumbnails/<?php echo $sdeals['imageg1']; ?>" >
                      <?php if(!empty($sdeals['coupon_code'])) { ?>
                      <div style="font-size:60%;margin-top:5px;"> Use Coupon: <?php echo $sdeals['coupon_code']; ?></div>

                      <?php } ?>
                        <div style="font-size:60%;margin-top:5px;">Rs.<?php echo $sdeals['price']; ?></div>

                </div>
                </a>

<div class="ui-block-b">
                <a href="">
                <div  style="height:120px;   text-align: center;">
                    <div style="font-size:60%;margin-top:5px;"><?php echo $sdeals['productname']; ?></div>
                     <img style="margin-top:5px;" height="67" width=50" src="../thumbnails/<?php echo $sdeals['imageg1']; ?>" >
                      <?php if(!empty($sdeals['coupon_code'])) { ?>
                      <div style="font-size:60%;margin-top:5px;"> Use Coupon: <?php echo $sdeals['coupon_code']; ?></div>

                      <?php } ?>
                        <div style="font-size:60%;margin-top:5px;">Rs.<?php echo $sdeals['price']; ?></div>

                </div>
                </a>

            </div>


<?php } ?>
</div>

我得到的输出是

block-a block-b
  pro1   pro1
  pro2   pro2
  pro3   pro3
  pro4   pro4

正确的输出应该是

  block-a block-b
   pro1    pro2
   pro3    pro4
   pro5    pro6
   pro7    pro8
   pro9    pro10

2 个答案:

答案 0 :(得分:2)

<div class="ui-grid-a">
<?php
$num=0;
$class1 = "";

$query = mysql_query("select * from table"); 
while($sdeals = mysql_fetch_array($query)
{ 

    if($num%2==0) 
    { $class1 = 'class="ui-block-a"';}
    else
    { $class1 = 'class="ui-block-b"';}
?>
    <div <?php echo $class1;?>>
        <a href="">
        <div  style="height:120px;   text-align: center;">
            <div style="font-size:60%;margin-top:5px;"><?php echo $sdeals['productname']; ?></div>
             <img style="margin-top:5px;" height="67" width=50" src="../thumbnails/<?php echo $sdeals['imageg1']; ?>" >
              <?php if(!empty($sdeals['coupon_code'])) { ?>
              <div style="font-size:60%;margin-top:5px;"> Use Coupon: <?php echo $sdeals['coupon_code']; ?></div>

              <?php } ?>
                <div style="font-size:60%;margin-top:5px;">Rs.<?php echo $sdeals['price']; ?></div>

        </div>
        </a>
    </div>
<?php 

$num++;
} //while loop closing
?>
</div>

答案 1 :(得分:1)

您尚未将行指针移动到2 DIV之间的下一行,因此$sdeals在两个DIV中都包含相同的结果。

<div class="ui-grid-a">

<?
    php $query = mysql_query("select * from table"); 
    $blockA = true;

    while($sdeals = mysql_fetch_array($query){ 

        if ( $blockA ) {
            echo '<div class="ui-block-a">';
        } else {
            echo '<div class="ui-block-b">';
        }
        $blockA = ! $blockA;
?>
    <a href="">
        <div style="height:120px;text-align:center;">
            <div style="font-size:60%;margin-top:5px;"><?php echo $sdeals['productname']; ?></div>
            <img style="margin-top:5px;" height="67" width=50" src="../thumbnails/<?php echo $sdeals['imageg1']; ?>" >
<?php 
   if(!empty($sdeals['coupon_code'])) { ?>

                  <div style="font-size:60%;margin-top:5px;"> Use Coupon: <?php echo $sdeals['coupon_code']; ?></div>

<?php } ?>
            <div style="font-size:60%;margin-top:5px;">Rs.<?php echo $sdeals['price']; ?></div>
        </div>
    </a>
    </div>
<?php 
    }
?>
</div>