将php循环连续划分为两个div

时间:2016-06-26 10:14:31

标签: php html

我有以下代码:

<section class="sppb-section " style="margin:0px;padding:70px 0px;background-color:#e5e5e5;">

<?php 
$c_pk = $rows['id'];
  $sql_i = mysql_query("SELECT * FROM `purchasify_items` WHERE cat_id = '$c_pk' ORDER BY id DESC LIMIT 0 , 6");
     if(mysql_num_rows($sql_i)>0) { 
        while($row = mysql_fetch_array($sql_i)) 
        {

?>

<div class="sppb-container"><div class="sppb-row"><div class="sppb-col-sm-12">
.......
</div></div></div>

</section>

它显示如下

<section class="sppb-section " style="margin:0px;padding:70px 0px;background-color:#e5e5e5;"><div class="sppb-container"><div class="sppb-row"><div class="sppb-col-sm-12">
.......
</div></div></div></section>

<section class="sppb-section " style="margin:0px;padding:70px 0px;background-color:#e5e5e5;"><div class="sppb-container"><div class="sppb-row"><div class="sppb-col-sm-12">
.......
</div></div></div></section>

我希望代码有两个不同背景颜色的部分,如下所示:

<section class="sppb-section " style="margin:0px;padding:70px 0px;background-color:#e5e5e5;"><div class="sppb-container"><div class="sppb-row"><div class="sppb-col-sm-12">
.......
</div></div></div></section>

<section class="sppb-section " style="margin:0px;padding:70px 0px;background-color:#ffffff;"><div class="sppb-container"><div class="sppb-row"><div class="sppb-col-sm-12">
.......
</div></div></div></section>

<section class="sppb-section " style="margin:0px;padding:70px 0px;background-color:#e5e5e5;"><div class="sppb-container"><div class="sppb-row"><div class="sppb-col-sm-12">
.......
</div></div></div></section>

<section class="sppb-section " style="margin:0px;padding:70px 0px;background-color:#ffffff;"><div class="sppb-container"><div class="sppb-row"><div class="sppb-col-sm-12">
.......
</div></div></div></section>

有人可以帮忙吗?我不擅长php

2 个答案:

答案 0 :(得分:0)

您可以使用变量来跟踪行号并根据该颜色为您的部分着色。例如:

$c_pk = $rows['id'];
$sql_i = mysql_query("SELECT * FROM `purchasify_items` WHERE cat_id = '$c_pk' ORDER BY id DESC LIMIT 0 , 6");
    if(mysql_num_rows($sql_i)>0) {
        // Initialize the Variable
        $rowNumber = 0;
        while($row = mysql_fetch_array($sql_i)) 
        {
            // other code

            // Update the current row number
            $rowNumber++;

然后使用行号,您可以通过检查它们是偶数还是奇数(例如if(is_float($rowNumber / 2)))或模数运算符来为您的部分着色:if($rowNumber%2 == 0)

答案 1 :(得分:0)

我使用的逻辑是,如果$ i甚至显示第一部分,则显示第二部分,依此类推

  public ActionResult Html(string strUrl) 
    {
        string xhr;
        xhr = GetHTTPRequest(strUrl, "GET");
        // make pdf file fron xhr
        return View("Index");

    }

    [ChildActionOnly]
    public static string GetHTTPRequest(string RequestUrl, string RequestMethod)
    {
        try
        {
            HttpWebRequest r = (HttpWebRequest)WebRequest.Create(RequestUrl);
            r.Method = RequestMethod;
            HttpWebResponse res = (HttpWebResponse)r.GetResponse();
            Stream sr = res.GetResponseStream();
            StreamReader sre = new StreamReader(sr);

            string s = sre.ReadToEnd();

            return s;
        }
        catch (Exception)
        {

            return string.Empty;
        }
    }