Welcome Guest, Not a member yet? Register   Sign In
Recursion problem
#1

[eluser]anpmtp[/eluser]
how to write a recursion function in model class
#2

[eluser]nzmike[/eluser]
Can you provide more information on what you are trying to do?
#3

[eluser]anpmtp[/eluser]
function parent_categery($Id,$selId = 0, $sym='')
{
if($Id==0)
$gr='';
else
$gr=$sym.'>';
$this->load->database();
$this->db->select('*');
$this->db->from('categories');
$this->db->join('categories_description', 'categories_description.categories_id = categories.categories_id');
$this->db->where('parent_id',$Id);
$query=$this->db->get();
$data['Select']='Select';
if ($query->num_rows() > 0)
{
foreach($query->result_array() as $row)
{
$Id=$row['categories_id'];
$data[$row['categories_id']]=$gr.$row['categories_name'];
$this->parent_categery($Id,$selId, $gr);
}
}
return $data;


}
This is my code. My query is working well. function cannot calling recursively
#4

[eluser]nzmike[/eluser]
The code's a wee bit messy but I can see what you're trying to do. At the moment you do a recursive call with $this->parent_categery($Id,$selId, $gr); but you're assigning the result to a variable.

So I think you need to do something like:

Code:
$Id=$row[‘categories_id’];
          $data[$row[‘categories_id’]]= array($gr.$row[‘categories_name’], $this->parent_categery($Id,$selId, $gr));
#5

[eluser]anpmtp[/eluser]
but it shows error ..

Use of undefined constant ‘categories_id’ - assumed '‘categories_id’'

i want each id with its name
#6

[eluser]Georgi Budinov[/eluser]
You can achieve this by defining data variable array in the model class and use it instead of variable $data like this - $this->data. This should work as you want it.

But here I must say that this way you are making db queries in recursion which is not desirable. Better catch all the rows you need with one query and then iterate through them!
#7

[eluser]nzmike[/eluser]
Quote:But here I must say that this way you are making db queries in recursion which is not desirable. Better catch all the rows you need with one query and then iterate through them!

Agreed. You want to let the database do the hard work for you , it'll be faster that way and easier to get your head around.
#8

[eluser]Unknown[/eluser]
HI,
This is just an example of the problem.

Let's assume you have moved to a new apartment. You are exploring the neighborhood, and you suddenly discover that you don't know your house number. You looked at the building, didn't find a number there. You knocked on a random apartment, and asked, "what is the number of this building," and the guy who opened the door smiled and said, "I don't know the number of this building, but if you look at the number of the building next to us and add 1, you will know our building number."

Now, let's pause for a moment. The problem you are trying to solve is "knowing the number of your house." Let's look at the proposed solution to this problem. What was the suggestion? You were asked to look at the number of the building next to you and add 1. Your problem was thus reduced into a problem of exactly the same nature! At first your problem was "to find the number of a house," and your problem now became "finding the number of a house."
#9

[eluser]nzmike[/eluser]
Yes, we understand what recursion is and your problem, as far as I can tell, is recursive. It's an interesting problem that I have come across before when trying to store a tree-like structure in the database. Perhaps this article might be of some use to you.




Theme © iAndrew 2016 - Forum software by © MyBB