rain
rain

Reputation: 95

Dynamic table columns with PHP

I want to make a table with 2 dynamic columns using one MySQL query. I was able to make the first row of columns but the second row is just blank. Any ideas? Here is my current code:

<?php
$query_dates ="SELECT uploadedby, count(item) as item, date_format(uploaddate, '%m-%d-%y') as date FROM `imsexport` WHERE uploadedby='Gerry Dellosa' and uploaddate between '2012-12-25' and '2013-01-15' GROUP BY date ORDER BY uploaddate ASC";
$result = mysql_query($query_dates,$prepress) or die(mysql_error());
$row_dates = mysql_fetch_assoc($result);
?>

<html>
<head>
</head>
<body>
<table width="200" border="1">
  <tr>
    <td>NAME</td>
<?php do{?>
    <td><?php echo $row_dates['date']?></td>
<?php } while ($row_dates = mysql_fetch_assoc($result)); ?>
 </tr>
 <tr>//This is the second row set of dynamic columns
   <?php do{?>
    <td><?php echo $row_dates['date']?></td>
   <?php } while ($row_dates = mysql_fetch_assoc($result)); ?>  
 </tr>
</table>
</body>
</html> 

Upvotes: 0

Views: 3146

Answers (2)

Norton
Norton

Reputation: 340

reset fetch loop before second row

mysql_data_seek($result,0);

Upvotes: -1

vectorialpx
vectorialpx

Reputation: 586

<?php
$query_dates ="SELECT uploadedby, count(item) as item, date_format(uploaddate, '%m-%d-%y') as date FROM `imsexport` WHERE uploadedby='Gerry Dellosa' and uploaddate between '2012-12-25' and '2013-01-15' GROUP BY date ORDER BY uploaddate ASC";
$result = mysql_query($query_dates,$prepress) or die(mysql_error());
while($row_dates = mysql_fetch_assoc($result)) {
    $record[] = $row_dates;
}
?>

<html>
<head>
</head>
<body>
<table width="200" border="1">
  <tr>
    <td>NAME</td>
<?php foreach($record as $rec ) { ?>
    <td><?php echo $rec['date'] ?></td>
<?php } ?>
 </tr>
 <tr>//This is the second row set of dynamic columns
   <?php foreach($record as $rec ) { ?>
    <td><?php echo $rec['date'] ?></td>
   <?php } ?>
 </tr>
</table>
</body>
</html>

Upvotes: 2

Related Questions