Log in

View Full Version : Dynamically preselected checkboxes...?



Johan Beijar
03-10-2008, 01:31 PM
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

city_coder
03-10-2008, 02:11 PM
For starters, you just have questions coming out of your ears dont you lol

Anyway checkboxes use "checked" ie


<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


<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.

Johan Beijar
03-10-2008, 09:34 PM
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:
http://www.project-wiz.com/addprod.jpg

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

city_coder
03-10-2008, 10:16 PM
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.

Johan Beijar
03-11-2008, 08:53 AM
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

city_coder
03-11-2008, 01:51 PM
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.

Johan Beijar
03-11-2008, 08:35 PM
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
if($roweusize['attrib_value'] == "")

How should I do this...?

current status:
http://www.project-wiz.com/addprod2.jpg


<?
$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

Master_script_maker
03-11-2008, 08:38 PM
try checked="checked" (more friendly to browsers like firefox)

city_coder
03-11-2008, 09:49 PM
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 :D )

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.

Johan Beijar
03-12-2008, 02:11 PM
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

city_coder
03-12-2008, 03:13 PM
Iv just played around with it a bit and if this is the answer then your probably gona kick yourself for it :P

You'll notice that the following bit code does not have any single quotes around the field name.



echo "<input type=\"checkbox\" name=\"size[]\" value=" .$roweusize[value_id]. " checked>";


It should be



echo "<input type=\"checkbox\" name=\"size[]\" value=" .$roweusize['value_id']. " checked>";


Iv just been doing it in work and its only just occured to me once i put it in dreamweaver and saw some differences.


You also do it on this line. Iv added the commas just.



echo "<input type=\"checkbox\" name=\"size[]\" value=" .$roweusize['value_id']. ">";


If thats not it, then oops, but at least it will make a difference. If it is then great :D we did it.

Let us know.

Johan Beijar
03-12-2008, 05:13 PM
Thanks, but there are no difference using ' or not. I have added it to the code with the same result. No the problem is within the SQL-query.
The result of the query is:

value_id attrib_value
42 41
43 42
44 43
45 44
46 45
47 46

and this is good for displaying the checked checkboxes but I need to display the full table with sizes from 30-47 and also precheck the attrib_values stated above in *the same* table.

Ok...it is small problem compared to hunger in africa but a big problem for me...

Any ideas...?


/Johan

city_coder
03-12-2008, 05:44 PM
Right ok, well it should make a difference, cos it did on my mock version.
Right so you have a single cell that has all of the sizes that are applicable to that product_id? ie

Value_id Attrib_id Attrib_value
1 1 30,31,32,33,34,35,36 //through to 47


Is that right? If so then its just simply taking that field and splitting it at each point to put into an array and then putting in on the loop. It'll be easier to use a for loop to keep things tidier.

Johan Beijar
03-12-2008, 06:03 PM
hmm...

The IF-statement says that: IF nothing then do a un-checked checkbox else do a checked checkbox. Yes, that is correct but I only have checked values in the array so it will never print a unchecked checkbox....only checked.

I need somehow to select all sizes (30-47) and the sizes available for the specific product. The SQL-statement I have now only selects the available sizes for the specific product.

Should I do another array with all the sizes and the compare them somehow?


<?
$resulteusize = mysql_query
("SELECT
attribute_values.value_id,
attribute_values.attrib_value
FROM
attribute_values,
product_attributes
WHERE
'$session->username'= product_attributes.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>";
}
?>

city_coder
03-12-2008, 08:15 PM
Ok well in that case then you'll need to change the database slightly. So if say a product has all of the sizes available for it, it will print all of the checkboxes, but if it doesnt then it will only print those that are available.

You'll need to put in the field that holds the sizes the missing spaces, regardless of whether they are available or not. put a ,"", in i think.

so for example it would be.

30,31,32,"","",35,"",37,38,39,40,"",42,43,44,45,""

I think that might work

Master_script_maker
03-12-2008, 09:51 PM
the value does not have any quotes around it!
echo "<input type=\"checkbox\" name=\"size[]\" value=" .$roweusize['value_id']. ">";
should be:
echo "<input type=\"checkbox\" name=\"size[]\" value=\"" .$roweusize['value_id']. "\">";

Johan Beijar
03-13-2008, 06:24 PM
Ok...yes...I think that is a good idea that you have to put a "" in the BD for the sizes not checked...then next questions comes...:-)
How do I do that in a good way...

The code for the input form is:


<?
//Get all the EU-sizes from the attrib_value table attrib_id=size
$resulteusize = mysql_query ("SELECT value_id, attrib_value FROM attribute_values WHERE attrib_id=1 and attrib_value between 16 and 30 ORDER BY 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>";
// put the result into an array
while($roweusize = mysql_fetch_array( $resulteusize )) {
// dispaly the rows of sizes
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 "</table>";
?>

The code for the processing side is:


<?
include("include/session.php");
$strQuery=mysql_query("
INSERT INTO products
(brandid, username, productname, productnumber, producttype, gender, currency, price, recpriceretail, prodpic, mancountry, productdescription,cat_brand_id)
Values
(
'" . $_POST['Brand'] . "',
'$session->username',
'" . $_POST['Productname'] . "',
'" . $_POST['Productnumber'] . "',
'" . $_POST['Prodtype'] . "',
'" . $_POST['Gender'] . "',
'" . $_POST['Currency'] . "',
" . $_POST['Price'] . ",
" . $_POST['Retailprice'] . ",
'" . $_POST['Prodpic'] . "',
'" . $_POST['Mancountry'] . "',
'" . $_POST['Proddescription'] . "',
" . $_POST['Prodcat'] . "
)") or
exit(mysql_error());

How should input a "" or a 0 in the database?

/Johan

city_coder
03-13-2008, 08:12 PM
Right yeah, i think iv got the idea so il look into it tomorrow at work and then il hopefully be able to do it. I think its just gona be a case of comparing it to an array and then writing it to another array while you loop round and check them against each other..i think.

Anyway il look tomorrow and hopefully il have an answer :D

Johan Beijar
03-14-2008, 08:43 AM
Ok...thanks...this turned into a small challenge...which I like...
Looking forward to learn some more!

/Johan

city_coder
03-14-2008, 05:04 PM
Right then, i thinks me got it...like i said 'think'

You want to put the sizes into the DB right? So that when you go to insert you get for example 30,31,0,33,0,0,36 etc to 46

this should be what you want.


<?
error_reporting(E_ALL);
$orig = $_POST['size'];
//$orig = array(30,31,32,34,35,37,38,39,41,42,43,44,45); //list of entries user has entered - example
$cmp = array(30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46);
$new = array();
//$cnew = array(30,31,32,0,34,35,0,37,38,39,0,41,42,43,44,45,0); - how it should look when its outputted
$count = count($orig);
if($count == 17) {
//do insert because 17 records exist and thats how many we should have
} else {
//got to edit array
for($i = 0; $i < 17; $i++) {
if($cmp[$i] == $orig[$i]) {
array_push($new, $cmp[$i]);
} else {
for($x = $i; $x < 17; $x++) {
if(count($new) == $x) {
$remain = 17 - $x;
for($x = 0; $x < $remain; $x++) {
array_push($new, "0");
}
} else {
echo $i.' ';
echo $orig[$x].' '.$cmp[$i].'<br>';
if($orig[$i] == $cmp[$x]) {
array_push($new, $cmp[$x]);
$i++;
} else {
array_push($new, "0");
}
}
}
}
}
}
//INSERT INTO sql goes here
/*echo '<br>';
foreach($new as $value) {
echo $value.',';
}
echo '<br>';
foreach($cnew as $value) {
echo $value.',';
}*/
?>


Right so basically what it does is checks each array position and compared to a standard 1, if its not right then it will add a zero until it finds the right 1.

There is a part of code that i havent checked as iv just thought of it on the way home


if(count($new) == $x) {
$remain = 17 - $x;
for($x = 0; $x < $remain; $x++) {
array_push($new, "0");
}
} else {

That if statement part wasnt there, something like it(if it doesnt work) needs to be there
If that bit doesnt work then sorry you'll have to wait til monday lol

Hope thats helped:D