simon551 Posted May 22, 2007 Share Posted May 22, 2007 Hi, I'm trying to execute this foreach loop on a query. I have tested the query outside of php and it returns 2 records. But with the code I am using here, it is returning the first record, but twice. Not sure what I'm doing wrong. </tr> <?php //start new Show if recordset not empty if ($totalRows_rsERDets_Amex2 > 0) { ?> <?php //start repeat region for do { ?> <tr> <td><?php echo $row_rsERDets_Amex2['fdate']; ?></td> <td><?php echo $row_rsERDets_Amex2['ItemName']; ?></td> <td><?php echo $row_rsERDets_Amex2['vendor']; ?></td> <td><?php echo $row_rsERDets_Amex2['Description']; ?></td> <td><?php echo $row_rsERDets_Amex2['Project']; ?></td> <td><?php echo $row_rsERDets_Amex2['amtForeign']; ?></td> <td><?php echo $row_rsERDets_Amex2['amtUS']; ?></td> <td><?php echo $row_rsERDets_Amex2['PERS']; ?></td> <td>edit</td> <td>split</td> </tr> <?php //end repeat } while ($totalRows_rsERDets_Amex2 = mysql_fetch_assoc($rsERDets_Amex2)); ?> <?php } // End Show if recordset not empty statement ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/52541-solved-strange-loop-results/ Share on other sites More sharing options...
Mr. R Posted May 22, 2007 Share Posted May 22, 2007 where is the foreach bit? for a if statement, like you have, you dont need the "do" bit. Quote Link to comment https://forums.phpfreaks.com/topic/52541-solved-strange-loop-results/#findComment-259256 Share on other sites More sharing options...
simon551 Posted May 22, 2007 Author Share Posted May 22, 2007 I took out the 'do' part and now it returns one row, but there should be 2... (note: edited my original post to remove the 'foreach' description, thanks) Quote Link to comment https://forums.phpfreaks.com/topic/52541-solved-strange-loop-results/#findComment-259275 Share on other sites More sharing options...
kenrbnsn Posted May 22, 2007 Share Posted May 22, 2007 Change the loop to a "while" statement: <?php while ($totalRows_rsERDets_Amex2 = mysql_fetch_assoc($rsERDets_Amex2)) { ?> <tr> <td><?php echo $row_rsERDets_Amex2['fdate']; ?></td> <td><?php echo $row_rsERDets_Amex2['ItemName']; ?></td> <td><?php echo $row_rsERDets_Amex2['vendor']; ?></td> <td><?php echo $row_rsERDets_Amex2['Description']; ?></td> <td><?php echo $row_rsERDets_Amex2['Project']; ?></td> <td><?php echo $row_rsERDets_Amex2['amtForeign']; ?></td> <td><?php echo $row_rsERDets_Amex2['amtUS']; ?></td> <td><?php echo $row_rsERDets_Amex2['PERS']; ?></td> <td>edit</td> <td>split</td> </tr> <?php //end repeat } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/52541-solved-strange-loop-results/#findComment-259277 Share on other sites More sharing options...
per1os Posted May 22, 2007 Share Posted May 22, 2007 </tr> <?php //start new Show if recordset not empty if ($totalRows_rsERDets_Amex2 > 0) { ?> <?php //start repeat region for while ($totalRows_rsERDets_Amex2 = mysql_fetch_assoc($rsERDets_Amex2)) { ?> <tr> <td><?php echo $totalRows_rsERDets_Amex2['fdate']; ?></td> <td><?php echo $totalRows_rsERDets_Amex2['ItemName']; ?></td> <td><?php echo $totalRows_rsERDets_Amex2['vendor']; ?></td> <td><?php echo $totalRows_rsERDets_Amex2['Description']; ?></td> <td><?php echo $totalRows_rsERDets_Amex2['Project']; ?></td> <td><?php echo $totalRows_rsERDets_Amex2['amtForeign']; ?></td> <td><?php echo $totalRows_rsERDets_Amex2['amtUS']; ?></td> <td><?php echo $totalRows_rsERDets_Amex2['PERS']; ?></td> <td>edit</td> <td>split</td> </tr> <?php //end repeat } ?> <?php } // End Show if recordset not empty statement ?> </table> Remember to call the RIGHT variable names. Quote Link to comment https://forums.phpfreaks.com/topic/52541-solved-strange-loop-results/#findComment-259278 Share on other sites More sharing options...
simon551 Posted May 22, 2007 Author Share Posted May 22, 2007 Frost, That seems to be working. Thank you for the help! Ken, I tried your suggestion, too and it returned the same row twice (?) Thanks all! -s Quote Link to comment https://forums.phpfreaks.com/topic/52541-solved-strange-loop-results/#findComment-259288 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.