SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Mar 2002
    Location
    Toronto
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Update from Checkbox Array

    Hello,

    I'm having a problem updating a Product lookup table
    from a checkbox array, and need some help.

    I have an Edit page where you can select
    Categories for each Product, using checkboxes:

    For example:

    Category - checkbox array - $fcategory[]

    1 - Canadian Catalogue
    2 - USA Catalogue
    3 - 3 Day Sale
    4 - 2 for 1 Sale
    5 - Half Price Sale


    For example, for Product ID 362, the edit form is
    populated from a lookup on tbl_catlookup,
    and the $fcategory[] (checkboxes array) would
    have items 3 and 4 checked:

    Current table:
    $result = mysql_query("SELECT * FROM tbl_catlookup
    WHERE ProdID=".$fid." ORDER BY CatID ASC", $db);

    Table: tbl_catlookup
    Code:
    =========================== 
    ProdID CatID
    ===========================
    362	 3
    362	 4
    Now, assume the following changes are made
    to "Category" on the Edit page and submitted
    to the database:

    1 - Canadian Catalogue (REMAINS UNCHECKED)
    2 - USA Catalogue (REMAINS UNCHECKED)
    3 - 3 Day Sale (REMAINS CHECKED - SAME)
    4 - 2 for 1 Sale (CHECK REMOVED - DELETE)
    5 - Half Price Sale (NOW CHECKED - ADD)

    I now need to DELETE CatID 4 and ADD CatID 5
    to tbl_catlookup.

    How do I go about looping through the checkbox array,
    and updating the table (DELETE CatID 4 and ADD CatID 5)?

    Any help you can shed would be appreciated....

    Thanks!

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why u submit the form and the recheck the checkboxes?
    nevermind....
    du u have to login to make this changes or not?

  3. #3
    SitePoint Member
    Join Date
    Mar 2002
    Location
    Toronto
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can grab all the checked values from the form:

    if (count($fsection) >= 1) {
    foreach ($fsection as $value) {

    1) I need to INSERT into the table if the $value is NOT in the table
    (ADD a NEW checkbox (Category) $value for that ProdID).

    2) I need to DELETE from the table if the CatID is in the table,
    but is no longer a $value in the checkbox array (it has been UNCHECKED).
    (DELETE existing table CatID if there is not a corresponding $value from checkbox array).

    When the form is submitted, all the new values are added,
    and formerly checked values which are now UNCHECKED are deleted
    from the database table.

    The only thing looping right now is my brain.....

    Any suggestions are welcome....

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    when submit form
    PHP Code:
    $n count($_POST["checkboxes"]); // asume that u have fields the same number like checkboxes
    for($i 1$i <= $n; ++$i) { // start from 1 couse row 0 is id
    $val $_POST["checkboxes"][$i];
    $ret mysql_query("SELECT * FROM table");
    $row mysql_fetch_row($ret);
    if(empty(
    $row[$i])) {
    $retu mysql_query("UPDATE table SET field'$i' = '$val'");
    echo 
    "ok";
    } else {
    $retc mysql_query("DELETE FROM table WHERE field'$i' = '$row[$i]'");
    $retu mysql_query("UPDATE table SET field'$i' = '$val'");
    echo 
    "ok";
    }
    mysql_free_result($ret);

    i havent tested it but i hope u understand the concept
    cheers

  5. #5
    SitePoint Member
    Join Date
    Mar 2002
    Location
    Toronto
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks reminder....

    A slightly modified version of the code you supplied works great...

    BUT, there is one small problem....

    1 - Canadian Catalogue
    2 - USA Catalogue
    3 - 3 Day Sale
    4 - 2 for 1 Sale
    5 - Half Price Sale

    I can now check or uncheck items 2 thru 5 no problem,
    and the database is updated, but if I uncheck the FIRST checkbox,
    (if currently checked) and post the form, it remains checked.

    NOTE:

    It was not working with $i=1 in the "for loop", but it works
    fine (for items 2 thru 5) with $i=0.

    I have a hunch that the "for loop" is the culprit...?
    (or my brain loop....)

    This wasn't working:
    for ($i = 1; $i <= count($_POST["fsection"]); $i++) {


    This works for checkboxes 2 thru 5
    for ($i = 0; $i <= count($_POST["fsection"]); $i++) {


    Any suggestions on getting that first checkbox to work... ?

    Thanks!!!

  6. #6
    SitePoint Member
    Join Date
    Mar 2002
    Location
    Toronto
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    RESOLVED:

    See attached Zip file for solution:

    Zip includes:
    1) index.php - Change the username/password and database name at the top.
    2) SQL dump to create two tables (tbl_section and tbl_sectionlookup).
    3) Image icons for sections also included.
    Attached Files Attached Files


Bookmarks

Posting Permissions

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