How to echo error from foreach loop?

Php foreach loop displaying result only when the loop is completed?

  • Hi there, I have an array i need to loop though and echo each element. the way I want it to work is as follows: I would like the code to echo each element of the array as it gets to it. ( it will be more complicated than that since I will execute an operation on the array) I want to monitor the result for each element and the only way I can think of is to echo the result for each one. trouble is when i use foreach loop what happens is that all the results are printed at once when the script is done executing. thank you in advance for your help.

  • Answer:

    Salaam for showing all ______________ $sql = "SELECT * FROM table1;"; $rs = mysql_query($sql); while($row = mysql_fetch_array($rs)){ echo $row['name']; } for one by one ______________ $sql = "SELECT * FROM table1 where itemID=".$_GET['id']."; $rs = mysql_query($sql); while($row = mysql_fetch_array($rs)){ echo $row['name']; } Good Luck

Salmane S at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

You need to flush the output buffer, if you want the page sent to you sequentially, rather than after execution. You may also want to sleep() if you want it slow enough to see. i.e. ob_flush() http://php.net/manual/en/function.ob-flush.php

Rob

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.