0

I am trying to loop through MySQL results and return same dates & agents in a separate . The table containing this data has the number of tickets each agent works on a specific day. Each group of dates should be separated by a blank row in the table. Below is the code I am working with. I believe I have to do a foreach, but not sure how to get it working.

Here is a screenshot to a final table layout I am looking to achieve.

enter image description here

    if($res && mysql_num_rows($res))
{
    while($row = mysql_fetch_array($res))
        {
if ($row['total_updated'] > 0) {
print   "<tr>";
print       "<td align=center>" . $row['date_added'] . "</td>";
print       "<td nowrap>" . $row['agent'] . "</td>";
print       "<td nowrap>" . $row['agent_location'] . "</td>";
print       "<td align=center>" . number_format($row['total_updated']) . "</td>";
print       "<td align=center>" . number_format($row['total_notes']) . "</td>";
print       "<td align=center>" . number_format($row['total_closed']) . "</td>";
print       "<td align=center>" . number_format($row['ticket_app1_updated']) . "</td>";
print       "<td align=center>" . number_format($row['ticket_app1_notes']) . "</td>";
print       "<td align=center>" . number_format($row['ticket_app1_closed']) . "</td>";
print       "<td align=center>" . number_format($row['ticket_app2_updated']) . "</td>";
print       "<td align=center>" . number_format($row['ticket_app2_notes']) . "</td>";
print       "<td align=center>" . number_format($row['ticket_app2_closed']) . "</td>";
print       "<td align=center>" . number_format($row['ticket_app3_updated']) . "</td>";
print       "<td align=center>" . number_format($row['ticket_app3_notes']) . "</td>";
print       "<td align=center>" . number_format($row['ticket_app3_closed']) . "</td>";
print   "</tr>";
    }
        }
}
print   "</table>";
4
  • so... slap some <tr> and </tr> tags in there. We have no idea what your expected layout is, so that's the best you're going to get. Commented Dec 4, 2013 at 14:44
  • yeah^^ pls add more infos about what should be the endresult and a question Commented Dec 4, 2013 at 14:46
  • Added a screenshot of how I'd like the final table output to look. Sorry for the lack of detail. Commented Dec 4, 2013 at 14:54
  • 2
    Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. Commented Dec 4, 2013 at 14:55

1 Answer 1

1

If you sort your results by date_added you don't need any foreach, just compare previous date with current one:

while($row = mysql_fetch_array($res))
{
    if (!isset($lastdate))
        $lastdate = $row['date_added'];
    if ($lastdate != $row['date_added']) {
         ?><tr><td colspan="15">---blank line---</td></tr> <?php
    }
    //paste all of your prints here
    $lastdate = $row['date_added'];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.