【问题标题】:Alternating CSS Style with multiple Mysql Results使用多个 Mysql 结果交替 CSS 样式
【发布时间】:2009-08-27 02:43:19
【问题描述】:

我有一个网站,有人在其位置搜索 x 产品,然后该网站会返回一个结果列表。

if(isset($_POST['zip'])){
$qry="SELECT business_id FROM ".TBL_BUSINESS." WHERE zip LIKE '%".$_POST['zip']."%'";
$rs = mysql_query($qry);

$rec = array();

while(($row = mysql_fetch_array($rs)) !== FALSE ){
    $rec[] = $row[0];
}

if(!empty($rec[0])){

    echo "Products for this location<br/>";

    foreach ($rec as $result)
    {
        $bid = $result;
        $qry2 = "SELECT * FROM products WHERE business_id = '".$bid."'";
        $rs2 = mysql_query($qry2);
        $rec2 = mysql_fetch_array($rs2);
        ?>
            <div class="norm">
                <img src="admin/product/img/<?php echo $rec2['image']; ?>" height="40" width="40" />
                <h3><a href="product.php?id=<?php echo $rec2['id']; ?>"><?echo $rec2['name'];?>&nbsp;&nbsp;<?php echo $rec2['prodvalue']?></a></h3>
                <div class="prodlistMeta">
                    <a class='view' href="product.php?id=<?php echo $rec2['id']; ?>">View Product</a>
                    <a class="print" href="#">Print</a>
                </div>
            </div>
        <?php
    }
}
else
{
    echo "No Product is added for this location";
}

} ?>

&lt;div class="norm"&gt;&lt;div class="alt"&gt; 交替使用的最佳方式是什么?

【问题讨论】:

    标签: php mysql css


    【解决方案1】:

    保留一个计数器并使用它的值模 2 来确定类应该是“norm”还是“alt”。

     $rec2 = mysql_fetch_array($rs2);
     $count++;
       ?>
          <div class="<?php echo($count%2?"norm":"alt"); ?>">
    

    【讨论】:

      【解决方案2】:

      我倾向于使用这样的东西:

      $row = 0;
      foreach ($rec as $result) {
        $class = $row++ & 1 == 1 ? 'alt' : 'norm';
        ...
        echo <<<END
      <div class="$class">
      ...
      END;
      }
      

      您可以使用花括号在字符串中执行表达式,但我通常不喜欢嵌入这种逻辑。它(恕我直言)有点难以阅读。另外,以上内容使您有机会更轻松地输出它以进行调试等。

      【讨论】:

        【解决方案3】:
        if(some expression)
        {
            $class="norm";
        }
        else
        {
            $class="alt";
        }
        ?>
        <div class="<?php echo $class;?>">
        

        【讨论】:

          【解决方案4】:

          在您的输出循环上设置一个计数器。每当计数器为偶数时,将类设置为正常,否则将其设置为交替。

          【讨论】:

            【解决方案5】:

            为什么不使用模数和行 ID?更简单,不需要不必要的变量

            foreach ($rec as $rid => $result)
            {
                $bid = $result;
                $qry2 = "SELECT * FROM products WHERE business_id = '".$bid."'";
                $rs2 = mysql_query($qry2);
                $rec2 = mysql_fetch_array($rs2);
                ?>
                        <div class="<?=($rid % 2 == 0) ? 'norm' : 'alt' ?>">
                                <img src="admin/product/img/<?php echo $rec2['image']; ?>" height="40" width="40" />
                                <h3><a href="product.php?id=<?php echo $rec2['id']; ?>"><?echo $rec2['name'];?>  <?php echo $rec2['prodvalue']?></a></h3>
                                <div class="prodlistMeta">
                                        <a class='view' href="product.php?id=<?php echo $rec2['id']; ?>">View Product</a>
                                        <a class="print" href="#">Print</a>
                                </div>
                        </div>
                <?php
            }
            

            【讨论】:

              【解决方案6】:

              【讨论】:

                猜你喜欢
                • 2018-05-12
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-06-12
                • 1970-01-01
                • 2019-05-10
                • 1970-01-01
                相关资源
                最近更新 更多