【问题标题】:Attempting to group events by date in PHP尝试在 PHP 中按日期对事件进行分组
【发布时间】:2012-06-14 01:24:53
【问题描述】:

我正在从数据库中获取事件列表,并尝试将类似日期与一个标题相关联。目前我正在根据要求使用<dl><dt><dd>等。这可能会改变,但我认为这不会影响我的问题的整体影响。

基本上我试图通过查询获取 5 个最新事件:

global $wpdb;

$sql = "SELECT e.*, c.*, ese.start_time, ese.end_time
        FROM wp_events_detail e
        JOIN wp_events_category_rel r ON r.event_id = e.id
        JOIN wp_events_category_detail c ON c.id = r.cat_id
        LEFT JOIN wp_events_start_end ese ON ese.event_id= e.id 
        WHERE e.is_active =  'Y'
        AND c.category_identifier = 'main'
        ORDER BY e.start_date ASC
        LIMIT 0, 5";
$results = $wpdb->get_results($sql);

然后我通过foreach() 循环它们:

if(count($results) > 0) {

    echo '<div class="calendar-list">';

    foreach($results as $event) {

        $event_id = $event->id;
        $event_name = $event->event_name;
        $start_date = $event->start_date;
        $start_time = $event->start_time;

        $start_time = strtotime($start_time);
        $start_time = date('g:ia', $start_time);

        $the_time =  strtotime($start_date);

        $the_day = date('d', $the_time);
        $the_month = date('M', $the_time);

        echo '<dl class="calendar-day first-child">';
        echo '<dt>';
        echo '<p class="the-month">' . $the_month . '</p>';
        echo '<p class="the-day">' . $the_day . '</p>';
        echo '</dt>';
        echo '<dd>';
        echo '<h4><a href="' . $event_id . '">' . $event_name . '</a></h4>';
        echo '<h5><span class="time">' . $start_time . '</span></h5>';
        echo '</dd>';
        echo '</dl>';

    }

    echo '</div>';
}

我的主要问题是我需要找到一种将日期相互关联的方法。例如,6 月 27 日。如果有两个日期,我希望它看起来像:

<div class="calendar-list">
    <dl class="calendar">
        <dt>
            <p class="the-month">JUN</p>
            <p class="the-day">27</p>
        </dt>
        <dd>
            <h4><a href="#">Title</a></h4>
                        <h5><span class="time">Time</span></h5>
        </dd>
        <dd>
            <h4><a href="#">Title</a></h4>
                        <h5><span class="time">Time</span></h5>
        </dd>
    </dl>
    <dl class="calendar">
        <dt>
            <p class="the-month">JUN</p>
            <p class="the-day">28</p>
        </dt>
        <dd>
            <h4><a href="#">Title</a></h4>
                        <h5><span class="time">Time</span></h5>
        </dd>
        </dl>
</div>

我很不确定如何实现这一点。我一直在尝试设置和取消设置一些变量,但无法达到理想的结果。

如果有人能指出我该做什么的正确方向,那将不胜感激。

谢谢!

特雷

【问题讨论】:

    标签: php mysql date grouping


    【解决方案1】:

    基本上,您必须在循环末尾的每一行中记住$the_time,并将其与当前的$the_time 进行比较,以决定是否必须回显一个新的&lt;dt&gt;。不过,每天都有自己的&lt;dl&gt; 让事情变得有点复杂。这是一个丑陋的解决方案:

    // Init empty previous time
    $previous_time = null;
    // Open first definition list
    echo '<dl class="calendar-day first-child">';
    foreach($results as $event) {
    
        $event_id = $event->id;
        $event_name = $event->event_name;
        $start_date = $event->start_date;
        $start_time = strtotime($event->start_time);
        $the_time = $start_time;
        $the_day = date('d', $start_time);
        $the_month = date('M', $start_time);
        $start_time = date('g:ia', $start_time);
    
        // Date changed
        if($the_time != previous_time) {
            // Unless first iteration, close previous list
            if(!empty($previous_time)) {
                echo '</dl>';
            }
            // Open new list
            echo '<dl class="calendar-day first-child">'
            // Show definition term for the group of events
            echo '<dt>';
            echo '<p class="the-month">' . $the_month . '</p>';
            echo '<p class="the-day">' . $the_day . '</p>';
            echo '</dt>';
        }
    
        // Display current event
        echo '<dd>';
        echo '<h4><a href="' . $event_id . '">' . $event_name . '</a></h4>';
        echo '<h5><span class="time">' . $start_time . '</span></h5>';
        echo '</dd>';
    
        // Remember date from previous row
        previous_time = $the_time;
    }
    // Close last definition list
    echo '</dl>';
    

    【讨论】:

    • 效果非常好。我不得不调整一些东西以使其适用于我的内容,但这个想法得到了很好的传达。非常感谢。
    【解决方案2】:
    <?php
    //***************************************
    /// Read the readme.txt file before using ///////
    // This is downloaded from www.plus2net.com //
    /// You can distribute this code with the link to www.plus2net.com ///
    //  Please don't  remove the link to www.plus2net.com ///
    // This is for your learning only not for commercial use. ///////
    //The author is not responsible for any type of loss or problem or damage on using   this script.//
    /// You can use it at your own risk. /////
    //*****************************************
    
    $dbservertype='mysql';
    $servername='ur host';
    // username and password to log onto db server
    $dbusername='ur user';
    $dbpassword='ur pwd';
    // name of database
    $dbname='ur db';
    
    ////////////////////////////////////////
    ////// DONOT EDIT BELOW  /////////
    ///////////////////////////////////////
    connecttodb($servername,$dbname,$dbusername,$dbpassword);
    function connecttodb($servername,$dbname,$dbuser,$dbpassword)
    {
    global $link;
    $link=mysql_connect ("$servername","$dbuser","$dbpassword");
    if(!$link){die("Could not connect to MySQL");}
    mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
    }
    //////// End of connecting to database ////////
    
    // Init empty previous time
     $previous_time = null;
    
     // Open first definition list
         echo '<dl class="calendar-day first-child">';
        $q1="SELECT * FROM msg WHERE sendto = '1' || sender = '1' ORDER BY time desc";
     $n1=mysql_query($q1);
        while($nt1=mysql_fetch_array($n1)){
    
     $start_time = strtotime($nt1['time']); //change to string
        $the_time = date('j m', $start_time); //select wat u need i.e day and month
    
         //$the_time = $nt1['time']);  //for troublshooting db
    
       // Date changed
    if($the_time != $previous_time) {
        // Unless first iteration, close previous list
        if(!empty($previous_time)) {
            echo '</dl>';
        }
        // Open new list
        echo '<dl class="calendar-day first-child">';
        // Show definition term for the group of events
       echo '<h4>'.$nt1['time'].'</h4>';
     }
    
     // Display current event
    echo '<dd>';
    echo $nt1['message'] .' '. $nt1['time'];
    echo '</dd>';
    
    // Remember date from previous row
    $previous_time = $the_time;
     }
     // Close last definition list
     echo '</dl>';
    
    ?>
    
    
     </body>
    
           </html>
    

    兄弟们,有了这段编辑过的代码,你可以按时间对任何东西进行分组

    【讨论】:

      猜你喜欢
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 2013-03-17
      • 2013-12-06
      • 1970-01-01
      • 2023-02-07
      相关资源
      最近更新 更多