0

我正在尝试从 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
4

2 回答 2

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>
于 2013-10-22T09:44:00.313 回答
1

You have not moved the row pointer onto the next row between the 2 DIV's so $sdeals contains the same result in both DIV's.

<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>
于 2013-10-22T09:43:36.687 回答