sabinmash Posted December 2, 2011 Share Posted December 2, 2011 I've been trying to simplify this code, let alone make it work. It's a loop to change the different question checkboxes to "checked" if certain degree_ids are found. All of the examples i have looked at are very strange and I'm not sure how to go about this, thought it seems to be a common question. Everyone seems to have their own way. Any advice would be greatly appreciated. for($i =1; $i <= $arraycount; $i++){ $query = "SELECT degree_id FROM `user-degree` WHERE `user_id` = '".$user_id."' AND degree_id = '".$i."'"; $result = $conn->query($query) or die(mysql_error()); $row = mysqli_fetch_array($result) ; echo $row['degree_id']; if(!$y){ $y = 0; } if ($row['degree_id'] == 1){ $q4[0] = 'checked'; } if ($row['degree_id'] == 2){ $q4[1] = 'checked'; } if ($row['degree_id'] == 3){ $q4[2] = 'checked'; } if ($row['degree_id'] == 4){ $q4[3] = 'checked'; } if ($row['degree_id'] == 5){ $q4[4] = 'checked'; } if ($row['degree_id'] == 6){ $q4[5] = 'checked'; } if ($row['degree_id'] == 7){ $q4[6] = 'checked'; } if ($row['degree_id'] == { $q4[7] = 'checked'; } if ($row['degree_id'] == 9){ $q4[8] = 'checked'; } } Quote Link to comment https://forums.phpfreaks.com/topic/252352-trouble-with-using-database-retrievals-to-turn-check-boxes-to-checked/ Share on other sites More sharing options...
br3nn4n Posted December 2, 2011 Share Posted December 2, 2011 This may be the most useless advice you'll read on here, but keep in mind that you need to later call those $q4's in the HTML form. What might help is if you just did shorthand ifs for each check, and did this: <?php $row['degree_id'] == 1 ? $q4[0] = 'checked'; : $q4[0] == ''; ?> That says to set $q4[0] to be the string 'checked' if degree_id is 1, and nothing if it isn't. What you can then do in the html is: <input type="checkbox" name="degree_1" <?=$q4[0];?> /> That will spit out a "checked" into the <input /> element if degree_id is 1, which would check the box. Again, that probably was no help at all and not what you were asking for help with, but I hope it helps in some way. Quote Link to comment https://forums.phpfreaks.com/topic/252352-trouble-with-using-database-retrievals-to-turn-check-boxes-to-checked/#findComment-1293703 Share on other sites More sharing options...
sabinmash Posted December 2, 2011 Author Share Posted December 2, 2011 Thank you this will certainly help shorten the code a bit. If only it worked! I have spent so many hours on this problem, and of course the solution is going to be relatively simple i bet. Quote Link to comment https://forums.phpfreaks.com/topic/252352-trouble-with-using-database-retrievals-to-turn-check-boxes-to-checked/#findComment-1293719 Share on other sites More sharing options...
Pandemikk Posted December 3, 2011 Share Posted December 3, 2011 I see lots of problems with your code. You shouldn't be querying data inside a loop first of all. Secondly, you don't need that many if statements; just assign the value. Thirdly, you shouldn't give your variables shortnames just to less the amount of code. That's just ridiclous. Make your variables make sense so that when you or other people look at your code with unfamiliarity they'll be able to understand wtf is going on. <?php // i dont know how you're getting $arraycount so i can't show you how to do this part // just sample data $sql_in = '1, 2, 3, 4, 5'; // don't wrap integers with apostrophes in your query. // the in clause will allow you to grab all your results with one query // instead of having to do a seperate query for each one (waste of resources) $query = "SELECT degree_id FROM `user-degree` WHERE `user_id` = " . intval($user_id) . "' AND degree_id IN (" . $sql_in . ")"; $result = $conn->query($query) or die(mysql_error()); while ($row = mysqli_fetch_array($result)) { echo $row['degree_id']; // what is $y? if (!$y) { $y = 0; } $q4[intval($row['degree_id']) - 1] = 'checked'; } ?> I'm pretty sure that the HTML should be checked="checked" btw. Quote Link to comment https://forums.phpfreaks.com/topic/252352-trouble-with-using-database-retrievals-to-turn-check-boxes-to-checked/#findComment-1293725 Share on other sites More sharing options...
Pikachu2000 Posted December 3, 2011 Share Posted December 3, 2011 Based only on the code you've posted, since the value of the index of the $q4[] array seems to always be one less than the value from the database: $cb = $row['degree_id'] - 1 $q4[$cb] = 'checked = "checked"'; P.S. Running queries in a loop is not the best idea. Quote Link to comment https://forums.phpfreaks.com/topic/252352-trouble-with-using-database-retrievals-to-turn-check-boxes-to-checked/#findComment-1293727 Share on other sites More sharing options...
sabinmash Posted December 3, 2011 Author Share Posted December 3, 2011 Oh, i didn't know about the IN clause, that really makes things a lot simpler, thank you! I am trying to make this a function to be used multiple times. However, nothing is being checked in the checkboxes when i use it as a function. function create_questions($question, $total_options){ for($i=0 ; $i<$total_options; $i++) { $question[] = array($i); } return $question; } function uncheck_questions($question, $total_options){ for($i=0 ; $i<$total_options; $i++) { $question[$i] = 'unchecked'; } return $question; } function data_retrieve($question_numb, $total_options, $sql_in_num, $id_being_checked, $get_query){ $conn = db_connect(); //create the question selection array that will hold the "checked" or "unchecked" variables. $question_numb = create_questions($question_numb, $total_options); //uncheck all of the $question_numb = uncheck_questions($question_numb, $total_options); // The IN operator allows you to specify multiple values in a WHERE clause, allowing you to grab all your results with one query. $sql_in = $sql_in_num; // Remember, don't wrap integers with apostrophes in your query. $query = $get_query; $result = $conn->query($query) or die(mysql_error()); while ($row = mysqli_fetch_array($result)) { //echo $row['id_being_checked']; $question_numb[intval($row[$id_being_checked]) - 1] = 'checked'; } } The query function is here. function get_question4_data($sql_in) { // Remember, don't wrap integers with apostrophes in your query. $query = "SELECT degree_id FROM `user-degree` WHERE `user_id` = " . $_SESSION['user_id'] . " AND degree_id IN (" . $sql_in . ")"; return $query ; } Quote Link to comment https://forums.phpfreaks.com/topic/252352-trouble-with-using-database-retrievals-to-turn-check-boxes-to-checked/#findComment-1294018 Share on other sites More sharing options...
scootstah Posted December 3, 2011 Share Posted December 3, 2011 It should be checked="checked", not just checked. Quote Link to comment https://forums.phpfreaks.com/topic/252352-trouble-with-using-database-retrievals-to-turn-check-boxes-to-checked/#findComment-1294027 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.