DATE重复直到while循环获取记录

时间:2011-04-02 09:11:45

标签: php function date

请检查图像,它会直观地解释

http://dubads.com/temp/Untitled-1.jpg

for ($index = 1; $index <= $numdays; $index++) {

    $users =
        "SELECT invoice_date, DAY(invoice_date) as invoiceday, " .
        "sum(total_amount) as fullamount from invoices " .
        "WHERE MONTH(invoice_date)='".$month."' AND YEAR(invoice_date)='".$year.
        "' GROUP BY invoiceday";
    $res = mysql_query($users);
    $row = mysql_num_rows($res);

    while ($fetch = mysql_fetch_array($res)) {

        echo "<tr id='".(($j % 2 == 0) ? 'row3' : 'row4')."'>";

        $explodedate = explode("-", $fetch['invoice_date']);
        $days = $explodedate[2];
        $month1 = $explodedate[1];
        $year1 = $explodedate[0];

        $dates = date("jS", strtotime($fetch['invoice_date']));

        echo "<td style='text-align:left;padding-left:12px'>".$index.
            "</td>";
        if ($index == $dates) {

            echo "<td style='text-align:right;padding-right:12px'>".
                $fetch['fullamount']."</td>";

        } else {

            echo "<td style='text-align:right;padding-right:12px'>--</td>";

        }

        echo "</tr>";
    }
}

$total =
    "SELECT SUM(total_amount) as fulltotal from invoices where MONTH(invoice_date)='".
    $month1."' AND YEAR(invoice_date)='".$year1."';";

$totres = mysql_query($total);
$totfetch = mysql_fetch_assoc($totres);
if ($totfetch['fulltotal'] == "") {
    echo "<tr><td>NOTHING FOUND</td></tr>";
} else {
    ECHO "<TR>";
    echo "<td style='color:#BD0000;padding-left:12px;border:1px solid #ccc;width:20%;height:40px;text-align:left'>TOTAL AMOUNT </td>";
    echo "<td style='color:#BD0000;border:1px solid #ccc;width:20%;height:40px;text-align:right;padding-right:12px'>".$totfetch['fulltotal']."</TD></TR>";

}

echo "</table>";

1 个答案:

答案 0 :(得分:0)

你有一个循环计数$ index。我建议将索引设为0,但这不是你的问题:

你有外环:

for ($index = 1; $index <= $numdays; $index++) {
    ...
}

在其中你有内循环,它正在为每条记录做输出:

while ($fetch = mysql_fetch_array($res)) {
    ...
}

因此,您每天都会输出查询返回的所有记录。

我不能给你任何建议,如何解决这个问题,因为我看不到,你想做什么。也许你可以解释,哪些记录可以显示,何时!

哦,你的html语法错误了! ID必须是唯一的。在你的情况下,你应该使用class not id!

编辑:

啊......好吧,我想我能想象,你的意思!

您希望每天有一行,而对于空白的一天,您还想要一个空行......

实际上这很容易我开始写它......可能需要大约5分钟!

编辑2:

for ($index = 0; $index < $numdays; $index++)
{

    $sql =
        'SELECT sum(total_amount) as fullamount from invoices ' .
        'WHERE MONTH(invoice_date)=\'' . $month . '\' AND YEAR(invoice_date)=\'' . $year .
        '\' And DAY(invoice_date)=\'' . ($index+1) . '\';';

    $res = mysql_query($sql);
    $row = mysql_fetch_array($res))

    echo '<tr class=\'' . (($j % 2 == 0) ? 'row3' : 'row4') . '\'>' . "\n";
    echo '<td style=\'text-align:left;padding-left:12px\'>' . ($index+1) . '</td>' . "\n";

    if($row['fullamount']!=null&&$row['fullamount']!=''&&$row['fullamount']!=0)
    {
        echo '<td style=\'text-align:right;padding-right:12px\'>' . $row['fullamount'] . '</td>' . "\n";
    }
    else
    {
        echo '<td style=\'text-align:right;padding-right:12px\'>--</td>' . "\n";
    }

    echo '</tr>' . "\n";
}

$sql =
    'SELECT SUM(total_amount) as fulltotal from invoices where MONTH(invoice_date)=\'' .
    $month . '\' AND YEAR(invoice_date)=\'' . $year . '\';';

$res = mysql_query($sqö);
$fetch = mysql_fetch_array($res);

if ($fetch['fulltotal'] == '')
{
    echo '<tr><td>NOTHING FOUND</td></tr>';
}
else
{
    echo '<tr>' . "\n";
    echo '<td style=\'color:#BD0000;padding-left:12px;border:1px solid #ccc;width:20%;height:40px;text-align:left\'>TOTAL AMOUNT </td>' . "\n";
    echo '<td style=\'color:#BD0000;border:1px solid #ccc;width:20%;height:40px;text-align:right;padding-right:12px\'>' . $fetch['fulltotal'] . '</td>';
    echo '</tr>' . "\n";
}

echo '</table>';

我希望,这对你有所帮助!祝你好运!