Using PHP to display date-based MySQL info

Discussion in 'PHP' started by adamjthompson, Feb 5, 2009.

  1. #1
    I have a database of orders - the below code is supposed to give me a list of the months that have orders. Each month links to a page showing all the orders for that month. The table also includes sales totals for each month.

    It is only showing the current month, though. What is wrong? Thanks!

    <?php
    $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
    mysql_select_db($db);
    $result = mysql_query("SELECT DISTINCT year FROM orders ORDER BY year DESC");
    while($row = mysql_fetch_array($result))
      {
    $year = $row['year'];
    $result = mysql_query("SELECT DISTINCT month FROM orders WHERE year='$year' ORDER BY month DESC");
    while($monthrow = mysql_fetch_array($result))
      {
    $month = $monthrow['month'];
    $result = mysql_query("SELECT * FROM orders WHERE year='$year' && month='$month' ORDER BY orderid DESC");
    while($orderrow = mysql_fetch_array($result))
      {
    $monthlyordertotal = $monthlyordertotal + $orderrow['subtotal'];
      }
    echo "<tr><td><a href=month.php?month=".$monthrow['month']."&year=$year>".$monthrow['month']."  - $year</a></td><td>$$monthlyordertotal</td></tr>";
      }
      }
    ?>
    PHP:
     
    adamjthompson, Feb 5, 2009 IP
  2. steelaz

    steelaz Peon

    Messages:
    47
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    From your code it looks like it is fetching months for a single year. So if it is fetching 2009, perhaps it has only data for January.
     
    steelaz, Feb 5, 2009 IP
  3. adamjthompson

    adamjthompson Well-Known Member

    Messages:
    1,242
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    125
    #3
    No, it has data for December, January, & February.
     
    adamjthompson, Feb 5, 2009 IP