jacko_162 Posted March 15, 2010 Share Posted March 15, 2010 I have a site which allows a user to input upto 14 fields, i want a settings.php page where the user can make fields visable ro hidden. my first thought was to use a series (14x) checkboxes in the settings page and pass a binary value of "1" or "0" to each result where 1=visable and 0=hidden. im VERY confused on how i can achieve this or if there would be a better option to achieve this simple task. im still learning php so its EXTRA confusing. i currently have 2x tables; members (member_id, firstname, lastname, login, email, password) tests (id, member_id, date, time, test1, test2, test3 ...... etc) users can enter, edit and view there results but not every user uses all 14 fields so thats why i wanted a settings page to hide the fields. how can i do this? Quote Link to comment Share on other sites More sharing options...
aeroswat Posted March 15, 2010 Share Posted March 15, 2010 If these settings are viewed all on one page I would recommend storing a new field in your members table that would use, like you suggested, either a 0 or 1 for each field. You could use something like a | or , or ; for a delimiter. Then in your page when you read this setting you would explode the string into an array and read each one. If they are multiples of the same thing then you could use a combo box to allow the user to select how many visible fields they want. However, if each field is its own entity then you will have to use checkboxes in order to achieve this. I'd say you are on the right path. Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted March 15, 2010 Author Share Posted March 15, 2010 If these settings are viewed all on one page I would recommend storing a new field in your members table that would use, like you suggested, either a 0 or 1 for each field. You could use something like a | or , or ; for a delimiter. Then in your page when you read this setting you would explode the string into an array and read each one. If they are multiples of the same thing then you could use a combo box to allow the user to select how many visible fields they want. However, if each field is its own entity then you will have to use checkboxes in order to achieve this. I'd say you are on the right path. thank you, very imformative. each field does have its own entity. i was thinking off adding the 14 fields to the members table, for example; viewtest1 viewtest2 viewtest3 etc..... then having the default value of 1 added when a user signs up, this way all are visable by default. then user can go to settings.php and un check the items they dont want visable. only problem is i dont know where to start with my php code theory is the easy part i guess. can anyone help me out? i can do the following; <input type="checkbox" value="box1" name="checks[]" />box 1 <input type="checkbox" value="box2" name="checks[]" />box 2 <input type="checkbox" value="box3" name="checks[]" />box 3 and print out the array using "print_r($_POST['checks']);" but i dont know where to start on the rest of the coding, like exploding the array and updating the database. Quote Link to comment Share on other sites More sharing options...
aeroswat Posted March 15, 2010 Share Posted March 15, 2010 First you need to select a delimiter. I will use a semi-colon cause it's my favorite. You will need to take those values in that array and implode it like so $str = implode(';',$checkboxes); This will put your checkboxes in 1 single string. Then you can update their info in the database mysql_query("UPDATE members SET settings ='" . $str . "' WHERE member_id='" . $id . "'); Next in order to read it you will get their information via a select query and explode that field into an array $result=mysql_query("SELECT * FROM members WHERE member_id = '" . $id . "'); $row = mysql_fetch_array($result); $arr = array(); $arr = explode(';',$row['settings']); Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted March 15, 2010 Author Share Posted March 15, 2010 i have tried that and failing miserably please help me out. here is my code; <?php session_start(); ?> <? include('Includes/auth.php'); ?> <?php require_once('header.php');?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta http-equiv="content-style-type" content="text/css" /> <meta http-equiv="content-script-type" content="text/javascript" /> <title>Index</title> </head> <body class="cloudy"> <div id="content" class="basic"> <div id="container"> <div class="inner-container"> <div class="box box-75 altbox"> <div class="boxin"> <div class="header"> <h3>Settings</h3> </div> <form enctype="multipart/form-data" action="<? $_SERVER['PHP_SELF']; ?>" method="post"> <? if (isset($submit)) { // UPDATE QUERY CODE WHEN SUBMIT IS ENTERED $str = implode(';',$checkboxes); $insert = "UPDATE members SET settings='" . $str . "' WHERE member_id='" . $member_id . "'"; if (@mysql_query($insert)) { ?> <script type="text/javascript"> document.location.replace('tests.php'); </script> <? } else { echo('Error in you submission:' . mysql_error() . "<br />" . $sql); } } ?> <?php { $result=mysql_query("SELECT * FROM members WHERE member_id = '" . $member_id . "'); $row = mysql_fetch_array($result); $arr = array(); $arr = explode(';',$row['settings']); $member_id = $row['member_id']; $firstname = $row['firstname']; $lastname = $row['lastname']; ?> <input type="text" name="member_id" value="<?php echo $member_id ?>" /> <input type="text" name="member_id" value="<?php echo $firstname ?>" /> <input type="text" name="member_id" value="<?php echo $lastname ?>" /> <input type="text" name="member_id" value="<?php echo $settings ?>" /> <br /> <? echo ( ($member_id == "$_SESSION[sESS_MEMBER_ID]") ? "<input class='button altbutton' type='submit' name='submit' value='Submit' /> <input class='button' type='reset' name='reset' value='Reset' />" : ""); ?> </form><?php } ?> </div> </div> </div> </div></div> </body> Quote Link to comment Share on other sites More sharing options...
aeroswat Posted March 15, 2010 Share Posted March 15, 2010 What's going wrong with ur code? Tell me what errors you are getting. Also don't use the shorthand version of <?php. Never use <? in code unless the setting is set to allow shorthand and the code is not going anywhere else besides that site. Quote Link to comment Share on other sites More sharing options...
aeroswat Posted March 15, 2010 Share Posted March 15, 2010 Also I just saw your message sent to me. Do not make the field TINYINT (1). It needs to be VARCHAR and needs to be 2x the length of however many settings you have. So if you have 20 settings then the length of the field needs to be at least 40. Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted March 15, 2010 Author Share Posted March 15, 2010 Also I just saw your message sent to me. Do not make the field TINYINT (1). It needs to be VARCHAR and needs to be 2x the length of however many settings you have. So if you have 20 settings then the length of the field needs to be at least 40. yes i thought that and changed it before error message i get is: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /homepages/htdocs/*****/*********/*********/set.php on line 54 line 54: $arr = explode(';',$row['settings']); Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 15, 2010 Share Posted March 15, 2010 Nothing wrong with line 54. The issue is further up on line 51. Which is this line $result=mysql_query("SELECT * FROM members WHERE member_id = '" . $member_id . "'); Your not ending your string properly $result=mysql_query("SELECT * FROM members WHERE member_id = '" . $member_id . "'"); Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted March 15, 2010 Author Share Posted March 15, 2010 Nothing wrong with line 54. The issue is further up on line 51. Which is this line $result=mysql_query("SELECT * FROM members WHERE member_id = '" . $member_id . "'); Your not ending your string properly $result=mysql_query("SELECT * FROM members WHERE member_id = '" . $member_id . "'"); beet me to the post within 5 seconds Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted March 15, 2010 Author Share Posted March 15, 2010 aeroswat, with the above code fixed i can now load it and submit, upon submit i dont get any errors and it perform the javascript redirect. but the "settings" field doesnt update; has it something to do with the following; $str = implode(';',$checkboxes); where does it get $checkboxes from? and what values and names should i give the checkboxes? i currently have; <input type="checkbox" value="box1" name="checks[]" />box 1 <input type="checkbox" value="box2" name="checks[]" />box 2 <input type="checkbox" value="box3" name="checks[]" />box 3 thank you. Quote Link to comment Share on other sites More sharing options...
aeroswat Posted March 15, 2010 Share Posted March 15, 2010 aeroswat, with the above code fixed i can now load it and submit, upon submit i dont get any errors and it perform the javascript redirect. but the "settings" field doesnt update; has it something to do with the following; $str = implode(';',$checkboxes); where does it get $checkboxes from? and what values and names should i give the checkboxes? i currently have; <input type="checkbox" value="box1" name="checks[]" />box 1 <input type="checkbox" value="box2" name="checks[]" />box 2 <input type="checkbox" value="box3" name="checks[]" />box 3 thank you. lol >< I thought u would fill in the $checkboxes with the name of your array. It is supposed to be the checks array in there. so instead you would have $str = implode(';',$_POST['checks']); Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted March 15, 2010 Author Share Posted March 15, 2010 aeroswat, with the above code fixed i can now load it and submit, upon submit i dont get any errors and it perform the javascript redirect. but the "settings" field doesnt update; has it something to do with the following; $str = implode(';',$checkboxes); where does it get $checkboxes from? and what values and names should i give the checkboxes? i currently have; <input type="checkbox" value="box1" name="checks[]" />box 1 <input type="checkbox" value="box2" name="checks[]" />box 2 <input type="checkbox" value="box3" name="checks[]" />box 3 thank you. lol >< I thought u would fill in the $checkboxes with the name of your array. It is supposed to be the checks array in there. so instead you would have $str = implode(';',$_POST['checks']); excellent its now updating the database with the array contents; box1;box2 when i load the settings.php page tho its not loading the boxes with the checked states etc.. Quote Link to comment Share on other sites More sharing options...
aeroswat Posted March 15, 2010 Share Posted March 15, 2010 In order to load them with the correct items to be checked you will need to do a little magic. First you will have to load the settings field for the member into that array with the code i showed u up above. Then you will need to use some logic operators to check to see if the html input should be checked or not something like this <input type="checkbox" value="box1" name="checks[]" <?php echo ($arr[0] == 1 ? "checked" : ''); ?>/>box 1 <input type="checkbox" value="box2" name="checks[]" <?php echo ($arr[1] == 1 ? "checked" : ''); ?>/>box 2 <input type="checkbox" value="box3" name="checks[]" <?php echo ($arr[2] == 1 ? "checked" : ''); ?>/>box 3 Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted March 15, 2010 Author Share Posted March 15, 2010 In order to load them with the correct items to be checked you will need to do a little magic. First you will have to load the settings field for the member into that array with the code i showed u up above. Then you will need to use some logic operators to check to see if the html input should be checked or not something like this <input type="checkbox" value="box1" name="checks[]" <?php echo ($arr[0] == 1 ? "checked" : ''); ?>/>box 1 <input type="checkbox" value="box2" name="checks[]" <?php echo ($arr[1] == 1 ? "checked" : ''); ?>/>box 2 <input type="checkbox" value="box3" name="checks[]" <?php echo ($arr[2] == 1 ? "checked" : ''); ?>/>box 3 this is what i got laid out; <?php { $result=mysql_query("SELECT * FROM members WHERE member_id=$_SESSION[sESS_MEMBER_ID]"); $row = mysql_fetch_array($result); $arr = array(); $arr = explode(';',$row['settings']); $member_id = $row['member_id']; ?> <?php echo $member_id ?> <input type="checkbox" value="test1" name="checks[]" <?php echo ($arr[0] == 1 ? "checked" : ''); ?>/>Test 1<br /> <input type="checkbox" value="test2" name="checks[]" <?php echo ($arr[1] == 1 ? "checked" : ''); ?>/>Test 2<br /> <input type="checkbox" value="test3" name="checks[]" <?php echo ($arr[2] == 1 ? "checked" : ''); ?>/>Test 3<br /> <input type="checkbox" value="test4" name="checks[]" <?php echo ($arr[3] == 1 ? "checked" : ''); ?>/>Test 4<br /> <input type="checkbox" value="test5" name="checks[]" <?php echo ($arr[4] == 1 ? "checked" : ''); ?>/>Test 5<br /> <input type="checkbox" value="test6" name="checks[]" <?php echo ($arr[5] == 1 ? "checked" : ''); ?>/>Test 6<br /> <input type="checkbox" value="test7" name="checks[]" <?php echo ($arr[6] == 1 ? "checked" : ''); ?>/>Test 7<br /> <input type="checkbox" value="test8" name="checks[]" <?php echo ($arr[7] == 1 ? "checked" : ''); ?>/>Test 8<br /> <input type="checkbox" value="test9" name="checks[]" <?php echo ($arr[8] == 1 ? "checked" : ''); ?>/>Test 9<br /> <input type="checkbox" value="test10" name="checks[]" <?php echo ($arr[9] == 1 ? "checked" : ''); ?>/>Test 10<br /> <input type="checkbox" value="test11" name="checks[]" <?php echo ($arr[10] == 1 ? "checked" : ''); ?>/>Test 11<br /> <input type="checkbox" value="test12" name="checks[]" <?php echo ($arr[11] == 1 ? "checked" : ''); ?>/>Test 12<br /> <input type="checkbox" value="test13" name="checks[]" <?php echo ($arr[12] == 1 ? "checked" : ''); ?>/>Test 13<br /> <input type="checkbox" value="test14" name="checks[]" <?php echo ($arr[13] == 1 ? "checked" : ''); ?>/>Test 14<br /> <br /> <?php echo ( ($member_id == "$_SESSION[sESS_MEMBER_ID]") ? "<input class='button altbutton' type='submit' name='submit' value='Submit' /> <input class='button' type='reset' name='reset' value='Reset' />" : ""); ?> </form><?php } ?> upon loading page i still get un checked boxes. but the database isnt showing "1" or "0" it shows; test1;test3;test5 do i have to change the value of the checkboxes? Quote Link to comment Share on other sites More sharing options...
aeroswat Posted March 15, 2010 Share Posted March 15, 2010 Shit >< We need to rethink the way you are going to do this. Didn't take into account that you were using an array for your checkboxes. What you will need to do is change the logic. It needs to check in the array to see if that checkbox exists. So it will be like this: <input type="checkbox" value="box1" name="checks[]" <?php echo (in_array("box1",$arr) ? "checked" : ''); ?>/>box 1 <input type="checkbox" value="box2" name="checks[]" <?php echo (in_array("box2",$arr) ? "checked" : ''); ?>/>box 2 <input type="checkbox" value="box3" name="checks[]" <?php echo (in_array("box3",$arr) ? "checked" : ''); ?>/>box 3 Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 15, 2010 Share Posted March 15, 2010 You'll be a lot better of generating your checkboxes dynamically. I'd setup an array which will store all possible options, eg test1 to test20 $options = array(); for($i = 1; $i <= 20; $i++) $options[] = 'test'.$i; Now we can easily generate the checkboxes foreach($options as $option) { $checked = (in_array($option ,$arr) ? ' checked="checked"' : null); echo '<input type="checkbox" value="'.$option.'" name="checks[]"'.$checked.'/>'. $option . '<br />'; } $arr is the options stored in your database $arr = array(); $arr = explode(';',$row['settings']); This will save you a lot of typing Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted March 15, 2010 Author Share Posted March 15, 2010 Shit >< We need to rethink the way you are going to do this. Didn't take into account that you were using an array for your checkboxes. What you will need to do is change the logic. It needs to check in the array to see if that checkbox exists. So it will be like this: <input type="checkbox" value="box1" name="checks[]" <?php echo (in_array("box1",$arr) ? "checked" : ''); ?>/>box 1 <input type="checkbox" value="box2" name="checks[]" <?php echo (in_array("box2",$arr) ? "checked" : ''); ?>/>box 2 <input type="checkbox" value="box3" name="checks[]" <?php echo (in_array("box3",$arr) ? "checked" : ''); ?>/>box 3 thats done the trick. i believe it all works now.. now to list up the checkboxes dynamically i guess thank you VERY much aero, you the man Quote Link to comment Share on other sites More sharing options...
aeroswat Posted March 15, 2010 Share Posted March 15, 2010 Shit >< We need to rethink the way you are going to do this. Didn't take into account that you were using an array for your checkboxes. What you will need to do is change the logic. It needs to check in the array to see if that checkbox exists. So it will be like this: <input type="checkbox" value="box1" name="checks[]" <?php echo (in_array("box1",$arr) ? "checked" : ''); ?>/>box 1 <input type="checkbox" value="box2" name="checks[]" <?php echo (in_array("box2",$arr) ? "checked" : ''); ?>/>box 2 <input type="checkbox" value="box3" name="checks[]" <?php echo (in_array("box3",$arr) ? "checked" : ''); ?>/>box 3 thats done the trick. i believe it all works now.. now to list up the checkboxes dynamically i guess thank you VERY much aero, you the man Not a problem at all man If ya need more help just let us know. That's what we're here for! Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted March 15, 2010 Author Share Posted March 15, 2010 Shit >< We need to rethink the way you are going to do this. Didn't take into account that you were using an array for your checkboxes. What you will need to do is change the logic. It needs to check in the array to see if that checkbox exists. So it will be like this: <input type="checkbox" value="box1" name="checks[]" <?php echo (in_array("box1",$arr) ? "checked" : ''); ?>/>box 1 <input type="checkbox" value="box2" name="checks[]" <?php echo (in_array("box2",$arr) ? "checked" : ''); ?>/>box 2 <input type="checkbox" value="box3" name="checks[]" <?php echo (in_array("box3",$arr) ? "checked" : ''); ?>/>box 3 thats done the trick. i believe it all works now.. now to list up the checkboxes dynamically i guess thank you VERY much aero, you the man Not a problem at all man If ya need more help just let us know. That's what we're here for! seen as you asked so nicely. is it possible to make an If statement to check if test4 is in the stored array in "settings" field in the database, and if so echo a small icon? this i will repeat on another page for each test result. Quote Link to comment Share on other sites More sharing options...
aeroswat Posted March 15, 2010 Share Posted March 15, 2010 Yes of course. Just wherever you want to put the icon type in this code: <?php echo (in_array("test4",$arr) ? "<img src='icon.gif' />" : ''); ?> of course whatever page you do this on you will have to have done the explode we talked about with the mysql_query results Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted March 15, 2010 Author Share Posted March 15, 2010 Yes of course. Just wherever you want to put the icon type in this code: <?php echo (in_array("test4",$arr) ? "<img src='icon.gif' />" : ''); ?> of course whatever page you do this on you will have to have done the explode we talked about with the mysql_query results thank you again Quote Link to comment Share on other sites More sharing options...
Solution aeroswat Posted March 15, 2010 Solution Share Posted March 15, 2010 Yes of course. Just wherever you want to put the icon type in this code: <?php echo (in_array("test4",$arr) ? "<img src='icon.gif' />" : ''); ?> of course whatever page you do this on you will have to have done the explode we talked about with the mysql_query results thank you again No problem Please mark the thread as solved 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.