Welcome Guest, Not a member yet? Register   Sign In
sub-categories and sub-subcategories on a associative array with CI
#11

[eluser]jedd[/eluser]
Hey markup2go,

When you say:
Quote:I'm building a small online shop that requires unlimited subcategories as well as a many-to-many relationship between categories and products.

Do you mean that there are unlimited levels of sub-categories - such as children, grand children, great-grand children, and so on forever? Or just that you can have lots of subcategories?

The fact that categories:products is n:m might make this slightly more confusing, if you want to be able to track back up the multiple parents(?).

Am I right in assuming that Products are completely flat here - there's no hierarchy at all for them?
#12

[eluser]markup2go[/eluser]
Yes, I mean unlimited levels of subcategories.

I don't need to do any back tracking up the hierarchy to find a products parents etc.

Products are completely flat.

I'm more concerned about being able to create a properly formatted multidimensional array of categories and children for passing to form_dropdown(). I think once I understand how it would properly be done I can manage to create other datasets for menu systems etc, on my own.

Thanks for the help.

[quote author="jedd" date="1252620441"]Hey markup2go,

When you say:
Quote:I'm building a small online shop that requires unlimited subcategories as well as a many-to-many relationship between categories and products.

Do you mean that there are unlimited levels of sub-categories - such as children, grand children, great-grand children, and so on forever? Or just that you can have lots of subcategories?

The fact that categories:products is n:m might make this slightly more confusing, if you want to be able to track back up the multiple parents(?).

Am I right in assuming that Products are completely flat here - there's no hierarchy at all for them?[/quote]
#13

[eluser]jedd[/eluser]
[quote author="markup2go" date="1252621713"]
Yes, I mean unlimited levels of subcategories.
[/quote]

Ahh .. potentially very dangerous stuff. Did you check out MPTT as an option here?

How and where do you prevent loops in your references? If you don't, then you'll need to cater for them at extraction time.

It's a bit dodgy, but if you have an idea of the actual quantity (and sanity) of your data, as it'll be presented, it might affect the algorithms you choose to use in your model. Do you have such an idea?

If you need to have all descendent elements returned, then you'll want some recursive function - my preference is to do that in PHP. If you're happy to just take an arbitrary number of levels deep, then life becomes Much Easier.
#14

[eluser]MEM[/eluser]
Sorry markup2go. Of course no problem.

Credits go to oddz from sitepoint forums. I just made little changes.

Actually, right now, I'm trying to figure out, how can we build something like this. (the one that I will post, works, I just need to understand HOW). Smile

Please be patient since I'm not on my computer, hence, not able to put it here. I will do that in 5 hours.


Regards,
Márcio
#15

[eluser]markup2go[/eluser]
Actually I think my issue comes down to the form_dropdown() method. I don't think it supports multidemensional arrays over 2 levels deep.
#16

[eluser]MEM[/eluser]
Sorry. :S I forgot to post de model, here please:

I'm really sorry. Sad



Code:
<?php

class M_Categoria extends Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}


public function getAllCategorias()
{

$query=$this->db->get(');

if($query->num_rows()>0)
{
return $query->result();
}
}

public function getCategoria($id_cat)
{

$query = $this->db->get_where(', array('id_cat'=>$id_cat));

if ($query->num_rows() > 0)
{
$record = $query->row();
}

return $record;
}


public function getCategoriasPrincipais()
{

$this->db->where('parent_id_cat', NULL);

$this->db->where('activa_cat =', 1);


$query=$this->db->get(');

if($query->num_rows()>0)
{
return $query->result();
}
}


public function getSubCategorias($id_cat)
{

$strSQL = 'SELECT c.id_cat,c.nome_cat,c.parent_id_cat FROM categoria c';
$query = $this->db->query($strSQL);

$arrQuery = $query->result_array();


return $this->_parseResultTree($id_cat,$arrQuery, $intRunner=0);

}



private function _parseResultTree($intParentId,&$arrRows,$intRunner)
{

$arrChildren = array();


for($i=0;$i<count($arrRows);$i++)

{

if($intParentId == $arrRows[$i]['parent_id_cat'])
{

$arrChildren = array_merge($arrChildren,array_splice($arrRows,$i--,1));
}
}


$intChildren = count($arrChildren);


if($intChildren != 0)
{

for($i=0;$i<$intChildren;$i++)
{

$arrChildren[$i]['children'] = $this->_parseResultTree($arrChildren[$i]['id_cat'],$arrRows,$intRunner++);
}
}

return $arrChildren;

}

}//end of class

?&gt;


I had to do some css and forms for another site, more urgent than this one, and I end up forgetting this one. I'm really sorry for having this forgotten for so long. Sad

Good Luck on you case, because now that I'm back, I must confess that I'm still stuck on this one.



Regards,
Márcio




Theme © iAndrew 2016 - Forum software by © MyBB