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
https://forums.phpfreaks.com/topic/63073-more-checkbox-fun/
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
https://forums.phpfreaks.com/topic/63073-more-checkbox-fun/#findComment-314307
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
https://forums.phpfreaks.com/topic/63073-more-checkbox-fun/#findComment-314888
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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