按月只有一个标题输出

时间:2013-12-26 04:50:45

标签: php mysql grouping

我一直试图将这个从我当前的输出变为'按月'输出。我准备将这一切都拔掉。我想要做的就是将游戏分组几个月。所以January, feb march等会让每个游戏都在其下面。现在我必须为每个团队执行此操作,并为每个团队创建静态placeholder和新变量。

数据库结构

[id]   [opponent]   [month]   [team]   [notes]   [date_added]   [buyLink]
[time]   [date]

<?php
// Check to see the URL variable is set and that it exists in the database
// Connect to the `MySQL` database
include "../../includes/db_conx.php"; 
//--------------------------------------------------------------------------------------
$sqlcountRam = "SELECT * FROM discounts WHERE team='cyoram' ORDER BY date ASC";
$sql_countRam = mysqli_query($db_conx,$sqlcountRam);
$calCountRam = mysqli_num_rows($sql_countRam); 
//--------------------------------------------------------------------------------------
if ($calCountRam > 0) {
// get all the product details

 $x=0;
while($row = mysqli_fetch_assoc($sql_countRam)){


             $id = $row["id"];
             $opponent = $row["opponent"];
             $team = $row["team"];
             $notes = $row["notes"]; 
             $month = $row["month"];

             $buyLink = $row["buyLink"];

             $time = $row["time"];
             $date = $row["date"];
             $formatted_date = date("l d F Y H:i A", strtotime($date));
             $raw = "$formatted_date";
             $xplod = explode(' ',$raw);
/*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
include "includes/logos.php"; 


/*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
 $classCssRam = "inline-block";
  $x++; 
$classChangeRam = ($x%2 == 0)? 'whiteBackground': 'grayBackground';

 $product_listRam .= "  
   <div class='aNew' style='clear:both;display:<?php echo $classCssRam; ?>'>

<div class='monthSalute'>$month</div>

    <table width='400px' border='0'>
                <tr class='$classChange'>
                    <td rowspan='2' class='date' >$xplod[1]</td>
                    <td class='day'>$xplod[0]</td>
                 <td rowspan='2' class='centerLogo'><div class='containImgPos'><img src='$imgLogo' height='32px' style='position:relative;left:$posImgR;' /></div></td>
                    <td class='city'>$city</td>
                    <td rowspan='2' class='butt_pad'><a href='calendar_edit.php?pid=$id'>edit</a> &bull; <a href='calendar_list.php?deleteid=$id'>delete</a></td>
                </tr>
                <tr class='$classChangeRam'>
                    <td class='time $classChangeRam'>$time</td>
                    <td class='opponent $classChangeRam'>$opponent</td>
              </tr>
    </table>
</div>



";
    }
    }
 else {
    $product_listRam = "";
    $classCssRam = "none";
}

?>

2 个答案:

答案 0 :(得分:1)

在数据库结构中更改为日期格式,并包括startdate和end date以根据这样的月份获取数据:这将为您提供月份数据,希望其有用

 $mon = 1;
 $i=1;
                    $specify = array();
                    while($mon <=12)
                    {
                        $startdate = '2012-'.$mon.'-01';
                        $enddate = '2012-'.$mon.'-31';`
    $result = mysql_query("SELECT * FROM discounts   WHERE `date` BETWEEN '".$startdate."' AND '".$enddate ."'");
$row[i] = mysql_fetch_assoc($result);
$i++;
    }

答案 1 :(得分:0)

请参阅以下代码中$monthDiv的使用。

我也修正了一个错误。你不能在字符串中使用<?php echo ... ?>,你应该只是插入变量。

<?php
while($row = mysqli_fetch_assoc($sql_countRam)){


    $id = $row["id"];
    $opponent = $row["opponent"];
    $team = $row["team"];
    $notes = $row["notes"]; 
    $month = $row["month"];

    $buyLink = $row["buyLink"];

    $time = $row["time"];
    $date = $row["date"];
    $formatted_date = date("l d F Y H:i A", strtotime($date));
    $raw = "$formatted_date";
    $xplod = explode(' ',$raw);
    /* \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ */
    include "includes/logos.php"; 

    /* \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ */
    $classCssRam = "inline-block";
    $x++; 
    $classChangeRam = ($x%2 == 0)? 'whiteBackground': 'grayBackground';

    /* Print month heading for new month */
    if ($last_month != $month) {
        $monthDiv = "<div class='monthSalute'>$month</div>";
        $last_month = $month;
    } else {
        $monthDiv = "";
    }

    $product_listRam .= "  
   <div class='aNew' style='clear:both;display:$classCssRam;'>

   $monthDiv

    <table width='400px' border='0'>
                <tr class='$classChange'>
                    <td rowspan='2' class='date' >$xplod[1]</td>
                    <td class='day'>$xplod[0]</td>
                 <td rowspan='2' class='centerLogo'><div class='containImgPos'><img src='$imgLogo' height='32px' style='position:relative;left:$posImgR;' /></div></td>
                    <td class='city'>$city</td>
                    <td rowspan='2' class='butt_pad'><a href='calendar_edit.php?pid=$id'>edit</a> &bull; <a href='calendar_list.php?deleteid=$id'>delete</a></td>
                </tr>
                <tr class='$classChangeRam'>
                    <td class='time $classChangeRam'>$time</td>
                    <td class='opponent $classChangeRam'>$opponent</td>
              </tr>
    </table>
    </div>
";
}