Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 7 of 7

Thread: Tick checkbox if database field contains value

  1. #1

    Tick checkbox if database field contains value

    Hi Guys,

    I have used a bit of PHP but I'm not very good at it!

    I was hoping someone could help with my problem.

    I have a table with several checkboxes.
    The user can select the boxes, the values are made into a comma separated string and added to a DB field.
    I want the checkboxes that have been previously checked (the field contents) to be checked when the user revisits that page.

    So it's something like.

    if($field CONTAINS $checkBoxValue){
    checkbox = checked
    }

    I am having trouble thinking of a way of implementing this.

    Here is my form code:

    HTML Code:
    <form method=post action=''>
            
        <table width="100">
        <tr>
        <td width="60">Identity</td>
        <td width="40"><input type="checkbox" <?php echo $checked; ?> name=fields[] value='Identity'></td>
        </tr>
        
        <tr>
        <td width="60">Stationery</td>
        <td width="40"><input type="checkbox" <?php echo $checked; ?> name=fields[] value='Stationery'></td>
        </tr>
        
        <tr>
        <td width="60">Print</td>
        <td width="40"><input type="checkbox" <?php echo $checked; ?> name=fields[] value='Print'></td>
        </tr>
        
        <tr>
        <td width="60">Packaging</td>
        <td width="40"><input type="checkbox" <?php echo $checked; ?> name=fields[] value='Packaging'></td>
        </tr>
        
        <tr>
        <td width="60">Exhibition</td>
        <td width="40"><input type="checkbox" <?php echo $checked; ?> name=fields[] value='Exhibition'></td>
        </tr>
        
        <tr>
        <td width="60">Web</td>
        <td width="40"><input type="checkbox" <?php echo $checked; ?> name=fields[] value='Web'></td>
        </tr>
        
        <tr>
        <td width="60">3D</td>
        <td width="40"><input type="checkbox" <?php echo $checked; ?> name=fields[] value='3D'></td>
        </tr>
        
        </table><br>
        
        <input type="submit" name="editFields" value="Select">
        
        
        </form>
    Here is my PHP code:

    PHP Code:
    $checked "";

    $WordsToFind "Identity,Stationery,Print,Packaging,Exhibition,Web,3D";
    $WordsToFind explode(',',$WordsToFind);

    $text "Identity,Stationery,Print,Packaging,Exhibition,Web,3D";

    for(
    $i 0$i<count($WordsToFind); $i++) {
        if(
    stripos($text,$WordsToFind[$i])) { 
            
    $checked='checked'; break; 
        }

    NOTE: $WordsToFind is actually pulled from a DB. I have written it out so you can see the values.


    The PHP works, just not in the way I would like. i.e. $checked does equal "checked" but I need it to only equal checked for the correct checkboxes.


    Thanks in advance.

    Sean

  2. #2
    actionAction's Avatar
    1,142
    posts
    humanBeing._beard=true;
    The way you have implemented the database is going to limit your options with displaying what the user has checked. In fact, the way you have done it is probably your only option. It is not good practice to store comma delimited strings in fields, especially when you are wanting to look at each individual item in the field. Database storage engines are much faster than PHP, so it would be ideal if SQL was doing the heavy lifting. Plus, what happens if a person is no longer interested in something, and what if you stop offering a service they are interested in?

    I would suggest creating two new tables: one for the items they could be interested in (it looks like that's what you are doing):
    item_id
    item_name
    Another table will create the relationship, person_interest:
    person_id
    item_id

    Then you can query like this (to get a person with all of the things they are interested in):
    Code:
    SELECT p.person_id, p.person_name, pi.item_id, i.item_name 
    FROM person p, person_interest pi, item i
    WHERE i.item_id = pi.item_id
    AND pi.person_id = p.person_id
    or even better like this:
    Code:
    SELECT person.person_id, person.person_name, person_interest.item_id, item.item_name
    FROM person LEFT JOIN (person_interest LEFT JOIN item ON item.id = person_interest.item_id)
    ON person.person_id = person_interest.person_id
    GROUP BY person_id
    Hope this helps you!

  3. #3
    Thanks for the fast reply!

    I'm not too clued up about relationships in DBs (I need to read up on it!)

    It doesn't matter too much about the customers because this part of the site is in the admin area, but thanks for the heads up!

    This is basically part of a portfolio website which shows the list of clients on a page. The clients may have several different types of work completed for them e.g. Print, Web, 3D or just the one e.g. Identity.
    I couldn't think of an easy way of doing this. (I don't know enough options)

    I have different pages that will show different sections of work. e.g. Identity
    That is displayed using the same sort of process, turns the field into an Array, searches through for the right keyword, and uses a while loop to display all of the clients with the keyword.


    If I could learn more about relational DBs that seems to be a cleaner way of working it.

    One question about your reply, in the first example where does the p. i. and pi. come from?

    Any advice on this wold be much appreciated!

    Thanks

  4. #4
    actionAction's Avatar
    1,142
    posts
    humanBeing._beard=true;
    No problem! To answer your last question first, the p., i. etc. is an alias for a table name. It is necessary when selecting a column from two tables where the column name is the same. For instance, if you selected person.person_id and person_interest.person_id; the query result would be one person_id column, not two. However if you aliased the columns (person.person_id AS p1, person_interested.person_id AS p2) you can access them separately. I used them in my example to alias table names as more of a typing convenience.

    Moving on, the big gain in using a relational database is relationships (though MySQL often is used as a flat data store). Relational databases (though there are many books and courses on them) are really not all that difficult to comprehend.

    1. Tables should have a strong primary key. Most often, it is just easiest (and perfectly acceptable) to have an auto-incremented integer be the primary key.
    2. Database tables should be "normalized". To simplify what this means, let's just say as an example that your person table should ONLY have personal details name, address, city, state, zip, phone. The technical term is functional determinism
    3. Normalized database tables should be related to one another through Bridge connections. What this means is that using only the primary keys from each table, a relationship is formed (like the person_interest table I described above). This allows relationships to be added and broken, details from individual tables to be updated with less risk of corruption.

    So, to answer your pressing question. It isn't working because you are assigning the same value to all checkboxes ($checked). I will post the solution in a few minutes (busy at the moment)
    Last edited by actionAction; November 9th, 2009 at 12:48 PM.

  5. #5
    actionAction's Avatar
    1,142
    posts
    humanBeing._beard=true;
    PHP Code:
    $WordsToFind "Identity,Stationery,Print,Packaging,Exhibition,Web,3D";
    $WordsToFind explode(',',$WordsToFind);

    $text "Identity,Stationery,Print,Packaging,Exhibition,Web,3D";
    //Explode this as well, then you can compare arrays
    $text explode(","$text);

    //returns array of matched values between the two arrays
    $checked_boxes array_intersect($text$WordsToFind); 
    The statement below says: if 'Print' is in the array of checked boxes, echo "checked"; otherwise nothing.

    HTML Code:
    <tr>
        <td width="60">Print</td>
        <td width="40"><input type="checkbox" <?php echo (in_array('Print', $checked_boxes)) ? "checked" : "" ?> name=fields[] value='Print'></td>
    </tr>
    Let me know if you have any questions. I still suggest you consider modifying your database model, it will be much more maintainable in the long run.

  6. #6
    Thank you VERY much for your answers!
    I have never received such helpful answers on a forum before!

    I will use your solution for now and look into changing my database for the future.

    I'm sure I will be back with questions about that!!

    Thanks again actionAction

  7. #7
    actionAction's Avatar
    1,142
    posts
    humanBeing._beard=true;
    No problem, glad to help. Come back anytime with questions, there are a lot of friendly and helpful folks around here.

    Good luck on your project!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012