Jump to content

Recommended Posts

I have made a class to use with categorizing content.

<?php
class category {
	function generate($array, $subcat=false, $se='.'){
		$n = count($array);
		$c = array();
		if($subcat === true){
			$i = 0;
			foreach($array as $category){
				$array[$i] = array_fill(0, $n, explode($se, $category));
				++$i;
			}
			array_unique($array);
		}
		for($i = 0; $i < $n; ++$i)
			$c[pow(2, $i)] = ($subcat === true) ? $array[$i][1] : $array[$i];
		return $c;
	}

	function check($num, $category){
		if($num & $category) return 'CHECKED';
		else return '';
	}

	function checkboxes($array, $numval=''){
		$form = '<table border="0" width="500px">';
		$categories = $this->generate($array);
		$i = 0;
		foreach($categories as $key => $val){
			$checked = $this->check($numval, $key);
			if($i%2==0) $form .= '<tr><td><input type="checkbox" ' . $checked . ' name="c' . $key . '" id="c' . $key . '">' . '<label for="c' . $key . '"> ' . $val . '</label></td>';
			else $form .= '<td><input type="checkbox" ' . $checked . ' name="c' . $key . '" id="c' . $key . '">' . '<label for="c' . $key . '"> ' . $val . '</label></td></tr>';
			if($i%2!=0) $form .= '</tr>';
			++$i;
		}
		$form .= '</table>';
		return $form;
	}
}
$category = new category;
?>

 

With the way it's set up now, you insert an array, which has its Index numbers converted to a base-2 system using the generate() function. This is then rotated through a foreach loop which creates a form.

 

<?php
$categories = array('Audio', 'Video', 'Freeware', 'Open-Source')
echo $category->checkboxes($categories);
?>

 

Would output something similar to this:

[ ] Audio          [ ] Video
[ ] Freeware       [ ] Open-Source

 

But I'm trying to accomplish subcategories like so:

<?php
$categories = array('Media.Audio', 'Media.Video', 'Software.Freeware', 'Software.Open-Source')
echo $category->checkboxes($categories, true, '.');
?>

 

Which I want it to output something along the lines of this:

Media              Software
[ ] Audio         [ ] Freeware
[ ] Video         [ ] Open-Source
------ or this -------
Media
[ ] Audio         [ ] Video
Software
[ ] Freeware      [ ] Open-Source

 

I know there must be some way to accomplish this. I'm curious if anyone can give me some input/help because this has become a considerable obstacle for me.

Link to comment
https://forums.phpfreaks.com/topic/162686-subcategories-for-checkbox-forms/
Share on other sites

Just a quick thought, but would it not be easier to do

$categories = array('Media' => array('Audio','Video'), 'Software'=>array('Freeware', 'Open-Source') )

then have

      function checkboxes($mainarray, $numval=''){
         $form = '<table border="0" width="500px">';
         foreach($mainarray as $mk=> $array)
         {
                  echo $mk; //example
                  $categories = $this->generate($array);
                  //....

 

EDIT: added example for category name

Ahh that works quite well, MadTechie. The code is isn't as bloatful as it was before either! Thank you very much. There is only one downside to this method and that would be that the category-IDs for the checkboxes are repeated.

 

An example:

<?php
$c = array('Media' => array('Audio', 'Video'), 'Software'=> array('Freeware', 'Open-Source'));

echo $category->checkboxes($c, true, 3);
?>

 

This would result in:

Media
   [X] Audio         [X] Video
Software
   [X] Freeware      [X] Open-Source

 

Instead of:

Media
   [X] Audio         [X] Video
Software
   [ ] Freeware      [ ] Open-Source

 

Inserting a 3 into the function should check the first two boxes because the sum of the first two ID-Numbers is 3. It does do this, but it does it for every category-set. So, I just need to find a fix for this slight downside.

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.