I'm creating a formulary with a lot of things into it and i've a simple problem with my foreach.
Here is my code :
foreach($aDonnes['Grilles'][$iGrilleId] as $iCptGrille){
$sHtml .='<tr><td class="grille">'.($sGrilleLib=$aDonnes['Grilles'][$iCptGrille][1]).'</tr></td>';
$aDonnes['Domaines']=$oRolling->find_domaines($iCptGrille);
if ($iCptGrille < $aDonnes['Grilles'].length) {
$iCptGrille++;
}
// Several nested foreach
I've tried without the if condition, or anything and it still doesn't increment when I check the logs, It just do the same thing again and again.
If you've any questions feel free to ask.
NB : $iCptGrille is my int that doesn't increment and aDonnes is an array that has a lot of data into it, and in our case this is for each ID of my Table called "Grille" that i want to loop.
If you are checking a 2D array, I think the foreach input should be:
foreach($aDonnes['Grilles'] as $iCptGrille){...}
Also the counter name has the same name as the object item $iCptGrille, which doesn't make sense.
Also you can try initializing the counter by 0 before foreach block and remove 'if' statement if it's unnecessary.
For furthur investigations, you can use some kind of flag (for example echo 'entered'; ) inside the foreach block, to make sure it enters into the foreach block.
I hope it helps.
As Answers_Seeker said, "It's a matter of scope : using the as keyword makes a copy of your actual array element".
Related
I am trying to loop while inside for loop but the while loop is only iterating in the first iteration of for loop after I only get whatever is outside while loop to work but while stay here my code any help will be great . Thanks
if(mysqli_affected_rows($obj->conn)>0){
foreach ($dateGroup as $value) {
while($fetchdata=mysqli_fetch_array($selectdata))
{
echo 'executing while <br>';
}
echo "<b>executing foreach</b><br>";
}
}
First of all - you should reeeeeally try spliting your questions into more understandable fashion - using more sentences, commas, question marks, etc. It could be really hard to understand your problem.
Secondly - you should actually ask a question, not just ask for help with a code that might contain who-knows-what specifics.
In your case the expression inside the while loop parenthesis fetches the data once and there is no more fetching of data afterwards. This is why your while loops executes just once. You should rethink your design here, code proposal, in my opinion are not relevant.
I'm fairly certain the answer is no, but is it possible to insert something into an array during a foreach loop? Ideally at the very spot you are at in the array during the loop.
For example:
foreach($stock->StockData as &$stock) {
if($dateTime < $stock['DateTime']) {
// INSERT NEW RECORD AT THIS SPOT IN THE ARRAY
}
}
As I say, I'm fairly certain the answer is no, but rather than build a new array, I just thought I'd ask.
I stand corrected!
http://docstore.mik.ua/orelly/webprog/php/ch05_07.htm
It apparently is just fine to do this in PHP.
According to the reference, PHP operates on a copy of the array when you start a foreach iterator, meaning that the iterator will not be corrupted by operations on the original array within the body of the foreach!
You really don't want to mutate an object is being iterated on. It will break your iterator/loop and could possibly crash the script/program by accessing or changing memory that you don't have access to anymore, possibly because array size has reduced.
The funny thing is that I can't find a good Explanation for this question.
Is there any disadvantage when I don't use a "while-loop" to fetch a mysql result?
Everywhere I see this line of code:
while ($row = mysql_fetch_array($result)) {
// do something with $row
}
I'm not sure that I have ever seen code that used a foreach-loop to fetch mysql results.
So is there anything wrong with other Loops or is the while-loop the only Loop that is easy and simple to use for it?
I guess there is no performance problem for regular uses with while-loops or foreach-loops with 100-1000 resultsets, isn't it?
I guess the answer is that :
"Foreach" is used when we want to loop through a code for each element in an array.(It handles each element inan array)
"For" is used when we want to loop through a block of code a specified number of times.
"While" : is used to loop through a block of code as long as a special condition is true .
I hope everything is clear for more info you may visit this website : http://www.tutorialspoint.com/php/php_loop_types.htm
someone wrote this code.
foreach ($node->taxonomy as $term) {
$tids[] = 't.tid = %d';
$args[] = $term->tid;
}
how he knows that in foreach "$node->taxonomy" is an array? and when i loop it,
foreach ($node->taxonomy as $term) {
}
the output that i get will be the $term's value. i don't know how it is change into the 't.tid = %d' and $term->tid. thank you.
In Drupal-related code, a $node is almost always an object produced by the node_load() function. Since every module has the opportunity to add its own properties to this object, it's very hard to find a central documentation of these properties.
By experience and by variable inspection, seasoned Drupal developers know that when set $node->taxonomy is always an array of term object (as returned by the taxonomy_get_term() function) indexed by their respective ids (named tids, for Term ID). This array is set by the taxonomy_nodeapi() function when $op == 'load' and is produced by the taxonomy_get_terms() function.
The question give little information but we can guess that the loop is meant to build two arrays used to generate a database query that filter on the tid column matching those of the $node object. Because the terms' data is already stored in the items of $node->taxonomy, let's hope that this query is not used to re-load the terms to display some of their name and/or description. Collecting 't.tid = %d' is probably a bad idea, the query would be better build with a single "tid in (". db_placeholder($args) .")" WHERE clause after collecting all the tids in $args.
The question is very unclear. All Items under the node object are arrays. You can check it yourself bu using:
print_r($node);
die;
Or using any PHP debugger.
for the foreach, It is very simple foreach... I don't understand what is the problem with that.
t.tid is simply an SQL query. %d is a placeholder for $args[], which consists of $term->tid. It's like this structure: PDO connections.
I'm trying to look through an array of records (staff members), in this loop, I call a function which returns another array of records (appointments for each staff member).
foreach($staffmembers as $staffmember)
{
$staffmember['appointments'] = get_staffmember_appointments_for_day($staffmember);
// print_r($staffmember['appointments'] works fine
}
This is working OK, however, later on in the script, I need to loop through the records again, this time making use of the appointment arrays, however they are unavailable.
foreach ($staffmembers as $staffmember)
{
//do some other stuff
//print_r($staffmember['appointments'] no longer does anything
}
Normally, I would perform the function from the first loop, within the second, however this loop is already nested within two others, which would cause the same sql query to be run 168 times.
Can anyone suggest a workaround?
Any advice would be greatly appreciated.
Thanks
foreach iterates over a copy of the array. If you want to change the value, you need to reference it:
foreach($staffmembers as &$staffmember) // <-- note the &
{
$staffmember['appointments'] = get_staffmember_appointments_for_day($staffmember);
// print_r($staffmember['appointments'] works fine
}
From the documentation:
Note: Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it.
and
As of PHP 5, you can easily modify array's elements by preceding $value with &. This will assign reference instead of copying the value.