Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20

Thread: Dynamically preselected checkboxes...?

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

    Default

    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.

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

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

    Code:
    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 we did it.

    Let us know.
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

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

    Default

    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

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

    Default

    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.
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

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

    Default

    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?

    Code:
    			<?
    	$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>";
     }
    ?>

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

    Default

    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
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

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

    Default

    the value does not have any quotes around it!
    Code:
    echo "<input type=\"checkbox\" name=\"size[]\" value=" .$roweusize['value_id']. ">";
    should be:
    Code:
    echo "<input type=\"checkbox\" name=\"size[]\" value=\"" .$roweusize['value_id']. "\">";
    [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.

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

    Default

    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:

    Code:
    			<?
    	//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:

    Code:
    <?
    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

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

    Default

    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
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

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

    Default

    Ok...thanks...this turned into a small challenge...which I like...
    Looking forward to learn some more!

    /Johan

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

    Default

    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.
    Code:
    <?
    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
    Code:
    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
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

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
  •