Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Dynamically preselected checkboxes...?

  1. #1
    Join Date
    Mar 2008
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Dynamically preselected checkboxes...?

    Dear all,

    I have given this some thoughts but I'm stuck and need to call in the heavy cavaliry...in other words...please help...

    My objective is to display a table with two columns in a page that will allow the user to update the information for a specific product. The table have one coloumn with the name/value and one coloum with the checkboxes. The checkboxes for the value in the database should be preselected.

    The name/value and the number of checkboxes are fetched from a table:

    ---Table attribute_values---
    value_id (PK)
    attrib_id
    attrib_value

    example:Table attribute_values
    value_id=1
    attrib_id=1 (1=size in another table)
    attrib_value=37 (size 37)

    I have another table that connects products to the attributes:
    ---Table product_attributes---
    value_id=1
    username=admin
    product_id=104

    In this example I have a product that have Size 37 as an attribute and I would like the checkbox for Size 37 to be preselected in the update page for the product.

    I choose the product to update in a previous page and get the product_id for this product via GET-method and transfer that ID into a variable in the update-page.

    My question to all you intelligent people out there is: How do I accomplish the pre-selected checkboxes? I know that I should use selected="Selected" but should I do a while-loop...or...??? An example would be nice...

    I'm grateful for all input.

    Thank you,

    //Johan Beijar

  2. #2
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default

    For starters, you just have questions coming out of your ears dont you lol

    Anyway checkboxes use "checked" ie
    Code:
    <input type="checkbox" name="mybox" value="1" checked>one
    <input type="checkbox" name="mybox" value="2" checked>two
    You can have as many or as little as you want checked.

    So in your PHP you would have
    Code:
    <form>
    while($row = mysql_fetch_assoc($results) {    
        if($row['attrib_value'] == "37") {
             <input type="checkbox" name="whatever" value="37???" checked>name
        }
        <input type="checkbox" name="whatever" value="36">name
    }
    </form>
    Where it says value you might want it to be different so you might want to retrieve it from the database and you know which 1 to get.
    Also where it says name, you'll probably want it to be something from the database.

    Someone will probably be able to do it better for you.

  3. #3
    Join Date
    Mar 2008
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question

    Hi,

    Yes, I'm a newbie...and without asking you never learn...:-)
    (I truely appriciate the help!)

    Thank you for the input.

    I have a form that looks like this:


    The checkboxes are dynamicly created dependent on user and what attributes the user has created.

    The user can in a different page choose to upd a specific product and will then come a similar page but with the information filled in and checkboxes selected.
    The database only holds information about which sizes that are connected to each product. Eg: product 1 cn have sizes 41,42,43,44,45. I still need all the sizes from 16-46 to be displayed in the upd-page so the user can chage available sizes from 41-45 to 40-44.

    I would like get value_id and attrib_value for the specific product ($produpd) where the productID=$produpd and only for the EUsize (attribute_values.attrib_id = 1) and sizes between 31 and 46.

    I have modified your code a bit...what do you say about...
    $produpd=id of the product to be updated.

    <?
    $resulteusize = mysql_query
    ("SELECT
    attribute_values.value_id,
    attribute_values.attrib_value
    FROM
    attribute_values,
    product_attributes
    WHERE
    product_attributes.productid = ".$produpd." and
    attribute_values.attrib_id = 1 and
    attribute_values.attrib_value BETWEEN 31 and 46
    ORDER BY
    attribute_values.attrib_value")
    or die(mysql_error());
    echo "<table border='1' bordercolor='#cccccc' cellspacing='0' cellpadding='1'>";
    echo "<tr bgcolor='#cccccc'>
    <td colspan=\"2\" align=\"center\">EU Size</td>";

    while($row = mysql_fetch_assoc($resulteusize)) {
    if(isset ($row['attrib_value'])) {
    echo "<tr><td>";
    echo "<input type=\"checkbox\" name=\"size[]\" value=" .$roweusize[value_id]. " checked>";
    echo "</td>";
    echo "<td>";
    echo $roweusize['attrib_value'];
    echo "</td></tr>";
    }
    echo "<tr><td>";
    echo "<input type=\"checkbox\" name=\"size[]\" value=" .$roweusize[value_id]. ">";
    echo "</td>";
    echo "<td>";
    echo $roweusize['attrib_value'];
    echo "</td></tr>";
    }
    ?>

    I still wonder around in the dark...

    //Johan Beijar

  4. #4
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default

    Of course as the great man himself says "The important thing is not to stop questioning. Curiosity has its own reason for existing." Albert Einstein :P

    Anyway back to reality...

    Have you done the mysql query in your mysql database? Just to check that you are getting some results and that they are the right ones.

    in your form, i wouldnt recommend naming your checkbox size[] or more to the point, i wouldnt recommend the '[]' it will just prove complicated and if the php throws an error later it just rules out one thing.

  5. #5
    Join Date
    Mar 2008
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Hi,

    I will doublecheck the query again.

    Is it correct of me to use if(isset ($row['attrib_value'])) to check if there is a value in attrib_value. If there is a value it should print a pre-checked checkbox, if no value it should print a unselected checkbox?

    Ok, I will try without [].

    Thanks,

    BR Johan Beijar

  6. #6
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default

    There is no point in doing the if(isset($row['attrib_value'])). If you've already got to the point of getting into the while loop i.e. there are some results, so then there is always gona be some data, unless of course if the field is null(empty).

    If there is nothing in the database then it will just build the checkbox with no value, no name next to it and not checked.

    Let us know if the mysql works and we'll look into it some more if not.
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

  7. #7
    Join Date
    Mar 2008
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Ok, I have removed if(isset($row['attrib_value'])) and replaced it with the code below. The SQL query is confirmed and working.

    When executing the code I get the following result as in the screenshot. It is one step closer to the truth...:-)
    What I although need is a table that looks like the one below the checked checkboxes, the one with sizes from 30-47 AND pre-checked checkboxes for values as the top table 41-46 in the same table.
    I need to display all the 17 checkboxes (size 30-47) and then check the checkboxes applicable for this specific product...

    I can not for my life figure out a SQL-statement for that...
    I alltered your code a bit to
    Code:
    if($roweusize['attrib_value'] == "")
    How should I do this...?

    current status:


    Code:
    <?
    	$resulteusize = mysql_query 
    	("SELECT 
    	attribute_values.value_id, 
    	attribute_values.attrib_value 
    	FROM 
    	attribute_values, 
    	product_attributes 
    	WHERE
    	username='$session->username' and
    	attribute_values.value_id = product_attributes.value_id and
    	product_attributes.productid = ".$produpd." and
    	attribute_values.attrib_id = 1 and
    	attribute_values.attrib_value BETWEEN 30 and 46
    	ORDER BY 
    	attribute_values.attrib_value") 
    	or die(mysql_error());
    echo "<table border='1' bordercolor='#cccccc' cellspacing='0' cellpadding='1'>";
    echo "<tr bgcolor='#cccccc'>
    <td colspan=\"2\" align=\"center\">EU Size</td>";
    	while($roweusize = mysql_fetch_assoc($resulteusize)) {    
        	 if($roweusize['attrib_value'] == ""){
         echo "<tr><td>"; 
    		 echo "<input type=\"checkbox\" name=\"size[]\" value=" .$roweusize[value_id]. ">";
         echo "</td>"; 
         echo "<td>"; 
         echo $roweusize['attrib_value'];
         echo "</td></tr>";
    }
       echo "<tr><td>"; 
    		 echo "<input type=\"checkbox\" name=\"size[]\" value=" .$roweusize[value_id]. " checked>";
         echo "</td>"; 
         echo "<td>"; 
         echo $roweusize['attrib_value'];
         echo "</td></tr>";
     }
    ?>
    Thanks in advance...

    /Johan

  8. #8
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    try checked="checked" (more friendly to browsers like firefox)
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  9. #9
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default

    Ok so i think i finally understand how you want this to work...sort of.

    I dont get the 41-46 part though?! But that bit you havent asked about so its not a problem.

    Should your query not be the same as your query for the 41-46 checkboxes? Or is that just html?

    To do your query i could do seeing your tables with some data in them that are linked together. that way i know what im looking for(im only 20 and still learning )

    As far as your PHP is concerned it looks ok. 1 thing i would do is if generally there are less ticked boxes then i would put that version (checked) in the if statement. If you get me, but its not gona make any difference.

    Try the suggestion of checked=checked. Might help when it comes to working, duno. If you show me some database entries and the tables then il be able to have a look in work and get it to work.

    Note: You havent changed the name of the checkboxes, dont know if you have already, i just noticed that they still had [], but that isnt gona make a difference to it 'til later.
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

  10. #10
    Join Date
    Mar 2008
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    The objective is to have one table displaying sizes from 30-47. If the user have defined earlier that the product is available in the sizes 40-46 these values should be prechecked in the table.

    The structure of the DB for this part is as follows;

    Tablename: **attributes**
    Field: attrib_id* (PK)
    Field: attrib_name

    Example:
    attrib_id= 1
    attrib_name= Eusize

    **attribute_values**
    value_id* (PK)
    attrib_id
    attrib_value

    Example:
    value_id=1
    attrib_id=1
    attrib_value=37

    Comment: attrib_id is connected to the attributes-table.
    attrib_value contains the all the possible sizes (30-47).

    **product_attributes**
    productid
    value_id

    Example:
    productid=41
    value_id=1

    Comment: This table connects the product with the attribute. The example says that the product with ID=41 and value_id=1 have an attribute (size) that equals size 37. The table product_attributes have no primary key so the table is populated with many instances of productid=41.

    I need to do two things at the same time...
    1. select all possible eusizes (attrib_id= 1) which will return: 30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47
    2. precheck the checkboxes for the available sizes for a specific product.

    Any suggestions...?

    //Johan

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
  •