once I return it within drupals interface (return, return t() ) it shows a single line. If I just echo the 'While' loop (try 4) it will show a blanc page with the 5 rows in an array. I have been searching but cannot find a solution, please take a look at the code and tell me how to handle this. In normal PHP i'd have 5 rows, now only 1. To use the while loop to dump the data into arrays only so you can do a foreach() loop is redundant.I want to fill out a page with information directly from the SQL DB, however within a function a while, foreach or for loop can only contain one line. So, whatever you want to do with that data do it within the while loop. You are already iterating through the results in the while() loop. In the foreach() loop you are only reassigning values to those variable and not doing anything with the values and they are getting overwritten on each iteration - so the foreach() loop is pointless.īut, for the sake of argument, let's say you want to put the values from the DB result set into different array values and then do a foreach(0 to do something with those values. Once you have all the arrays THEN you would exit the while loop and run the foreach() loop.ģ. You would want to run the while() loop to put all the records from the result set into arrays. First, I dont want to use for loop at all. If you want to define arrays for each field in the result set then you would NOT run the foreach() inside the while() loop. You should define the variable as an array $price = array() THEN you would add the values as a new index in the array $price = $row Ģ. Ok, there are a few things wrong with that:ġ. While loop is used to execute a block of code as long as the set condition is made to be false. $price, of all the price values from the database result set. The foreach loop is used to loop through arrays. If I understand you correctly you want to loop through the database records and create an array, e.g. I think I understand what you are trying to and it makes even less sense. $order1 = $order //The $order var is a string, not an array $id1 = $id //The $order var is a string, not an array I need the while loop to continue looping. Because the foreach loop does leaves the block when values have been iterated. $price1 = $price //The $price var is a string, not an array The foreach loop is used when we need to execute a code blocks multiple times for each element of an array. convert foreach into while loop Ask Question Asked 12 years ago Modified 12 years ago Viewed 4k times 2 Hi guys i need to convert a foreach loop into a while loop. $category1 = $category //The $category var is a string, not an array This call works just fine if it's outside a foreach loop. The code inside the foreach() loop is trying to define variables based upon an index of the variables defined at the beginning of the while loop Using do while loop inside a foreach Ask Question Asked 4 years, 10 months ago Modified 4 years, 9 months ago Viewed 552 times 0 I am doing this call to soccer api inside a foreach loop to return each competition matches. I really have no idea what you are trying to accomplish. But, what really makes no sense is how you are trying to define the variables in the foreach() loop. Then you have a foreach() loop which also defines some variables - one of which is overwriting the previous variable "$product". The first part of the code in the while() loop is defining some variables. I think the problem is not the foreach()/while() but in what you are doing with the. This is used frequently in many scripts where a common block of code to be executed. You simply put a foreach() within a while() loop. For loop in PHP is used to iterate through a section in an application.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |