php - testing database results to set a checkbox as checked -


If a data appears in my database, then I have a checked attribute output on my checkbox Want to do / P>

I tried to do in_array () , but it did not work due to values ​​(see database)

I do not know how I will output them Well

PHP Code:

  $ general_list = ""; ($ Line ['f_general'] == 'bar,') {$ general_list. = "& Lt; label class = \" bg-danger \ "& gt; & lt; input type = \" radio \ "name = \" normal [] \ "value = \" bar, \ "checked = \" Checked \ "/> bar & lt; / label & gt;"; } And {$ general_list. = "& Lt; labeled & gt; input type = \" radio \ "name = \" normal [] \ "value = \" bar, \ "/> bar, & lt; / label & gt; ; " ; } If ($ line ['f_general'] == 'restaurant,') {$ general_list = "& Lt; labeled> input type = \" radio \ "name = \" normal [] \ "value = \" restaurant, \ "check = \" checked \ "/> restaurant, Lt; / label & gt; "; } And {$ general_list. = "& Lt; Labels & gt; & lt; input type = \" radio \ "name = \" normal [] \ "value = \" restaurant, \ "/> restaurant, & lt; / label & gt; ; " ; } If {$ row ['f_general'] == 'coffee shop,') {$ general_list. = "& Lt; labeled & gt; input type = \" radio \ "name = \" normal [] \ "value = \" coffee shop, \ "check = \" checked \ "coffee Shop, & lt; / label & gt; "; } And {$ general_list. = "& Lt; Labels & gt; & lt; input type = \" radio \ "name = \" normal [] \ "value = \" coffee shop, \ "/> coffee shop, & lt; / label & Gt; "; }   

My database values ​​are stored in this way:

  bars, restaurants, coffee shops, concierge,   

HTML code:

   Bars, & lt; / Label & gt; & Lt; Label & gt; & Lt; Input type = "checkbox" name = "normal []" value = "restaurant", & gt; Restaurants, & lt; / Label & gt; & Lt; Label & gt; & Lt; Input type = "checkbox" name = "normal []" value = "coffee shop," /> coffee shop, & lt; / Label & gt; & Lt; Label & gt; & Lt; Input type = "checkbox" name = "normal []" value = "concierge," /> Concierge, & lt; / Labels & gt; & Lt; Label & gt; & Lt; Input type = "checkbox" name = "normal []" value = "business center," /> Business Center, & lt; / Labels & gt; & Lt; Label & gt; & Lt; Input type = "checkbox" name = "normal []" value = "salon," /> Salon, & lt; / Label & gt;    

It is unclear how your data is coming from the database, but if you Storing it as a comma separated string: No. There are better ways to do this - use different database rows for each unit in $ row ['f_general'] , i.e.

  f_general bar restaurant coffee shop (Etc)   

You can do a query and put all the items in a code in the field f_general is a comma-separated string.

Since you have a string, parse it to create an array:

  $ db_row = explosion (",", $ row ['f_general'] );   

You can make your code much more compact and easy to maintain by using the following code:

  # set an array in all checkboxes : $ Item = array ('bar', 'restaurant', 'coffee shop', 'concierge', 'business center', 'salon'); $ General_list = ''; # Go through each item in the array foreach ($ item as $ i) ## See if the item is in the data from the database (in_array ($ i, $ db_row)) {$ general_list. = '& Lt; Label class = "bg-danger" & gt; & Lt; Input type = "radio" name = "normal []" value = "'. $ I." "Checked" "check" /> . $ I. '& Lt; / Labels & gt; '; } And {$ general_list. = '& Lt; Label & gt; & Lt; Input type = "radio" name = "normal []" value = "'. $ I." "/ & Gt; '. I. I.' & lt; / labels & gt; ';}}    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -