My do while is not doing it

Can someone help me with my do while?
I have a table that pulls data from mysql database. The first column of the table has a link that shows all records that with the same order number and displays the results…well that’s what it should do, but it only shows the first record.

This is code that I used to generate the table.
[php] <?php do { ?>

  <td width="80"><div align="center"><?php echo $row_Orders['OrderDate']; ?></div></td>
  <td width="110"><div align="center"><?php echo $row_Orders['RequiredDate']; ?></div></td>
  <td width="100"><div align="center"><?php echo $row_Orders['fk_TrophyCode']; ?></div></td>
  <td><div align="center"><?php echo $row_Orders['Customer']; ?></div></td>
  <td><div align="center"><?php echo $row_Orders['PartCode']; ?></div></td>
  <td><div align="center"><?php echo $row_Orders['PartDescription']; ?></div></td>
  <td width="60"><div align="center"><?php echo $row_Orders['PartQty']; ?></div></td>
  <td width="15"><div align="center"><?php echo $row_Orders['Freight']; ?></div></td>
  <td width="15"><div align="center"><?php echo $row_Orders['Supplier']; ?></div></td>
</tr>
<?php } while ($row_Orders = mysql_fetch_assoc($Orders)); ?> [/php]

and this is my query
mysql_select_db($database_TrophyMaster, $TrophyMaster);
$query_Orders = sprintf(“SELECT * FROM tbl_OrderDetails WHERE OrderNumber = %s AND tbl_OrderDetails.ClientID = %s GROUP BY Customer, fk_TrophyCode”, GetSQLValueString($colname_Orders, “text”),GetSQLValueString($ID_Orders, “int”));
$Orders = mysql_query($query_Orders, $TrophyMaster) or die(mysql_error());
$row_Orders = mysql_fetch_assoc($Orders);
$totalRows_Orders = mysql_num_rows($Orders);
$count=mysql_num_rows($result);

Reverse it
[php]<?php while ($row_Orders = mysql_fetch_assoc($Orders)) { ?>


<?php echo $row_Orders['OrderDate']; ?>

<?php echo $row_Orders['RequiredDate']; ?>

<?php echo $row_Orders['fk_TrophyCode']; ?>

<?php echo $row_Orders['Customer']; ?>

<?php echo $row_Orders['PartCode']; ?>

<?php echo $row_Orders['PartDescription']; ?>

<?php echo $row_Orders['PartQty']; ?>

<?php echo $row_Orders['Freight']; ?>

<?php echo $row_Orders['Supplier']; ?>


<?php }?>[/php]

Hi richei,
Thanks for the suggestion, however, the result did not produce any records at all. Even the first record did not show up.

Is there something after the while statement that is missing?

Use Richei’s code… “Dont change it”

Comment out this…

[php]$row_Orders = mysql_fetch_assoc($Orders);[/php]

It’s not needed it and it’s making you lose your first record.

The other problem you have is that you’re query is most likely only returning one row.

I pasted the code over my existing code and still again get no records, not even the first one now.

Hi Karma,
I’m not sure what you mean in your comment.
I used Richei’s code as is, but you have added “Comment out this…” and then you have part of the first line of code. Why is this not needed? Sorry I don’t understand.

Can you check my query. I had it working before but now it now longer does a loop through the records.

I reviewed my query and removed the GROUP BY statements and it works now. Not sure why but it is showing all the right records now.
More testing but hopefully I have fixed the problem

Hi Richei,
I have had to go back to my original do…while statement and removed the GROUP BY statement in my query. Without the do…while the first record did not show up. The GROUP BY statement seemed to give me grief so removed it and it all appears to be working as it should. Not sure why but I’m a happy camper now. If you can explain why, I would like to really know and understand why.

Thanks for your input.

GROUP BY combines common records. You can use if statements to filter out categories or dates.
[php]

<?php $prev_date = ''; if(mysql_num_rows($Orders) == 0) { while ($row_Orders = mysql_fetch_assoc($Orders)) { if($prev_date != $row_Orders['OrderDate']) { echo " $row_Orders[OrderDate] "; $prev_date = $row_Orders['OrderDate']; } ?>
<?php echo $row_Orders['RequiredDate']; ?>
<?php echo $row_Orders['fk_TrophyCode']; ?>
<?php echo $row_Orders['Customer']; ?>
<?php echo $row_Orders['PartCode']; ?>
<?php echo $row_Orders['PartDescription']; ?>
<?php echo $row_Orders['PartQty']; ?>
<?php echo $row_Orders['Freight']; ?>
<?php echo $row_Orders['Supplier']; ?>
<?php } } else { echo " No Records Found "; }?>[/php]That does the same thing as the group by, but still gives you the first record, and tells you if any records were found. I know it works, i use this same code in a lot of my websites to do the samething.

Comment out This…

means to change this
[php]$row_Orders = mysql_fetch_assoc($Orders);
[/php]
to

[php]//$row_Orders = mysql_fetch_assoc($Orders);[/php]

So it becomes a comment in PHP and it’s not processed.

Doesn’t the while statement need a test statement? The loop would then read,

[php]while () { ?>


<?php echo $row_Orders['OrderDate']; ?>

<?php echo $row_Orders['RequiredDate']; ?>

<?php echo $row_Orders['fk_TrophyCode']; ?>

<?php echo $row_Orders['Customer']; ?>

<?php echo $row_Orders['PartCode']; ?>

<?php echo $row_Orders['PartDescription']; ?>

<?php echo $row_Orders['PartQty']; ?>

<?php echo $row_Orders['Freight']; ?>

<?php echo $row_Orders['Supplier']; ?>


[/php]

That wasn’t what he was talking about. This is from your post up top
[php]mysql_select_db($database_TrophyMaster, $TrophyMaster);
$query_Orders = sprintf(“SELECT * FROM tbl_OrderDetails WHERE OrderNumber = %s AND tbl_OrderDetails.ClientID = %s GROUP BY Customer, fk_TrophyCode”, GetSQLValueString($colname_Orders, “text”),GetSQLValueString($ID_Orders, “int”));
$Orders = mysql_query($query_Orders, $TrophyMaster) or die(mysql_error());
$row_Orders = mysql_fetch_assoc($Orders);
$totalRows_Orders = mysql_num_rows($Orders);
$count=mysql_num_rows($result);[/php]

$row_Orders = mysql_fetch_assoc($Orders); is the one you need to comment out

Leave the while loop alone.

Thanks guys. All sorted. Thanks for all the help and patience. I’ll call out when I need some more help.
cracker

Sponsor our Newsletter | Privacy Policy | Terms of Service