php while loops


#1

I have a small problem with my page.
I have been using dreamweaver to construct the SQL statements and quickly build forms.
First I’ll tell you what it is I’m trying to accomplish. We keep a database of site performance with each individual cell site having it’s own entry for every day. I want to do a month to month comparison, using the months the user selects.
Example
The user selects August and September from the list, and the totals for each month would appear on a seperate line (actually I would like to put this in a table.)
I am very close to acheiving my goal.
Here is what I am having problems with. When I select multiple dates (for the sake of this example lets say I chose 5 months), I get data results for the first month repeated 5 times. I have looked over the code for a while and I can’t figure out why it’s doing what its’ doing. Here is the code:

<?php require_once('../Connections/Reports.php'); ?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../styles/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<form name="form1" method="GET" action="">
  <select name="range[]" size="12" multiple class="formButton" id="range[]">
    <option value="-01-%">January</option>
    <option value="-02-%">February</option>
    <option value="-03-%">March</option>
    <option value="-04-%">April</option>
    <option value="-05-%">May</option>
    <option value="-06-%">June</option>
    <option value="-07-%">July</option>
    <option value="-08-%">August</option>
    <option value="-09-%">September</option>
    <option value="-10-%">October</option>
    <option value="-11-%">November</option>
    <option value="-12-%">December</option>
    </select>  
  <input type="hidden" name="year" value="2004">
  <br>
  <input type="submit" name="Submit" value="Submit">
</form>
<table border="0" cellspacing="0">
  <tr>
    <td>Drops</td>
    <td>Attempts</td>
    <td>Blocks</td>
    <td>Volume</td>
    <td>Handoff Attempts</td>
    <td>Handoff Completions</td>
    <td>Intersystem Handoff Attempts</td>
    <td>Intersystem Handoff Completions</td>
  </tr>
  <?
if (isset ($_GET['range'])){
$range = $_GET['range'];
$year = $_GET['year'];
mysql_select_db($database_Reports, $Reports);
$i=0;
$total = count($_GET['range']);
$date = $year . $range[$i];
while($i < $total){
$query_months = "SELECT DISTINCT SUM(GSM.Drops) AS Drops, SUM(GSM.Attempts) AS Attempts, SUM(GSM.Blocks) AS Blocks, SUM(GSM.Volume) AS Volume, SUM(GSM.HO_att) AS 'Handoff Attempts', SUM(GSM.HO_comp) AS 'Handoff Completions', SUM(GSM.intersystem_HO_att) AS 'Intersystem Handoff Attempts', SUM(GSM.intersystem_HO_comp) AS 'Intersystem Handoff Completions' FROM GSM WHERE GSM.Date LIKE '$date'";
$months = mysql_query($query_months, $Reports) or die(mysql_error());
$row_months = mysql_fetch_assoc($months);
$totalRows_months = mysql_num_rows($months);

echo ?>
  <tr>
    <td><?php echo $row_months['Drops']; ?></td>
    <td><?php echo $row_months['Attempts']; ?></td>
    <td><?php echo $row_months['Blocks']; ?></td>
    <td><?php echo $row_months['Volume']; ?></td>
    <td><?php echo $row_months['Handoff Attempts']; ?></td>
    <td><?php echo $row_months['Handoff Completions']; ?></td>
    <td><?php echo $row_months['Intersystem Handoff Attempts']; ?></td>
    <td><?php echo $row_months['Intersystem Handoff Completions']; ?></td>
  </tr>
  <?php 
mysql_free_result($months);  
$i++;
}

}
echo $date;

?>You have selected <? echo $total; ?> months to view.

</table>
</body>
</html>

Any and all comments are welcome.
Thank you in advance.


#2

bump


#3

you can use a for loop. How about something like this:

$row_months = mysql_fetch_assoc($months); // get first record
$totalRows_months = mysql_num_rows($months);
for ($i = 0; $i <$totalRows_months; $i++)
{
   echo ?>
  <tr>
    <td><?php echo $row_months['Drops']; ?></td>
    <td><?php echo $row_months['Attempts']; ?></td>
    <td><?php echo $row_months['Blocks']; ?></td>
    <td><?php echo $row_months['Volume']; ?></td>
    <td><?php echo $row_months['Handoff Attempts']; ?></td>
    <td><?php echo $row_months['Handoff Completions']; ?></td>
    <td><?php echo $row_months['Intersystem Handoff Attempts']; ?></td>
    <td><?php echo $row_months['Intersystem Handoff Completions']; ?></td>
  </tr>
  <?php 
   $row_months = mysql_fetch_assoc($months);  // get next record
}