Jump to content

Trouble with using database retrievals to turn check boxes to "checked"


sabinmash

Recommended Posts

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';	
}
}

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.