0

I have a field in my database called Weeks with entries in the form 1,2,3,4 or 1,3,5,6 ranging from minimum 1 to maximum 12. I was wondering how could select from field using SQL and output it as checkboxes.

For example if Weeks has the values 1,2,3,4 then the resulting output would be the following series of checkboxes:

checked Week 1
checked Week 2
checked Week 3
checked Week 4
unchecked Week 5
unchecked Week 6
unchecked Week 7
unchecked Week 8
unchecked Week 9
unchecked Week 10
unchecked Week 11
unchecked Week 12

3 Answers 3

2

If you have an array of checked weeks (ie. array(1, 3, 6, 7)) you could loop through the twelve weeks and see if the current week should be checked.

$selected_weeks = array(1, 3, 6, 7); // retrieve this from database
for($i=0; $i<12; $i++) { // loop twelve times
    $checked = in_array($i, $selected_weeks) ? ' checked="checked"' : '';
    echo '<input type="checkbox" name="selected_weeks[]" value="'. $i . '"'. $checked . ' />';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! How would I SELECT the array from the database? @Djumaka says I need to split them by ',' to create the array - can you show me an example I can implement in my work.
accidentally upvoted your comment... but well, gives you a rep kickstart :) to find out how to convert a string into an array by a certain delimiter please chekout nl3.php.net/manual/en/function.explode.php. If you don't know how to write the sql query go out and find some good tutorials, you're really gonna need them!
1

I am going to assume you're capable of pulling the data from the database and you're looking for the PHP code to create the checkboxes.

$result = <YOUR DB RESULTS>;
$checked_array = explode(',', $result);


for ($i = 1; $i <= 12; $i++)
{
    $checked = (in_array($i, $checked_array)) ? 'checked="checked"' : '';

    echo '<input type="checkbox"' . $checked . ' /> Week ' . $i;
}

Comments

0

Sory for being rude, but you are trying to make someone else solve your problem...

Anyway try printing all checkboxes from 1 to 12, and on each checkbox you should check witn in_array() if the current number is in the array from database. You might have to split the db field by "," to make it an array if all selected values are stored in a single column...

Try this and write back if any questions arise.

3 Comments

yes that is quite rude ;) try explaining to poster how a good SO question should be formulated, that is quite helpful. Look at his rep, don't blame him for not knowing
I apologise I'm new on the website - I will get better as time progresses
@Shay, I don't mean to be abusive. I know you're new here, we're all new :) Just my point was that you should look around in the internet, investigate a little, before posting. The very question is posted perfectly, it just apears that you haven't made any search before questioning.Giorgio's post is the solution. I didn't want to give you code, in order to make you look around a little :) Givin you ready-to-use code won't teach you a thing... Good luck in moving forawrd :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.