Jump to content

Recursive function and arrays.


ram4nd

Recommended Posts

Hey

assuming you have an open database connection, the code below might be suitable:

class recursive_scan
{
public $item_count = 0;
function scan( $parent_cat_id = 0 )
{
	$result = mysql_query('SELECT * FROM `cats` WHERE `parent_id` = "' . $parent_cat_id . '"');
	if ( mysql_num_rows($result) > 0 )
	{
		$item_count = $item_count + mysql_num_rows($result);
		while ( $row = mysql_fetch_assoc($result) )
		{
			$this->scan( $row['id'] );
		}
	}
}
function get_item_count()
{
	return $this->item_count;
}
}

$rs = new recursive_scan;
$rs->scan();

var_dump( $rs->get_item_count() );

 

I have tested it but it should work  :D

Hey

assuming you have an open database connection, the code below might be suitable:

class recursive_scan
{
public $item_count = 0;
function scan( $parent_cat_id = 0 )
{
	$result = mysql_query('SELECT * FROM `cats` WHERE `parent_id` = "' . $parent_cat_id . '"');
	if ( mysql_num_rows($result) > 0 )
	{
		$item_count = $item_count + mysql_num_rows($result);
		while ( $row = mysql_fetch_assoc($result) )
		{
			$this->scan( $row['id'] );
		}
	}
}
function get_item_count()
{
	return $this->item_count;
}
}

$rs = new recursive_scan;
$rs->scan();

var_dump( $rs->get_item_count() );

 

I have tested it but it should work  :D

Thanks, didn't think about putting it into class. I use codeigniter, i'll put the sql and other stuff like they have to be myself.

I'd do it with just one query, no need for recursion:

SELECT COUNT(id) FROM categories WHERE parent_cat_id = '{$cat_id}' OR cat_id = '{$cat_id}'

Sure you do, I have a dynamic thing you know. Read my post fully and you'll understand.

Oh i use codeigniter all the time...

 

Its easy to integrate it into a controller

 

class Home extends Controller
{
   public $item_count = 0;
   ...your other vars/controllers/etc...

   function _scan( $parent_cat_id = 0 )
   {
      $result = mysql_query('SELECT * FROM `cats` WHERE `parent_id` = "' . $parent_cat_id . '"');
      if ( mysql_num_rows($result) > 0 )
      {
         $item_count = $item_count + mysql_num_rows($result);
         while ( $row = mysql_fetch_assoc($result) )
         {
            $this->_scan( $row['id'] );
         }
      }
   }
   function _get_item_count()
   {
      return $this->item_count;
   }
}


 

Or you could put the function into a Model, which would make sense, as they are for DB stuff

Oh i use codeigniter all the time...

 

Its easy to integrate it into a controller

 

class Home extends Controller
{
   public $item_count = 0;
   ...your other vars/controllers/etc...

   function _scan( $parent_cat_id = 0 )
   {
      $result = mysql_query('SELECT * FROM `cats` WHERE `parent_id` = "' . $parent_cat_id . '"');
      if ( mysql_num_rows($result) > 0 )
      {
         $item_count = $item_count + mysql_num_rows($result);
         while ( $row = mysql_fetch_assoc($result) )
         {
            $this->_scan( $row['id'] );
         }
      }
   }
   function _get_item_count()
   {
      return $this->item_count;
   }
}


 

Or you could put the function into a Model, which would make sense, as they are for DB stuff

Yes sure I understand all that, just I am new with recursions.

I misunderstood the wording of unlimited amount of subcategories. If sub categories can have sub categories, and those sub-sub categories can have sub categories, etc. then surely, you'll need a recursive function.

Good that you get it now.

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.