All pastes #938449 Raw Edit

Johan

public php v1 · immutable
·published 2008-03-11 17:33 UTC
rendered paste body
<?php// I'm assuming that dropdown 1_1 is the first one, 1_2 is the second one and so on.// subcat_1_1_1 is the first checkbox in the first dropdown in the first of the three sections.// subcat_1_1_2 is the second checkbox in that dropdown and so on.$categories = array();// Just a little trick to make multiple variables on a single line$section_exists = $category_exists = $subcategory_exists = true;// While the array-key exists, this loop will run and collect all checkboxes// in the array called categoriesfor($w=1;$section_exists==true;$w++){	if($!array_key_exists('subcat_'.$w.'_1_1', $_POST)	{		$section_exists = false;	}		for($x=1;$category_exists==true;$x++)	{		if($!array_key_exists('subcat_1_'.$x.'_1', $_POST)		{			$category_exists = false;		}			for($y=1;$subcategory_exists==true;$y++)		{			if(!array_key_exists('subcat_1_'.$x.'_'.$y, $_POST)			{					$exists=false;			}			if($POST['subcat_1_'.$x.'_'.$y] == 'checked')			{				$categories[$w][$x][] = $i;			}		}	}}// Now you have an array with all checkboxes, something like// $subcat[1][1][1] = The number of the first checked checkbox in the first category// $subcat[1][1][2] = The number of the second checked checkbox  in the first category and so on.// $subcat[1][2][1] = The number of the first checked checkbox in the second category// $subcat[1][2][2] = The number of the second checked checkbox in the second category and so on.// $subcat[2][1][1] = The number of the first checked checkbox in the first category, second section// $subcat[2][1][2] = The number of the second checked checkbox  in the first category, second section and so on.$key_exists = true;$subkey_exists = true;$strings = array();// Loops through and turns all categories' arrays into stringsfor($h=1;key_exists==true;$h++){	if(!array_key_exists('['.$h.'][1]', $categories)	{		$key_exists = false;	}		for($i=1;$subkey_exists==true;$i++)	{		if(!array_key_exists('['.$h.']['.$i.']', $categories)		{			$subkey_exists = false;		}		// This will turn the array into a string with all subcategories, separated by ;.		// For example, 0;3;53 if subcategory 0, 3 and 53 are checked		$strings[$h][$i] = implode(';', $categories[$h][$i]);	}}// Now strings[] contains all the strings (like 1;3;5 and 3;5;7). Loop through the array:for($x=0;$x<sizeof($strings);$x++){	for($y=0;$y<sizeof($strings[$x]);$y++)	{		// *build a query using the string*		// Echoing just for debugging or whatever.		echo "INSERT into categories ('category', 'subcategories') VALUES ('".$x."', '".$y."')";	}}// Done.// Store that field in the DB and then when you want them into an array again, do:// $array = exlode(';', $string);?>