Johan Beijar Posted June 22, 2008 Share Posted June 22, 2008 Dear all, I would truly appriciate help in this matter... Background: I building an application that let the user add or change attributes for a specific product via checkboxes. A product can have an attribute called "colour" which have values like "Brown", "Blue", "Red" etc... The user then checks which of these attributes that is valid for the specific product. A product can have many different colours. Problem: When updating the attributes for a specific product some of the checkboxes should already be checked. The problem is that I only the the first value for the selectec product to be check. Eg, the product has 5 colours: Red, Green, Blue, Black and White. The user have previvously stated that this specific product is available in: Red, Green, Blue. He had then checked those three colours. When the user later on should update the product specification so that the product is also available in Black all three of the previously colours should be pre-checked. Non working solution: //**********Start*********** //First Query tot get all available colours for the product $resultcol = mysql_query (" SELECT value_id, attrib_value FROM attribute_values WHERE attrib_id=2 and username='$session->username' ORDER BY value_id") or die(mysql_error()); echo "<table border='1' bordercolor='#cccccc' cellspacing='0' cellpadding='1'>"; echo "<tr bgcolor='#cccccc'> <td colspan=\"2\" align=\"center\">Colour(s)</td>"; //Get the value_id for the choosen colour and compare the two arrays via in_array. $resultcolour= mysql_query ("SELECT attribute_values.value_id, attribute_values.attrib_value FROM attribute_values, product_attributes WHERE product_attributes.value_id = attribute_values.value_id and attribute_values.attrib_id=2 and product_attributes.productid= ".$produpd." ORDER BY attribute_values.value_id") or die(mysql_error()); $arraycolour = mysql_fetch_array( $resultcolour ); while($row = mysql_fetch_array( $resultcol )) // put the result into an array { // dispaly the left coloumn of colours echo "<tr><td>"; if (in_array($arraycolour['value_id'],$row)) { echo "<input type=\"checkbox\" name=\"colour[]\" value=" .$row['value_id']. " CHECKED>"; } else { echo "<input type=\"checkbox\" name=\"colour[]\" value=" .$row['value_id']. ">"; } echo "</td>"; echo "<td>"; echo $row['attrib_value']; echo "</td></tr>"; } //***********END************** As you see I', trying to use in_array and this setup works well with radiobutton where I only can have one pre-checked option but not with checkboxes... I have veryfied that both queries returns the expected results and they both do. Any help is very appriacited, either in getting this option to work or to how me another way of doing the same thing... Thank you in advanced, //Johan Beijar Link to comment https://forums.phpfreaks.com/topic/111362-checked-checkboxes/ Share on other sites More sharing options...
darkhappy Posted June 23, 2008 Share Posted June 23, 2008 I am not really understanding your question. Why can't you just add "checked" to the input tag for boxes you want checked? You may need to change your if statement so that the boxes you want to show as checked are not getting created by the line after the else, which does not include "checked" in the input tag. Link to comment https://forums.phpfreaks.com/topic/111362-checked-checkboxes/#findComment-571862 Share on other sites More sharing options...
redarrow Posted June 23, 2008 Share Posted June 23, 2008 Try this for me please cheers.... <?php $x=implode(' ',$color); if($x=="CHECKED"){ echo " The box CHECKED was >>$x<<"; ?> Link to comment https://forums.phpfreaks.com/topic/111362-checked-checkboxes/#findComment-571885 Share on other sites More sharing options...
Johan Beijar Posted June 23, 2008 Author Share Posted June 23, 2008 Hi, Thank you for the replies. To answer the question: "Why can't you just add "checked" to the input tag for boxes you want checked?" Answer: Which checkboxes that should be pre-checked are dependent on the content of the database. In another words do I not know which checkboxes the user had previously checked. I use this code to determine if the checkbox should be checked or not. If $arraycolour['value_id'] exists in array $row it should be pre-checked. Else it should not be pre-checked. This code do only pre-check the **first** of the pre-checked checkboxes. If the checkboxes for Blue, Red and Grey should be pre-check it only pre-check the Blue option and not the remaining red and grey. if (in_array($arraycolour['value_id'],$row)) { echo "<input type=\"checkbox\" name=\"colour[]\" value=" .$row['value_id']. " CHECKED>"; } else { echo "<input type=\"checkbox\" name=\"colour[]\" value=" .$row['value_id']. ">"; } Once again, I appriciate all help in this matter. Thank you in advance. //Johan Beijar Link to comment https://forums.phpfreaks.com/topic/111362-checked-checkboxes/#findComment-572107 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.