Jump to content


Photo

for loop inside another for loop


  • Please log in to reply
6 replies to this topic

#1 garry27

garry27
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 26 October 2006 - 10:06 PM

i have this code:

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

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



#2 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,892 posts
  • LocationCanada

Posted 26 October 2006 - 10:31 PM

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.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#3 garry27

garry27
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 26 October 2006 - 10:43 PM

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





#4 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,892 posts
  • LocationCanada

Posted 27 October 2006 - 12:00 AM

Still not 100% sure, how bout this:

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

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#5 garry27

garry27
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 27 October 2006 - 12:45 AM

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

foreach ($chkbox_arr as &$value)
 $value = "AND $chkbox_arr 'y'";


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.  ???


#6 garry27

garry27
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 27 October 2006 - 01:25 AM

i've sorted it now, thanks. it was a lot simpler problem than i thought.

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


#7 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,892 posts
  • LocationCanada

Posted 27 October 2006 - 01:47 PM

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.

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

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users