kokkentor Posted April 27, 2009 Share Posted April 27, 2009 I am diving straight into it... (following http://www.kitebird.com/mysql-book/3ed.php) learning the hard way..., and after much searching and trying without success, I would appreciate if someone would help me on the way to achieve the following :-) I want a (one for a starter) checkbox to be populated/checked depending on a field value in a database table -- "equivalent" to populating text fields: function text_field ($name, $value, $size) { printf ("<input type=\"%s\" name=\"%s\" value=\"%s\" size=\"%s\" />\n", "text", htmlspecialchars ($name), htmlspecialchars ($value), htmlspecialchars ($size)); } function display_column ($label, $row, $col_name, $editable = TRUE) { print ("<tr>\n"); printf ("<td>%s</td>\n", htmlspecialchars ($label)); print ("<td>"); if ($editable) # display as edit field text_field ("row[$col_name]", $row[$col_name], 80); else # display as read-only text print (htmlspecialchars ($row[$col_name])); print ("</td>\n"); print ("</tr>\n"); } function display_entry ($conn) //prints a form displaying fields from DB and with a button to update { . . printf ("<form method=\"post\" action=\"%s?action=%s\">\n", script_name (), urlencode (UPDATE_ENTRY)); . . display_column ("Last name", $row, "last_name"); display_column ("First name", $row, "first_name"); . . submit_button ("button", "Submit"); print "</form>\n"; } The relevant field name (column/variable) in the relevant table (member) is "henvis". NULLS are allowed, and value 1 should cause the checkbox to be checked. So something like if($henvis == "1"){echo " CHECKED";} somewhere in a check_box function, equivalent to the text_field function? Would appreciate if any of you gurus could point me in the right direction. :-) Thanks in advance! Quote Link to comment Share on other sites More sharing options...
chmpdog Posted April 27, 2009 Share Posted April 27, 2009 Im confused to what your asking, but I think this would do if you wanted a form to be checked: if($henvis == "1"){ echo "checked='true'"; } Quote Link to comment Share on other sites More sharing options...
kokkentor Posted April 28, 2009 Author Share Posted April 28, 2009 Thanks for replying, chmpdog! I was however thinking more along this line: http://www.fullposter.com/snippets.php?snippet=31&cat=856 I think. Quote Link to comment Share on other sites More sharing options...
kokkentor Posted May 5, 2009 Author Share Posted May 5, 2009 I think I have to start over again. Having tried and tried and seen this post: http://www.phpfreaks.com/content/print/131 I still can't get it working. So if someone can help me, I'll be happy. I am calling a function: display_printBoxColumn ("Henvisning nødvendig?", $row, "henvis"); The function is the follwing: function display_printBoxColumn ($label, $row, $col_name, $editable = TRUE) { print ("<tr>\n"); printf ("<td>%s</td>\n", htmlspecialchars ($label)); print ("<td>"); if ($editable) # display as edit field checkBox ("row[$col_name]", $row[$col_name], "$row[$col_name]"); else # display as read-only text print (htmlspecialchars ($row[$col_name])); print ("</td>\n"); print ("</tr>\n"); } But I can't get the checkbox function below to read the value (0 or 1) from the database and tick off (check) the checkbox if the value of the relevant database field is 1 (in this case column "henvis"), otherwise leave the checkbox unchecked/unticked. function checkbox ($name, $value, $checked) { printf ("<input type=\"%s\" name=\"%s\" value=\"%s\"%s />%s\n", "checkbox", htmlspecialchars ($name), htmlspecialchars ($value), ($checked = ? ($row[$col_name]==1) ? 'checked="checked"' : ''), htmlspecialchars ($label)); } Anyone? Quote Link to comment Share on other sites More sharing options...
kokkentor Posted May 6, 2009 Author Share Posted May 6, 2009 Figured it out. The checkbox function should be like this: function checkbox ($name, $value, $checked) { printf ("<input type=\"%s\" name=\"%s\" value=\"%s\"%s />%s\n", "checkbox", htmlspecialchars ($name), htmlspecialchars ($value), ($checked = ($value==1) ? 'checked="checked"' : ''), htmlspecialchars ($label)); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.