如何结合我约会的这个逻辑

时间:2014-03-16 16:28:21

标签: php mysql sql loops

我的思考时间很糟糕... IDK该怎么做?差不多一天仍然无法弄明白......这是我的展示,我想要像这样......

http://i.imgur.com/lNhagUD.jpg

我的实施

<?php
        $userId = (int) (!isset($_GET['id'])) ? 1 : $_GET['id'];
        $imagesResult = $db->getWhere("SELECT * FROM photo WHERE userID = {$userId} ORDER BY `when` DESC");
        echo '<pre>';
        print_r($imagesResult);
        echo '</pre>';
        foreach ($imagesResult as $key => $value) {

            if(strtotime(date('j M Y', $value['when'])) == strtotime(date('j M Y'))) {
                echo "<div class='item'><h3>Today</h3></div>";

            } else {
                echo "<div class='item'><h3>".date('j M Y', $value['when'])."</h3></div>";
            }
            echo "
                <div class='item'>
                    <a href='photo.php?photoid={$value['id']}'><img src='img/picture/{$value['userId']}/{$value['imageLocation']}' height='180' /></a>
                </div>";
        }
    ?>

我不知道从哪里开始...会像那样返回......当日期超出循环时很难做到。我不知道放在哪里......请帮我解决这个问题......给我一点提示来做这件事......

$imageResult的样本返回

Array
(
    [0] => Array
        (
            [id] => 35
            [userId] => 1
            [albumId] => 
            [imageLocation] => Background.png
            [description] => some description
            [tags] => 
            [when] => 1394965560
        )

    [1] => Array
        (
            [id] => 36
            [userId] => 1
            [albumId] => 
            [imageLocation] => Background1.png
            [description] => some description
            [tags] => 
            [when] => 1394965560
        )
)

2 个答案:

答案 0 :(得分:2)

首先,我建议您不要直接回显结果,而应构建最终数组GROUPED BY日期(包括今天)。

像这样:

     $actualImageResults = array();
     foreach ($imagesResult as $Image) {
         $subarr = (date('j M Y', $Image['when']) == date('d M Y')) ? "today" : date("j-m-Y");
         if(!is_array($actualImageResults[$subarr])) $actualImageResults[$subarr] = array();
         array_push($actualImageResults[$subarr], $Image);
    }

然后var_dump($actualImageResults)查看数据是否正确分组......

在此之后你可以继续回声结果:

foreach($actualImageResults as $stamp => $images) {
    echo "<strong>{$stamp}</strong>";
    foreach($images as $image) {
       echo $image["imageLocation"];
       /.../

    }
}

这只是一个粗略的想法,你需要根据你的需要进行必要的调整......但这是我从你发布的问题和图片链接中理解的。它是这样的 - &gt;制作一个最终的数组,从回显中将日期作为其键,并将所有图像分组在它们之间。


的问题:

我猜你正在一个完整的error_reporting / debugging / etc环境中工作......关闭文件启动时的错误报告通知。类似的东西:

    <?php
    error_reporting(E_ALL ^ E_NOTICE);
/.../

答案 1 :(得分:1)

尝试使用标志变量:

    $userId = (int) (!isset($_GET['id'])) ? 1 : $_GET['id'];
    $imagesResult = $db->getWhere("SELECT * FROM photo WHERE userID = {$userId} ORDER BY `when` DESC");
    echo '<pre>';
    print_r($imagesResult);
    echo '</pre>';
    $lastPrintedDateFlag = ""; // Flag variable
    foreach ($imagesResult as $key => $value) {
        if ($lastPrintedDateFlag != $value['when']) {
            $lastPrintedDateFlag = $value['when'];
            if(strtotime(date('j M Y', $value['when'])) == strtotime(date('j M Y'))) {
                echo "<div class='item'><h3>Today</h3></div>";

            } else {
                echo "<div class='item'><h3>".date('j M Y', $value['when'])."</h3></div>";
            }
        }
        echo "
            <div class='item'>
                <a href='photo.php?photoid={$value['id']}'><img src='img/picture/{$value['userId']}/{$value['imageLocation']}' height='180' /></a>
            </div>";
    }

但这只是存档你想要的一种棘手的方法。我建议准备一个2维数组,其中图像按日期分组,然后您将更清楚地打印结果:

数组示例:

Array
(
    [16-03-14] => Array
        (
            [1] => Array
                  (
                   [id] => 35
                   [userId] => 1
                   [albumId] => 
                   [imageLocation] => Background.png
                   [description] => some description
                   [tags] => 
                   [when] => 1394965560
               ),
            [2] => Array
                  (
                   [id] => 35
                   [userId] => 1
                   [albumId] => 
                   [imageLocation] => Background.png
                   [description] => some description
                   [tags] => 
                   [when] => 1394965560
               )
        )
    [17-03-14] => Array
        (
            [1] => Array
                  (
                   [id] => 35
                   [userId] => 1
                   [albumId] => 
                   [imageLocation] => Background.png
                   [description] => fuck this Shit
                   [tags] => 
                   [when] => 1394965560
               )
        )
)