Welcome Guest, Not a member yet? Register   Sign In
Check if exist in array (all in foreach)
#1

Hi!
I have little problem.
I need to check every row in another array, and if exist - do something.

This is my code:
PHP Code:
<?php $Sekcje explode','$Detail->SectionsPrefered ); ?>
                        <?php foreach ($SectionsList as $SectionOption): ?>
                                <div class="checkbox">
                                    <?php if ( in_array$SectionOption->Title $Sekcje ) ): ?>
                                        *
                                    <?php endif ?>
                                    <label>
                                        <input type="checkbox" value="<?php echo $SectionOption->Title?>">
                                        <?php echo $SectionOption->Title?>
                                    </label>
                                </div>
                        <?php endforeach ?>

It's display only one '*', but must display for 3 rows (Section1, Section2, Section3)

This is the array $Sekcje:
PHP Code:
array(3) { 
       [
0]=> string(8"Section1" 
       
[1]=> string(9" Section2" 
       
[2]=> string(9" Section3" 


Where is the problem?
Reply
#2

(This post was last modified: 12-11-2015, 10:51 AM by pdthinh.)

(12-11-2015, 09:27 AM)vertisan Wrote: Hi!
I have little problem.
I need to check every row in another array, and if exist - do something.

This is my code:
PHP Code:
                       <?php $Sekcje explode','$Detail->SectionsPrefered ); ?>
                        <?php foreach ($SectionsList as $SectionOption): ?>
                                <div class="checkbox">
                                    <?php if ( in_array$SectionOption->Title $Sekcje ) ): ?>
                                        *
                                    <?php endif ?>
                                    <label>
                                        <input type="checkbox" value="<?php echo $SectionOption->Title?>">
                                        <?php echo $SectionOption->Title?>
                                    </label>
                                </div>
                        <?php endforeach ?>

It's display only one '*', but must display for 3 rows (Section1, Section2, Section3)

This is the array $Sekcje:
PHP Code:
array(3) { 
 
      [0]=> string(8"Section1" 
 
      [1]=> string(9" Section2" 
 
      [2]=> string(9" Section3" 


Where is the problem?

Was it display only one '*' corresponding to the "Section1"? What's your $SectionsList value?
Reply
#3

Do a var_dump on your details array and make sure you are getting more then just one value.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

Var_dump $SectionsList:
PHP Code:
array(5) {
 
 [0]=>
 
 object(stdClass)#33 (3) {
 
   ["ID"]=>
 
   string(1"1"
 
   ["Title"]=>
 
   string(8"Section1"
 
   ["Description"]=>
 
   string(13"Opis sekcji 1"
 
 }
 
 [1]=>
 
 object(stdClass)#34 (3) {
 
   ["ID"]=>
 
   string(1"2"
 
   ["Title"]=>
 
   string(8"Section2"
 
   ["Description"]=>
 
   string(13"Opis sekcji 2"
 
 }
 
 [2]=>
 
 object(stdClass)#35 (3) {
 
   ["ID"]=>
 
   string(1"3"
 
   ["Title"]=>
 
   string(8"Section3"
 
   ["Description"]=>
 
   string(13"Opis sekcji 3"
 
 }
 
 [3]=>
 
 object(stdClass)#36 (3) {
 
   ["ID"]=>
 
   string(1"4"
 
   ["Title"]=>
 
   string(8"Section4"
 
   ["Description"]=>
 
   string(13"Opis sekcji 4"
 
 }
 
 [4]=>
 
 object(stdClass)#37 (3) {
 
   ["ID"]=>
 
   string(1"5"
 
   ["Title"]=>
 
   string(8"Section5"
 
   ["Description"]=>
 
   string(13"Opis sekcji 5"
 
 }


Var_dump $Sekcje:
PHP Code:
array(3) {
 
 [0]=>
 
 string(8"Section1"
 
 [1]=>
 
 string(9" Section2"
 
 [2]=>
 
 string(9" Section3"

Reply
#5

(12-11-2015, 12:42 PM)vertisan Wrote: Var_dump $SectionsList:
PHP Code:
array(5) {
 
 [0]=>
 
 object(stdClass)#33 (3) {
 
   ["ID"]=>
 
   string(1"1"
 
   ["Title"]=>
 
   string(8"Section1"
 
   ["Description"]=>
 
   string(13"Opis sekcji 1"
 
 }
 
 [1]=>
 
 object(stdClass)#34 (3) {
 
   ["ID"]=>
 
   string(1"2"
 
   ["Title"]=>
 
   string(8"Section2"
 
   ["Description"]=>
 
   string(13"Opis sekcji 2"
 
 }
 
 [2]=>
 
 object(stdClass)#35 (3) {
 
   ["ID"]=>
 
   string(1"3"
 
   ["Title"]=>
 
   string(8"Section3"
 
   ["Description"]=>
 
   string(13"Opis sekcji 3"
 
 }
 
 [3]=>
 
 object(stdClass)#36 (3) {
 
   ["ID"]=>
 
   string(1"4"
 
   ["Title"]=>
 
   string(8"Section4"
 
   ["Description"]=>
 
   string(13"Opis sekcji 4"
 
 }
 
 [4]=>
 
 object(stdClass)#37 (3) {
 
   ["ID"]=>
 
   string(1"5"
 
   ["Title"]=>
 
   string(8"Section5"
 
   ["Description"]=>
 
   string(13"Opis sekcji 5"
 
 }


Var_dump $Sekcje:
PHP Code:
array(3) {
 
 [0]=>
 
 string(8"Section1"
 
 [1]=>
 
 string(9" Section2"
 
 [2]=>
 
 string(9" Section3"


It's because you have white-spaces in $Sekcje elements. Trim it can solve the problem.

PHP Code:
<?php $Sekcje array_map('trim'explode','$Detail->SectionsPrefered )); ?>
Reply




Theme © iAndrew 2016 - Forum software by © MyBB