Jump to content

Archived

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

garry27

for loop inside another for loop

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
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



Share this post


Link to post
Share on other sites
Still not 100% sure, how bout this:

[code]
foreach ( $chkbox_arr as $KEY => $column ) {
      $sql_arr[$KEY] = ' AND ' . $COLUMN . "='y' ";
}
[/code]

Share this post


Link to post
Share on other sites
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.  ???

Share this post


Link to post
Share on other sites
i've sorted it now, thanks. it was a lot simpler problem than i thought.

[code]sort ($chkbox_arr);
  $result = count($chkbox_arr);
  echo $result;
  for ( $i = 0; $i <$result; $i++ )
  $chkbox_arr[$i]="AND $chkbox_arr[$i]='y'";[/code]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

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.