Jump to content

for loop inside another for loop


garry27

Recommended Posts

i have this code:

[code]for ( $i = 0; $i <10; $i++ )
$chkbox_arr[$i]; 
    for ( $ii = 0; $ii <10; $ii++ )
      $sql_arr[$ii] = ' AND '.$chkbox_arr[$i]."='y' "; [/code]

what I'm trying to do is create a new array which adds ' AND '.$chkbox_arr[$i]."='y' "; for each value in $chkbox_array. but all i get is:

AND ='y' AND ='y' AND ='y' AND ='y' AND ='y' AND ='y' AND ='y' AND ='y' AND ='y' AND ='y'

pleasecan someone show me how to fix this.

thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/25242-for-loop-inside-another-for-loop/
Share on other sites

I think you need to be more specific in what you are trying to accomplish. I've tried deciphering what you may want from your text and your code and don't have a clue. Give an example of the resulting array and values you want and we can help.
ok. i'm trying to adjust each value in the $chkbox_arr array so that it adds

" AND *followed by the value* ='y' "

this array im trying to store in $sql_ arr

currently each value in  $chkbox_arr is a string word which relate to a column on a mysql table

i will then use the implode function to add the new values together in a single string so i can use in a where clasuse in mysql.

the problem now is that the only result i'm getting is:

AND ='y' AND ='y' AND ='y' AND ='y' AND ='y' AND ='y' AND ='y' AND ='y' AND ='y' AND ='y'

..regardless of what's in the $chkbox_arr array



i'm trying to do something like this but using for so it keeps track of the index:

[code]foreach ($chkbox_arr as &$value)
$value = "AND $chkbox_arr 'y'"; [/code]

if i use the code above all i get is AND Array 'y' for each value that is returned. instead of the actual value contained in the array it just says Array. i has a similar problem with this when i tried to create $ckbox_arr array using a a foreach loop.  ???
I thought you might be moking more difficult than it needed to be - I was lost. Here is a simpler method, that doesn't require to to sort first.

[code]foreach ( $chkbox_arr as $key => $value ) {
   $chkbox_arr[$key]="AND $value ='y'";
}[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.