开发者

DATE repeating until the while loop fetches records

Please check the image, it will explain visually

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'] == "") {
    ech开发者_如何学运维o "<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>";


You have got one loop counting $index. I would recomment to make indexes 0-based, but that is not your problem:

You have got the outer loop:

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

And in it you have the inner loop, wich is doing the output for each record:

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

So for each day you output all the records returned by your query.

I can not give you any suggestions, how to solve this, because I can not see, what you want to do. Maybe you could explain, which records schould be shown, when!

Oh and you have got an error in your html syntax! An ID has to be unique. In your case you should use class not id!

Edit:

Ahh.. Ok i think I can imagine, what you mean!

You want to have one line per day and for an empty day you want also an empty line...

Actually that is fairly easy I start writing it... Might take about 5 minutes!

Edit No.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>';

I hope, this helps you! good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜