0

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> 
3
  • After the first do while loop you have iterated over the entire result set so the second so while loop contains nothing.
    – shannonman
    Jan 18, 2013 at 10:22
  • Are you sure you want 2 equal columns? or you just want to show your data in 2 columns? Jan 18, 2013 at 10:26
  • @YourCommonSense: sorry that was a typo. second set of columns are actually for $row_dates['item'];
    – rain
    Jan 19, 2013 at 1:40

2 Answers 2

2
<?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>
2
  • Worked great, thanks. Now what if there is no WHERE uploadedby='Gerry Dellosa' condition and I want uploadedby to echo vertically under the Name column while retaining the horizontal data values for each of the uploadedby values.
    – rain
    Jan 19, 2013 at 4:26
  • set the foreach for the line, not column, like <table> <tr>..<td>name</td> <td>date</td>..</tr> .. foreach( .. ) { <tr>..<td>$rec[name]</td> <td>$rec[date]</td>..</tr> } Jan 21, 2013 at 10:10
-1

reset fetch loop before second row

mysql_data_seek($result,0);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.