1

I am getting array to string conversion error , and also here i am able to get first value from database

                    <label for="#" class="col-sm-2 control-label">test</label>
                    <div class="col-sm-6">
                    <?php 
                    $c_box1=$c_box2=$c_box3=$c_box4=$c_box5='';
                     // $chk_box_data is which is from DB
                    foreach($selectdata as $r)
                    {
                      $list=explode(',',$r->facilities);
                      echo $list;

                      if($list=='1'){$c_box1='checked';}
                      if($list=='2'){$c_box2='checked';}
                      if($list=='3'){$c_box3='checked';}
                      if($list=='4'){$c_box4='checked';}
                      if($list=='5'){$c_box5='checked';}
                    }
                    ?>
                      <div class="col-sm-6 checkbox"><input id="checkbox5" type="checkbox" name="test[]" value="1" <?php echo $c_box1;?>><label for="checkbox5" >check</label></div>
                      <div class="col-sm-6 checkbox"><input id="checkbox1" type="checkbox" name="test[]" value="2" <?php echo $c_box2;?>><label for="checkbox1">check</label></div>
                      <div class="col-sm-6 checkbox"><input id="checkbox2" type="checkbox" name="test[]" value="3" <?php echo $c_box3;?>><label for="checkbox2">check</label></div>
                      <div class="col-sm-6 checkbox"><input id="checkbox2" type="checkbox" name="test[]" value="4" <?php echo $c_box4;?>><label for="checkbox3">check</label></div>
                      <div class="col-sm-6 checkbox"><input id="checkbox2" type="checkbox" name="test[]" value="5" <?php echo $c_box5;?>><label for="checkbox4">check</label></div>
                    </div>
3
  • 2
    $list=explode(',',$r->facilities); this line will give you an array. and you are treating as a string if($list=='1'){$c_box1='checked';}. That's why you are getting error Commented Sep 21, 2017 at 7:47
  • whats the output of var_dump($r->facilities); - post it pls Commented Sep 21, 2017 at 8:39
  • string(3) "1,2" Commented Sep 21, 2017 at 8:42

2 Answers 2

1

The return value of explode will be an array always. So after the line $list=explode(',',$r->facilities); , the $list will be an array which is then taken in the line if($list=='1'){$c_box1='checked';} as a string for comparison, that returned the error.

Sign up to request clarification or add additional context in comments.

Comments

0

$list=explode(',',$r->facilities); this line will give you an array. and you are treating as a string if($list=='1'){$c_box1='checked';}. That's why you are getting error

you can use if(in_array("1", $list)){$c_box1='checked';}, if(in_array("2", $list)){$c_box2='checked';} and so on...

<?php
    $c_box1=$c_box2=$c_box3=$c_box4=$c_box5='';
    $list=explode(',',"1,3,11,12,5");
    if(in_array("1", $list)){$c_box1='checked';}
    if(in_array("2", $list)){$c_box2='checked';}
    if(in_array("3", $list)){$c_box3='checked';}
    if(in_array("4", $list)){$c_box4='checked';}
    if(in_array("5", $list)){$c_box5='checked';}
?>


<div class="col-sm-6 checkbox"><input id="checkbox5" type="checkbox" name="test[]" value="1" <?php echo $c_box1;?>><label for="checkbox5" >check</label></div>
<div class="col-sm-6 checkbox"><input id="checkbox1" type="checkbox" name="test[]" value="2" <?php echo $c_box2;?>><label for="checkbox1">check</label></div>
<div class="col-sm-6 checkbox"><input id="checkbox2" type="checkbox" name="test[]" value="3" <?php echo $c_box3;?>><label for="checkbox2">check</label></div>
<div class="col-sm-6 checkbox"><input id="checkbox2" type="checkbox" name="test[]" value="4" <?php echo $c_box4;?>><label for="checkbox3">check</label></div>
<div class="col-sm-6 checkbox"><input id="checkbox2" type="checkbox" name="test[]" value="5" <?php echo $c_box5;?>><label for="checkbox4">check</label></div>

9 Comments

if(in_array("1", $list)){$c_box1='checked';} if(in_array("2", $list)){$c_box1='checked';} if(in_array("3", $list)){$c_box1='checked';} if(in_array("4", $list)){$c_box1='checked';} if(in_array("5", $list)){$c_box1='checked';}
is there any problem problem with explode syntax.
are you getting values in $r->facilities?
while i printing that value it shows only " array "
what do get output in that?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.