Results 1 to 4 of 4

Thread: Checkbox = checked depending on varying DB value (PHP).

  1. #1
    Flak Monkey! Dorza's Avatar
    Join Date
    Jul 2003
    Location
    UK - South Wales
    Posts
    1,762
    Thanks
    34
    Thanked
    17 times in 15 posts
    • Dorza's system
      • Motherboard:
      • Asus P5B Deluxe - WiFi
      • CPU:
      • Q6600 @ 3.06Ghz
      • Memory:
      • 2GB Crucial
      • Storage:
      • 500GB Samsung SpinPoint
      • Graphics card(s):
      • Geforce 9600GT
      • PSU:
      • Cosair HX520W
      • Case:
      • LianLi something something or other
      • Monitor(s):
      • Eizo FlexScan S1910 (1280*1024)
      • Internet:
      • 2mb Virgin (when they want to give me that: else 1mb)

    Checkbox = checked depending on varying DB value (PHP).

    This will take some explaining so please bear with me...

    To begin with there is a form that allows the user to post new information/articles to the public end of a website. Part of the form has a "Post to" section which allows the user to post a single bit of information to multiple parts or sections of a website.

    This part of the form has two checkboxes, namely "Home" and "Gaming". So for arguments sake we had an article with the title "MS unifies Windows and Xbox gamers online" the user might want to post that information to both the "Home" and "Gaming" sections of the site, so they decide to tick both the checkboxes.

    The structure of each checkbox looks like so:

    Code:
    The variables are had from records held in a "post Location" table 
    which holds the information on what parts of the site the user can post 
    information to e.g below:
    
    $postId could be "1"
    $postLocation could be "Home"
    
    <input type=\"checkbox\" name=\"postTo[]\" value=\"$postId\" />$postLocation
    Upon submitting, each checkbox that is ticked, has its value inserted into the table responsible for retrieving the information/article from the database. If both are checked then obviously two records are made and either one depending on its Post To value is displayed on the corresponding section of the site. This works fine, the problem comes when retrieving the information from the database when the user (any admins) needs to edit the entry.

    What I want to happen is when the information/article is chosen to be edited the user sees what "Post To" checkboxes are currently ticked. The idea is that if the user made the mistake of posting information to the gaming section of a site instead of the home page they can go in, un-tick the gaming checkbox and update record and then that record will no longer be viewable via the gaming section of the site.

    What I can't do 100&#37; successfully is retrieve the data from the database and show what checkboxes are ticked. I can semi do it but not 100%. I have tried all sorts over the last week to get this working but I can't find a solution to it.

    Below is the pure and simple crux of what I am trying to do. I know it does not work, is lacking security anderror capture features or any proper working structure, it is just to show you what it is I am trying to do.

    Code:
    TABLE STRUCTURE:
    --------------------------------
    Table: post_to_tab
    
    ptd_id : ptd_location
    1        : Home
    2        : Gaming
    --------------------------------
    
    Table: articleData_tab
    
    postTo_id : article_id
    1        : 23
    2        : 23
    --------------------------------
    Note: the article_id corresponds to the id of the actual textual 
    information which is stored in another table which in this scenario 
    is not important. There are other fields in the articleData_tab, but 
    I am concentrating on the relevant parts.
    The part that does the business of doing what I want to do:
    Code:
    $aId = $_GET['aId']    //e.g. page.php?aId=23
    
    $getPostToData = @mysql_query("SELECT ptd_id, ptd_location FROM post_to_tab")
    
    $getArticleData = @mysql_query("postTo_id FROM articleData_tab WHERE article_id='$aId'")
    
    while ($foundPTD = mysql_fetch_array($getPostToData) && $foundGAD = mysql_fetch_array($getArticleData))
    {
        $ptd_id = $foundPTD['ptd_id'];
        $ptd_loc = $foundPTD['ptd_location'];
    
        $postTo_id = $foundGAD['postTo_id'];
    
        if($ptd_id == $postTo_id)
        {
           echo "<input type=\"checkbox\" name=\"postTo[]\"  
                            value=\"$postTo_id\" checked="checked\" />$ptd_loc\n";
        }
        else
        {
           echo "<input type=\"checkbox\" name=\"postTo[]\"  
                            value=\"$postTo_id\" />$ptd_loc\n";
        }
    
    }
    I know it's a lot to take in and understand someone elses work, but I was just wondering if any of you knew what it was I was trying to do and if you could guide me in the right direction. I appreciate any efforts towards reading and giving input on this problem I seem to have.
    Last edited by Dorza; 11-09-2007 at 10:01 PM.

  2. #2
    Senior Member ajbrun's Avatar
    Join Date
    Apr 2004
    Location
    York, England
    Posts
    4,840
    Thanks
    4
    Thanked
    25 times in 13 posts

    Re: Checkbox = checked depending on varying DB value (PHP).

    Forgive me if I get the wrong end of the stick - it's late, so I probably haven't properly read it correctly. As I understand it, you want a checkbox to either default to checked or unchecked depending on the result of a database query. Can you not use a PHP if statement, and if the database result needs a checked checkbox, use the following code:

    <INPUT TYPE=CHECKBOX NAME="maillist" CHECKED>

  3. #3
    www.dougmcdonald.co.uk
    Join Date
    May 2007
    Location
    Bath
    Posts
    523
    Thanks
    5
    Thanked
    20 times in 20 posts
    • DougMcDonald's system
      • Motherboard:
      • Asus P5B Deluxe
      • CPU:
      • Inter Core 2 Duo E6600
      • Memory:
      • 2 x 2GB - Geil Black Dragon PC6400
      • Storage:
      • 2 x 400GB Samsung Spinpoints (Running in Matrix array) 100GB @ RAID0 + 300GB @ RAID1
      • Graphics card(s):
      • BFG nVidia 8800GTS 320MB OC2
      • PSU:
      • Corsair HX520W modular
      • Case:
      • Lian-Li PC7 II Plus
      • Monitor(s):
      • LG 17" Flat Thingy
      • Internet:
      • Crappy BT 1MB Unreliable wank :s

    Re: Checkbox = checked depending on varying DB value (PHP).

    Hmm, code looks sensible, I think all you're missing is an instruction in your while loop to tell things to move to the next row returned from the query.

    I would assume when you say it works partially, you mean if row one is not selected in the db, it returns both as not selected, and if it is, both are returned select.

    Im not sure quite how you do this with the arrays youve created, but I would do something like this:

    PHP Code:
    //SETUP QUERY STUFF
    $query = ("SELECT field_1,field_2 from table_a");
    $result mysql_query($query);
    //I TEND TO USE FETCH_ASSOC HENCE NO EXACT SYNTAX
    $data mysql_fetch_assoc($result);

    //WHILE A FIELD YOU'RE RETURNING STILL HAS A VALUE, IN YOUR EXAMPLE MAYBE USE THE LOOKUP TABLE SINCE IT WILL HAVE THE NUMBER OF ROWS YOU WANT TO DISPLAY CHECKBOXES FOR
    while ($data[field_1] != 0) {

    //INSERT YOUR CODE FOR CHECKBOXES HERE
    checkbox blah, if this do that etc


    //THEN MOVE TO NEXT ROW
    $data mysql_fetch_assoc($result);

    There is probably some simple command to tell arrays to move to next row, often it's something like movenext() have a look around.

    I think that's all that's missing, the array is not going to row 2 (or array index 1, depending on how you look at it)

    Hope it helps

  4. #4
    Flak Monkey! Dorza's Avatar
    Join Date
    Jul 2003
    Location
    UK - South Wales
    Posts
    1,762
    Thanks
    34
    Thanked
    17 times in 15 posts
    • Dorza's system
      • Motherboard:
      • Asus P5B Deluxe - WiFi
      • CPU:
      • Q6600 @ 3.06Ghz
      • Memory:
      • 2GB Crucial
      • Storage:
      • 500GB Samsung SpinPoint
      • Graphics card(s):
      • Geforce 9600GT
      • PSU:
      • Cosair HX520W
      • Case:
      • LianLi something something or other
      • Monitor(s):
      • Eizo FlexScan S1910 (1280*1024)
      • Internet:
      • 2mb Virgin (when they want to give me that: else 1mb)

    Re: Checkbox = checked depending on varying DB value (PHP).

    yes thats just it. I can get the first checkbox to be checked, or both or none, but if the second checkbox is checked and the first one isn't, then the first checkbox doesn't even appear. I will take a close look at this later when I get back to it. Thanks for your suggestion.

    @ajbrun...

    Thanks for that. I have used an if statement to see if the checkbox should be checked or not but it does not work, since as Doug pointed out it, doesn't seem to move onto the next record in some situations.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. MySQL extension for PHP 5.2.3 not working
    By Jerrythafast in forum Help! Quick Relief From Tech Headaches
    Replies: 18
    Last Post: 13-06-2007, 08:03 PM
  2. PHP and file uploads timing out too soon
    By McClane in forum Software
    Replies: 12
    Last Post: 02-12-2006, 05:57 PM

Posting Permissions

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