Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
600 views
in Technique[技术] by (71.8m points)

php - How to pass a JSON to checkbox

Json

{"segment":"Class I-V Tuition,Class VI-VIII Tuition"
 ,"Board":["cbse"]
 ,"Class I-V":["Allsubject"]
 ,"Class VI-VIII":["cbse","cse/Ise"]
 ,"Class VI-VIII Subject":["Allsubject","Science"]
 ,"Class XI-X":null
 ,"Class XI-X Subject":null
 ,"Class XI-XII":null
 ,"Class XI-XII Subject":null
 ,"Languagesubject":null
 ,"engineering":null
 ,"Diploma":null
 ,"Dancesubject":null
 ,"Degree Course":null
 ,"Degree Subject":null
}

Here  key:Board
      value:cbse
      key:Class I-V
      value:Allsubject like wise json pattern stored in classconducted column

My questions are:

  1. How to retrieve the json string?
  2. I have tried like this.

    $sqledit = mysql_query("select * from  tinfo where tsname='" .$_SESSION['tutorname'] ."'");
    $row = mysql_fetch_array($sqledit); 
    foreach (json_decode($row['classconducted'], true) as $key => $val) {
       if($key!='' && $val!='') {
          echo '<span class="details">'.$key.'</span>'.': '."<br/><br/>";
          if (is_array($val))
            echo implode(',', $val) . "<br/><br/>";
          else
            echo $val . "<br/>";
        }
    }
    

Now I want to edit the page. So using foreach I separate the values. Here I have decode the json values. now the values are coming . I want to know how to pass the json values to my form. Here is my form. I have tried like this, I don't know what is wrong in my code.

I want ouptut like this: check this is this correct

<input type="checkbox" name="board[]"  value="cbse" <?php if($val[0]=='cbse'){echo "checked";}?>>CBSE
<input type="checkbox" name="board[]"  value="cse/Ise" <?php if($val[1]=='ICSE/ISE'){echo "checked";}?>>ICSE/ISE.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You do the same as in your other question, but parse the JSON instead of exploding it.

$cats = json_decode($row['classconducted'], true);

<input type="checkbox" name="segment[]" value="Class I-V Tuition" <?php if(in_array('Class I-V Tuition', $cats)) { echo "checked"; }?>> Class I-V Tuition

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...