Jump to content
zazu

Insert checkbox into database and the select checked ones!

Recommended Posts

Hi guys,

 

I have a huge problem with inserting multiple checked checkboxes into database and thene display checkones on frontend.

 

In other word i have 2 problems regarding these checkboxes.

 

1. I have 3 checkboxes into user profile panel lets say checkbox 1, checkbox 2, etc. The user select the first 2 ones and inserts them into database. When he returns to his profile to edit his details, i want to display to the user those 2 checkbox that he selected earlier.

 

2. The secound problem i have is that i have a weekly calendar with week days and some hour intervals. For this the user must select the hours for a day when he is available and on his frontend profile to display them. This is another thing that i can't manage to do, and want a ideea.

 

A print scrit of this calendar is attached to this message.

post-165360-0-84122600-1431286235_thumb.png 

Share this post


Link to post
Share on other sites

To give you a more specific answer, we would need to know more about the html for the checkboxes and the kind of values you are storing in the db for these checkboxes.  Here is the most basic of answers though.  You simply need to compare the value in the db to the value of the checkbox and if they match, you output the checked attribute with php.  For that basic scenario to work, you would need the "name" of the checkbox to be the same as the name of the column in the db that is storing the value for that specific checkbox.

// php code in here is basically saying "if var1 equals 1 then echo checked"
<input type="checkbox" name="var1" value="1" <?php echo ($db['var1'] == 1) ? : 'checked="checked"'; ?>>
<input type="checkbox" name="var2" value="1" <?php echo ($db['var2'] == 1) ? : 'checked="checked"'; ?>>

Again, that is the most basic of concepts, which all I can provide without more detailed info about your code / db structure.

Share this post


Link to post
Share on other sites

<label class="checkbox-inline">
<input type="checkbox" name="children_age" value="1-3 years"> 1-3 years
<input type="checkbox" name="children_age" value="2-4 years"> 2-4 years
<input type="checkbox" name="children_age" value="4-6 years"> 4-6 years
</label>

Share this post


Link to post
Share on other sites

Ok, then you would be better off using arrays to dynamically build the checkboxes and compare the values.

Here's a working concept based on the html you posted.

$child_age = '2-4 years'; // I preset a value here to show you how it checks the box when it sees a match.

$ages = array(
	'1-3 years',
	'2-4 years',
	'4-6 years'
);

$html = '';

foreach($ages as $a)
{
	$html .= '<input type="checkbox" name="children_age" value="'.$a.'"'; // Build first part of the input
	
	$html .= ($a == $child_age) ? 'checked="checked"' : ''; // Check if the db value matches the inputs value
	
	$html .= '>'; // Close the input
}

echo $html;

$child_age would need to edited to equal the value from the db. So something like

$child_age = $row['children_age']; // $row['children_age'] would be the value from the db, just make sure to change the name to whatever the column name is in the db.

Share this post


Link to post
Share on other sites

So, having normalized your data you will have a table (eg age) to define the age ranges and table (eg children) to store the checkbox values each use chose.
 

age                                      children
+-------+-----------------+              +------+-------------+----------+
|  id   |    age_range    |              |  id  |  user_id    | age_id   |
+-------+-----------------+              +------+-------------+----------+
|   1   | 1-2 years       |              |   1  |     1       |    2     |
|   2   | 2-4 years       |              |   2  |     1       |    4     |
|   4   | 4-6 years       |              |   3  |     2       |    1     |
                                         |   4  |     2       |    2     |
                                         |   5  |     3       |    1     |

You use these tables to create the checkboxes for the user, so running this query (for user 1)
 

SELECT 
    a.id
   ,a.age_range
   ,c.id as checked
FROM
    age a
    LEFT JOIN children c ON a.id = c.age_id AND c.user_id = 1
ORDER BY a.id

Which will give
 

  id   |  age_range  |  checked
-------+-------------+----------
   1   |  1-2 years  |   NULL
   2   |  2-4 years  |    2
   4   |  4-6 years  |    4    

Loop through these results creating the checkboxes.

"id" will be the checkbox value and "age_range" will be the label.

If "checked" has value then the checkbox should be checked

Edited by Barand

Share this post


Link to post
Share on other sites

 

Ok, then you would be better off using arrays to dynamically build the checkboxes and compare the values.

Here's a working concept based on the html you posted.

$child_age = '2-4 years'; // I preset a value here to show you how it checks the box when it sees a match.

$ages = array(
	'1-3 years',
	'2-4 years',
	'4-6 years'
);

$html = '';

foreach($ages as $a)
{
	$html .= '<input type="checkbox" name="children_age" value="'.$a.'"'; // Build first part of the input
	
	$html .= ($a == $child_age) ? 'checked="checked"' : ''; // Check if the db value matches the inputs value
	
	$html .= '>'; // Close the input
}

echo $html;

$child_age would need to edited to equal the value from the db. So something like

$child_age = $row['children_age']; // $row['children_age'] would be the value from the db, just make sure to change the name to whatever the column name is in the db.

Hei, works well but only when i have a single value in my database, when i have 2 or 3 values isn't showing any checked inputs. Why is that? 

Share this post


Link to post
Share on other sites

You mean 2+ values for each User or 2+ values in the db records at all?  You should only be gathering records for the particular User of those records right?  Honestly it's hard to say cause we don't know your DB structure and how these things you are asking all tie together cause you haven't shown or told us any of that.

 

But to answer your specific question, the code I provided was designed with only a single value in mind for $child_age.  You would need to make an array of $child_age instead.  Here is a modified version of what I gave you.

$child_age = ['2-4 years', '1-3 years']; // I preset a value here to show you how it checks the box when it sees a match.

$ages = array(
	'1-3 years',
	'2-4 years',
	'4-6 years'
);

$html = '';

foreach($ages as $a)
{
	$html .= '<input type="checkbox" name="children_age" value="'.$a.'"'; // Build first part of the input
	
	$html .= (in_array($a, $child_age)) ? 'checked="checked"' : ''; // Check if the db value matches the inputs value
	
	$html .= '>'; // Close the input
}

echo $html;

Share this post


Link to post
Share on other sites

 

You mean 2+ values for each User or 2+ values in the db records at all?  You should only be gathering records for the particular User of those records right?  Honestly it's hard to say cause we don't know your DB structure and how these things you are asking all tie together cause you haven't shown or told us any of that.

 

But to answer your specific question, the code I provided was designed with only a single value in mind for $child_age.  You would need to make an array of $child_age instead.  Here is a modified version of what I gave you.

$child_age = ['2-4 years', '1-3 years']; // I preset a value here to show you how it checks the box when it sees a match.

$ages = array(
	'1-3 years',
	'2-4 years',
	'4-6 years'
);

$html = '';

foreach($ages as $a)
{
	$html .= '<input type="checkbox" name="children_age" value="'.$a.'"'; // Build first part of the input
	
	$html .= (in_array($a, $child_age)) ? 'checked="checked"' : ''; // Check if the db value matches the inputs value
	
	$html .= '>'; // Close the input
}

echo $html;

Well let me explain you more exactly what i have to do.

 

In my database i have a table where is a row named child_age. In the form i have 4 checkboxes 1-3 years.....10+ years. All these checkboxex must be inserted into child_age row like an array = 1-3 years, 3-6 years etc. depends what the user is selecting. And when the user edits his profile the form must returns him the selected checkboxex in the first time. So let's say in the first time he selected 1-3 years and 3-6 years, when he returns back on his profile and want to choose 6-8 years beside 1-3 years and 3-6 years the checkboxes with these values to be checked.

Share this post


Link to post
Share on other sites

So you're saying that the selections are stored in the db as a comma separated list like 1-3 years, 3-6 years ?  That's still pretty easy to deal with, although not the best way to store the data.  Ideally you should have another table that holds the individual selections, similar to a previous post on here.  But for your current setup, all you need to do is use explode() on the returned db string to make the $child_age array.

$child_age = explode(', ', $row['child_age']);

Share this post


Link to post
Share on other sites

 

So you're saying that the selections are stored in the db as a comma separated list like 1-3 years, 3-6 years ?  That's still pretty easy to deal with, although not the best way to store the data.  Ideally you should have another table that holds the individual selections, similar to a previous post on here.  But for your current setup, all you need to do is use explode() on the returned db string to make the $child_age array.

$child_age = explode(', ', $row['child_age']);

And how should the final code look like? I should replace 

$child_age = '2-4 years';

with 

$child_age = explode(', ', $row['child_age']);

Share this post


Link to post
Share on other sites

Yes assuming that $row is the name of the var you are using and assuming that you have a comma and a space between each value in the string.  If you don't understand how to use explode() then check the php.net for documentation, it's a very easy simple function to use.

Share this post


Link to post
Share on other sites

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.