Jump to content

More checkbox fun


squiggerz

Recommended Posts

I've read the 40 dozen posts on here dealing with checkboxes, either the replies dont apply to my situation or I'm just not grasping this concept yet (the latter probably).

 

<? if ($_GET['add'])
{
	$i = 0;
	$w = 0;
	echo '<table class="style3" align="right" width="90%" border="0" cellspacing="1"><tr>';	
  		while ($a_row = mysql_fetch_array ($a_results, MYSQL_ASSOC)) 
		{						
			$i++;
			$w++;

		echo '<td valign="middle" align="left" width="25%">';
		echo "<label><input type=\"checkbox\" name=\"".$a_row['category']."\" id=\"".$a_row['category']."\" value=\"".$a_row['category']."\"";

                        if($a_row['category'] == $ucat)
		      {
			   echo " checked >";
		      }
		else
	              {
			   echo ">";
		      }
			echo $a_row['category']."</label>";

			if($i % 1 == 0) 
				{ 
				echo '</td> <td valign="middle" align="left" width="25%" valign="top">';	
				}				

			if($w % 4 == 0) 
				{ 
					echo '</tr> <tr>'; 
				}
		}
	echo '</td></tr></table>';
} ?>

 

Ok, this loop goes through an array of categories from a categories table in my db, the table has 2 fields, id & category, id is autoincremented. It works correctly, naming the checkbox & giving it the value of the variable from the array like its supposed to.

 

When my form is submitted, how am I supposed to update my users table, which has a field 'category' that I want to have updated with all of the categories, I guess comma delimited.

 

Should I just make another loop like

if ($somevar)
    {
       while($_POST['WHATDOIPUTHERE?'] == $category)
           {
               //build my update query based on the $_POST[whatevergoeshere] var?
           }
    }

I guess my real question would be, how do I loop the $_POST array to check against the categories table?

 

I think... hope that made some sense to somebody.

 

SQ

 

Link to comment
Share on other sites

here's an example

 

<?php
if (isset($_POST['sub']))
{
    foreach ($_POST['cat'] as $catid)
    {
        echo $catid , '<br>';
        // insert catid into table
    }
    
    // NOT recommended, but if you must have comma-sep list
    // instead of writing them to a separate table

    $cats = join (',', $_POST['cat']);
    echo $cats;
}
?>
<form method='post'>
<input type="checkbox" name="cat[]" value="1">cat 1  <br>
<input type="checkbox" name="cat[]" value="2">cat 2  <br>
<input type="checkbox" name="cat[]" value="3">cat 3  <br>
<input type="checkbox" name="cat[]" value="4">cat 4  <br>
<input type="checkbox" name="cat[]" value="5">cat 5  <br>
<input type="submit" name="sub" value="Submit">
<form>

Link to comment
Share on other sites

I know this was solved but I figured it would be better to 'unsolve' this than start a new thread.

 

    // NOT recommended, but if you must have comma-sep list

    // instead of writing them to a separate table

 

 

^^ I now see the flaw of not just starting a new table, but how would you go about something like that? I mean  I know how to set up a table, but would I just make a table that has the customers name as a field and then have 100 category fields and have them as flag 1 or 0 fields??

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.