Re: [PHP] Recursion... Sort of...

2008-05-09 Thread tedd

At 5:48 PM -0400 5/8/08, Matt Neimeyer wrote:

Is there a way to tell if a function has been called that has resulted
in a call to the same function?

We have an in-house CRM app that has a function that draws a tabbed
panel on a screen... BUT if there are sub-sub-tabbed panels we want to
invert the tab colors and panel colors...

So...

DrawSubTab($Set) - Black on White
   Content
   DrawSubTab($Set) - White on Black
  Content
  DrawSubTab($Set) - Black on White
 Content
  DrawSubTab($Set) - Black on White
 Content
 Etc...

I suppose I could rewrite EVERY call to the function with a recursion
count like DrawSubTab($Set,$DepthCount+1) but that would be a MASSIVE
commit... whereas if the function can determine itself... All that
said I can't think of a good way to do it without a bastardized global
variable that track "how deep" we are and there is something that
bothers me about that approach... Unless that really is the easiest
way.

Thanks in advance!

Matt


If the CRM app uses a browser, then it sounds like a problem that 
could be solved via css -- parent/child relationships.


Cheers,

tedd

--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion... Sort of...

2008-05-09 Thread Matt Neimeyer
Wow! Thanks guys! Here's what I ended up doing... To get...

Black on White - 1
  White on Black - 2
 Black on White - 3
 Black on White - 3
  White on Black - 2
 Black on White - 3

I had to do something like...


function doStuff()
  {
  static $callCount = 0;
  $callCount++;

  if($callCount%2)
 { echo 'white on black - '.$callCount; }
  else
 { echo 'black on white - '.$callCount; }

  // Stuff that uses the depth count

  $callCount--;
  }

If I didn't put in the $callCount--; I ended up with something like this...

Black on White - 1
  White on Black - 2
 Black on White - 3
 White on Black - 4
  Black on White - 5
 White on Black - 6

I saw where it was said that "oh he said it wasn't recursive"... Sorry
I wasn't clearer. In my mind a "true" recursive function is a function
that operates on it's own output like a factorial... Not just a
function that is called inside itself.

This got me where I needed to be and it is GREATLY appreciated!

Matt

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion... Sort of...

2008-05-09 Thread Nathan Nobbe
On Fri, May 9, 2008 at 1:52 AM, Stut <[EMAIL PROTECTED]> wrote:

> On 9 May 2008, at 02:02, Nathan Nobbe wrote:
>
>> function doStuff() {
>> static $callCount;
>>
>> if(!isset($callCount))
>>  $callCount = 1;
>> else
>>  $callCount++;
>>
>> /// do stuff w/ $callCount to potentially handle sub-tabs and stuff
>>   if($callCount == 2) {
>> echo 'white on black';
>>   } else {
>> echo 'black on white';
>>   }
>>   echo PHP_EOL;
>> }
>>
>
> No need for the first if, just give the static var a default value...
>
> function doStuff() {
> static $callCount = 0;
> $callCount++;
> ...
> }
>
> Much neater.


in my haste i had also thought that would set the value to 0 every time the
function was called, heh.

-nathan


Re: [PHP] Recursion... Sort of...

2008-05-09 Thread Stut

On 9 May 2008, at 02:02, Nathan Nobbe wrote:

function doStuff() {
static $callCount;

if(!isset($callCount))
 $callCount = 1;
else
  $callCount++;

/// do stuff w/ $callCount to potentially handle sub-tabs and stuff
   if($callCount == 2) {
 echo 'white on black';
   } else {
 echo 'black on white';
   }
   echo PHP_EOL;
}


No need for the first if, just give the static var a default value...

function doStuff() {
static $callCount = 0;
$callCount++;
...
}

Much neater.

-Stut

--
http://stut.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion... Sort of...

2008-05-08 Thread Nathan Nobbe
On Thu, May 8, 2008 at 6:23 PM, Jim Lucas <[EMAIL PROTECTED]> wrote:

> Nathan Nobbe wrote:
>
>> On Thu, May 8, 2008 at 3:48 PM, Matt Neimeyer <[EMAIL PROTECTED]> wrote:
>>
>>  Is there a way to tell if a function has been called that has resulted
>>> in a call to the same function?
>>>
>>> We have an in-house CRM app that has a function that draws a tabbed
>>> panel on a screen... BUT if there are sub-sub-tabbed panels we want to
>>> invert the tab colors and panel colors...
>>>
>>> So...
>>>
>>> DrawSubTab($Set) - Black on White
>>>  Content
>>>  DrawSubTab($Set) - White on Black
>>> Content
>>> DrawSubTab($Set) - Black on White
>>>Content
>>> DrawSubTab($Set) - Black on White
>>>Content
>>>Etc...
>>>
>>> I suppose I could rewrite EVERY call to the function with a recursion
>>> count like DrawSubTab($Set,$DepthCount+1) but that would be a MASSIVE
>>> commit... whereas if the function can determine itself... All that
>>> said I can't think of a good way to do it without a bastardized global
>>> variable that track "how deep" we are and there is something that
>>> bothers me about that approach... Unless that really is the easiest
>>> way.
>>>
>>
>>
>> you dont need a global, you can have a variable that persists throughout
>> the
>> request local only to the function itself using the static keyword.
>>
>> function doStuff() {
>>  static $callCount;
>>
>>  if(!isset($callCount))
>>   $callCount = 1;
>>  else
>>$callCount++;
>>
>>  /// do stuff w/ $callCount to potentially handle sub-tabs and stuff
>>
>>  $callCount--;
>> }
>>
>> -nathan
>>
>>
> Look at the way he wants it to work.  Your way would change alternate the
> color each time the function is called.  I think the best/easiest way to
> keep track of depth will be by passing a variable in the function call
> itself.


actually, i didnt supply the part where he does what he wants with the
depth.  i merely provided a way to track it without using a global
variable.  he could easily do something specific depending upon the depth
with what ive shown.



nathan-nobbes-macbook-pro:~ nnobbe$ php testDepth.php
black on white
white on black
black on white
black on white
black on white

o ya and removed the part where the variable is decremented from the
original ;)  (good call there jim) i was thinking the function was going to
be called recursively at first which is why i had it in there and it would
make sense in that case; however since it isnt going to be called
recursively it doesnt.

-nathan


Re: [PHP] Recursion... Sort of...

2008-05-08 Thread David Otton
2008/5/8 Matt Neimeyer <[EMAIL PROTECTED]>:

> Is there a way to tell if a function has been called that has resulted
> in a call to the same function?

debug_backtrace()

Can't comment on performance, though. Its an inelegant solution.

> We have an in-house CRM app that has a function that draws a tabbed
> panel on a screen... BUT if there are sub-sub-tabbed panels we want to
> invert the tab colors and panel colors...
>
> So...
>
> DrawSubTab($Set) - Black on White
>   Content
>   DrawSubTab($Set) - White on Black
>  Content
>  DrawSubTab($Set) - Black on White
> Content
>  DrawSubTab($Set) - Black on White
> Content
> Etc...

Nested? You may be able to do this in pure CSS, without keeping a
depth counter at all.

> I suppose I could rewrite EVERY call to the function with a recursion
> count like DrawSubTab($Set,$DepthCount+1) but that would be a MASSIVE
> commit... whereas if the function can determine itself... All that

It's an easy change.

function DrawSubTable($Set, $DepthCount = 0)
{
[..]
DrawSubTable($Set, $DepthCount + 1);
[..]
}

By providing a default value, all the calls to DrawSubTable() can
remain unchanged. You just have to modify its own calls to itself.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion... Sort of...

2008-05-08 Thread Jim Lucas

Nathan Nobbe wrote:

On Thu, May 8, 2008 at 3:48 PM, Matt Neimeyer <[EMAIL PROTECTED]> wrote:


Is there a way to tell if a function has been called that has resulted
in a call to the same function?

We have an in-house CRM app that has a function that draws a tabbed
panel on a screen... BUT if there are sub-sub-tabbed panels we want to
invert the tab colors and panel colors...

So...

DrawSubTab($Set) - Black on White
  Content
  DrawSubTab($Set) - White on Black
 Content
 DrawSubTab($Set) - Black on White
Content
 DrawSubTab($Set) - Black on White
Content
Etc...

I suppose I could rewrite EVERY call to the function with a recursion
count like DrawSubTab($Set,$DepthCount+1) but that would be a MASSIVE
commit... whereas if the function can determine itself... All that
said I can't think of a good way to do it without a bastardized global
variable that track "how deep" we are and there is something that
bothers me about that approach... Unless that really is the easiest
way.



you dont need a global, you can have a variable that persists throughout the
request local only to the function itself using the static keyword.

function doStuff() {
  static $callCount;

  if(!isset($callCount))
   $callCount = 1;
  else
$callCount++;

  /// do stuff w/ $callCount to potentially handle sub-tabs and stuff

  $callCount--;
}

-nathan



Look at the way he wants it to work.  Your way would change alternate the color 
each time the function is called.  I think the best/easiest way to keep track of 
depth will be by passing a variable in the function call itself.


--
Jim Lucas

   "Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion... Sort of...

2008-05-08 Thread Nathan Nobbe
On Thu, May 8, 2008 at 3:48 PM, Matt Neimeyer <[EMAIL PROTECTED]> wrote:

> Is there a way to tell if a function has been called that has resulted
> in a call to the same function?
>
> We have an in-house CRM app that has a function that draws a tabbed
> panel on a screen... BUT if there are sub-sub-tabbed panels we want to
> invert the tab colors and panel colors...
>
> So...
>
> DrawSubTab($Set) - Black on White
>   Content
>   DrawSubTab($Set) - White on Black
>  Content
>  DrawSubTab($Set) - Black on White
> Content
>  DrawSubTab($Set) - Black on White
> Content
> Etc...
>
> I suppose I could rewrite EVERY call to the function with a recursion
> count like DrawSubTab($Set,$DepthCount+1) but that would be a MASSIVE
> commit... whereas if the function can determine itself... All that
> said I can't think of a good way to do it without a bastardized global
> variable that track "how deep" we are and there is something that
> bothers me about that approach... Unless that really is the easiest
> way.


you dont need a global, you can have a variable that persists throughout the
request local only to the function itself using the static keyword.

function doStuff() {
  static $callCount;

  if(!isset($callCount))
   $callCount = 1;
  else
$callCount++;

  /// do stuff w/ $callCount to potentially handle sub-tabs and stuff

  $callCount--;
}

-nathan


Re: [PHP] Recursion and threaded message boards...

2007-08-10 Thread Richard Lynch
On Fri, August 10, 2007 11:23 am, Tony Di Croce wrote:
> I have to write some PHP backend code for a threaded message board.
> The db
> has a message table, and each message has a parent id.
>
> Does anyone have any advice for someone whos never done this in PHP?
>
> I'm currently thinking that I write function that takes a db row as an
> argument, and initially, it is passed the root node of the whole tree.
> It is
> also probably passed a string variable.
>
> The first thing it will do is append the code for itself to the
> string.
>
> Then it will query the DB for all its children (with an order by post
> timestamp), and for every child, it will call itself on that child
> row.
>
> Am I on the right track? (I've done simmilar things in C++, just not
> in
> PHP)...

What you describe will "work" if you don't care about performance...

If there's any heavy activity at all, you'll be swamping your Database
in recursive "child" lookup queries.

Google for "SQL Tree Traversal" and you should find quite a few
solutions.

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion and threaded message boards...

2007-08-10 Thread Børge Holen
On Friday 10 August 2007 18:29, Stephen wrote:
> --- Tony Di Croce <[EMAIL PROTECTED]> wrote:
> > I have to write some PHP backend code for a threaded
> > message board. The db
> > has a message table, and each message has a parent
> > id.
> >
> > Does anyone have any advice for someone whos never
> > done this in PHP?
>
> You are reinventing the wheel here. Why?

Why bother doing with these questions when he only asks for advice... 
FOR THE SAKE OF DOING IT!!!


>
> There are a number of open source PHP message board
> scripts. Check those out, even if just to learn as
> examples.

Ah and here you come through =D

>
> Stephen

-- 
---
Børge
http://www.arivene.net
---

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion and threaded message boards...

2007-08-10 Thread Richard Davey
Hi Tony,

Friday, August 10, 2007, 5:23:28 PM, you wrote:

> I have to write some PHP backend code for a threaded message board.
> The db has a message table, and each message has a parent id.

> Does anyone have any advice for someone whos never done this in PHP?

> I'm currently thinking that I write function that takes a db row as
> an argument, and initially, it is passed the root node of the whole
> tree. It is also probably passed a string variable.

> The first thing it will do is append the code for itself to the string.

> Then it will query the DB for all its children (with an order by post
> timestamp), and for every child, it will call itself on that child row.

> Am I on the right track? (I've done simmilar things in C++, just not in
> PHP)...

To be honest this is less of a PHP issue (at this stage) and more of a
SQL one. What SQL package are you using? (MySQL? if so v4 or v5?). I
would strongly consider looking at using a nested set system for the
structure (or at least a derivative of it, there are many good ones
out there) in combination with a standard parent/child hierarchy.

You can wrap up nearly all of the complexity of this requirement (i.e.
the tree walking / retrieval) on a stored procedure level with some
decent table design.

Cheers,

Rich
-- 
Zend Certified Engineer
http://www.corephp.co.uk

"Never trust a computer you can't throw out of a window"

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion and threaded message boards...

2007-08-10 Thread Stephen
--- Tony Di Croce <[EMAIL PROTECTED]> wrote:

> I have to write some PHP backend code for a threaded
> message board. The db
> has a message table, and each message has a parent
> id.
> 
> Does anyone have any advice for someone whos never
> done this in PHP?

You are reinventing the wheel here. Why?

There are a number of open source PHP message board
scripts. Check those out, even if just to learn as
examples.

Stephen

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Recursion: Ugh!

2005-05-27 Thread Chris W. Parker
Steve Brown 
on Friday, May 27, 2005 2:17 PM said:

> So in your case, if you wanted to create a new item in the category
> "Round", you would first have to navigate to Food > Vegetables >
> Round, then create the new item.  This may seem more complicated,
> but think about how much time your users are going to spend scrolling
> through a list of lots of categories compared to this approach.

Oh ok. I understand what you're saying. It's like dmoz.org correct?

This is a good idea (and one that I might adopt in cart I've built for
the company I work for). But for the current project I'm working on (a
personal web based basic financial app [i.e. I need to make a budget and
keep track of it.]) I can see that kind of category navigation being
tedious. Reason being, I personally save all the receipts for every
transaction I make. I will then enter these receipts into the
application one at a time. But if I've got 25+ receipts to enter from
one week I'm not going to want to navigate through the category tree 25
times.

But in any case, this is mostly off topic at this point. Maybe all
future correspondence on this should be sent off list.


Chris.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion: Ugh!

2005-05-27 Thread Steve Brown
** email gagging, sorry if this is a DP **

On 5/27/05, Chris W. Parker <[EMAIL PROTECTED]> wrote:
> Let's say you're entering a new product, you'd want to see a list of all
> the available categories, not just the last node of a branch.

Not neccesarily; it depends on how big your tree structure is.  If you
only have 10 "categories" where an item could be placed, a list of all
available "categories" might be an OK solution.  However, if you have
100 "categories," listing all of them is impractical.  Even if you
only have 10 categories, you should plan for the day when you will
have >10 categories.

In our store (>500 categories), you add a new item by adding a child
to the current item.  Usually we know which category we want to place
the item in before we begin to lay it up (if we don't, we can move an
item at any time simply by changing the parent of the item).  So in
your case, if you wanted to create a new item in the category "Round",
you would first have to navigate to Food > Vegetables > Round, then
create the new item.  This may seem more complicated, but think about
how much time your users are going to spend scrolling through a list
of lots of categories compared to this approach.

> But I wouldn't be building the entire tree if I were only looking at a
> specific node.

That's the beauty of it! :)  You don't need the entire tree if you are
working on a specific node.  Look at it this way: you can only work on
one item at a time, right?  If you are working on the item "Fruit,"
why do you care that the parent of "Long" is "Vegetables" or that
"Round" has two children, "Spikey" and "Smooth"?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion: Ugh!

2005-05-27 Thread Steve Brown
On 5/27/05, Chris W. Parker <[EMAIL PROTECTED]> wrote:
> Let's say you're entering a new product, you'd want to see a list of all
> the available categories, not just the last node of a branch.

Not neccesarily; it depends on how big your tree structure is.  If you
only have 10 "categories" where an item could be placed, a list of all
available "categories" might be an OK solution.  However, if you have
100 "categories," listing all of them is impractical.  Even if you
only have 10 categories, you should plan for the day when you will
have >10 categories.

In our store (>500 categories), you add a new item by adding a child
to the current item.  Usually we know which category we want to place
the item in before we begin to lay it up (if we don't, we can move an
item at any time simply by changing the parent of the item).  So in
your case, if you wanted to create a new item in the category "Round",
you would first have to navigate to Food > Vegetables > Round, then
create the new item.  This may seem more complicated, but think about
how much time your users are going to spend scrolling through a list
of lots of categories compared to this approach.

> But I wouldn't be building the entire tree if I were only looking at a
> specific node.

That's the beauty of it! :)  You don't need the entire tree if you are
working on a specific node.  Look at it this way: you can only work on
one item at a time, right?  If you are working on the item "Fruit,"
why do you care that the parent of "Long" is "Vegetables" or that
"Round" has two children, "Spikey" and "Smooth"?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Recursion: Ugh!

2005-05-27 Thread Chris W. Parker
Steve Brown 
on Thursday, May 26, 2005 11:47 AM said:

> How is your structure being built?  Is it hard-coded or dynamic (e.g.
> pulled from a DB)?

>From a database.

> We employ a similar "tree" structure for manging
> items in our store front.  Believe me when I say, its a hell of a lot
> easier to only be concerned about the current item rathen then every
> item in the sturcture.

But what perspective are you coming at this?

Let's say you're entering a new product, you'd want to see a list of all
the available categories, not just the last node of a branch.

I'm not sure I understand what you're trying to say.

> What I mean is,
> if you are looking at the element "Round", don't concern yourself with
> "Fruit" or "Long".

But I wouldn't be building the entire tree if I were only looking at a
specific node.


Sorry I am misunderstanding you.


Chris.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion: Ugh!

2005-05-27 Thread Steve Brown
> Food:Fruit:Red
> Food:Fruit:Green
> Food:Fruit:Yellow
> Food:Vegetables:Long
> Food:Vegetables:Round
> Food:Vegetables:Round:Spikey
> Food:Vegetables:Round:Smooth

How is your structure being built?  Is it hard-coded or dynamic (e.g.
pulled from a DB)?  We employ a similar "tree" structure for manging
items in our store front.  Believe me when I say, its a hell of a lot
easier to only be concerned about the current item rathen then every
item in the sturcture.  Consider that a 1-to-many relationship is much
easier to deal with than a many-to-many relationship.  What I mean is,
if you are looking at the element "Round", don't concern yourself with
"Fruit" or "Long".  Figure out your "upstream" path for the current
element, e.g. "Food:Vegetables" (which should be easy if you assume
that each element only has 1 parent).  Then figure out the children
for the current element, e.g. "Spikey" and "Round". KISS. :)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion: Ugh!

2005-05-27 Thread Chris

Chris W. Parker wrote:


Hi everyone,

I've been working on a problem for a few days now and I'm not making any
headway so I think it's time I come to the list for some help (though
this really disappoints me since it appears I'm not capable of solving
this problem on my own!).

Anyway, I'm using the Modified Preorder Tree Traversal method to store
my category hierarchy. Using recursion I can build an array that
accurately depicts the layout of the categories. So far so good. What I
have not been able to do thus far is turn that array into a list that
looks like this:

Food:Fruit:Red
Food:Fruit:Green
Food:Fruit:Yellow
Food:Vegetables:Long
Food:Vegetables:Round
Food:Vegetables:Round:Spikey
Food:Vegetables:Round:Smooth

My array is included at the end of this email. (And yes I typed it by
hand so hopefully there aren't any errors in it.)

I've searched the web but haven't found anything that's helped.

Anyone have a solution?

Thanks,
Chris.

...



I'm assuming this *is* a in a DB with Left and Right values.

There are two ways I do this, the first is just a relatively simple query:

SELECT sName FROM table WHERE 5 != iID AND (SELECT iLeft FROM table 
WHERE 5 = iID) BETWEEN iLeft AND iRight ORDER BY iLeft;


That will get You the parents of the Node with ID 5, starting witht he 
Root node. It pulls out all the Nodes whose Left and Right values 
contain the target node.


I also do this otuside of the query sometimes like this:
$oBy is an Object that implements Iterator and returns MySQL database 
rows as objects. It maintains the Path parts as a stack


   $aStack = array();
   $aPath = array();
   $iDepth = 0;
   foreach($oBy as $oRow)
   {
   while($iDepth > 0)
   {
   if($aStack[$iDepth-1] < $oRow->iR) 
unset($aStack[--$iDepth],$aPath[$iDepth]);

   else break;
   }
   $aPath[$iDepth] = $oRow->sCategoryID;
   echo implode(':',$aPath);
   $aStack[$iDepth++] = $oRow->iR;
   }


I've been doing a lot of working with these things the last few weeks, 
alot of this stuff is still fresh in mind, but I'm a horrible 
communicator. If this didn't answer your question, or if you ihave more 
ask away, I'll do my best.


Chris

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion: Ugh!

2005-05-27 Thread Chris


Chris W. Parker wrote:


Hi everyone,

I've been working on a problem for a few days now and I'm not making any
headway so I think it's time I come to the list for some help (though
this really disappoints me since it appears I'm not capable of solving
this problem on my own!).

Anyway, I'm using the Modified Preorder Tree Traversal method to store
my category hierarchy. Using recursion I can build an array that
accurately depicts the layout of the categories. So far so good. What I
have not been able to do thus far is turn that array into a list that
looks like this:

Food:Fruit:Red
Food:Fruit:Green
Food:Fruit:Yellow
Food:Vegetables:Long
Food:Vegetables:Round
Food:Vegetables:Round:Spikey
Food:Vegetables:Round:Smooth

My array is included at the end of this email. (And yes I typed it by
hand so hopefully there aren't any errors in it.)

I've searched the web but haven't found anything that's helped.

Anyone have a solution?

Thanks,
Chris.

...



I'm assuming this *is* a in a DB with Left and Right values.

There are two ways I do this, the first is just a relatively simple query:

SELECT sName FROM table WHERE 5 != iID AND (SELECT iLeft FROM table 
WHERE 5 = iID) BETWEEN iLeft AND iRight ORDER BY iLeft;


That will get You the parents of the Node with ID 5, starting witht he 
Root node. It pulls out all the Nodes whose Left and Right values 
contain the target node.


I also do this otuside of the query sometimes like this:
$oBy is an Object that implements Iterator and returns MySQL database 
rows as objects. It maintains the Path parts as a stack


  $aStack = array();
  $aPath = array();
  $iDepth = 0;
  foreach($oBy as $oRow)
  {
  while($iDepth > 0)
  {
  if($aStack[$iDepth-1] < $oRow->iR) 
unset($aStack[--$iDepth],$aPath[$iDepth]);

  else break;
  }
  $aPath[$iDepth] = $oRow->sCategoryID;
  echo implode(':',$aPath);
  $aStack[$iDepth++] = $oRow->iR;
  }


I've been doing a lot of working with these things the last few weeks, 
alot of this stuff is still fresh in mind, but I'm a horrible 
communicator. If this didn't answer your question, or if you ihave more 
ask away, I'll do my best.


Chris

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion: Ugh!

2005-05-27 Thread Chris

Chris W. Parker wrote:


Marek Kilimajer 
   on Thursday, May 26, 2005 11:35 AM said:

 


untested:

function display($array, $prefix = '') {
echo $prefix ':' . $array['name'] . "\n";
if(is_array($array['children']) && $array['children']) {
foreach($array['children'] as $child) {
display($child, $prefix ':' . $array['name']);
}
}
}
   



Thanks Marek.

I've had one suggestion off list also ...


 

If that is the email from me, it's not intended to be offlist.  The list 
doesn't seem to be relay my email.



Thanks,
Chris.

 



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion: Ugh!

2005-05-26 Thread Rory Browne
This would have been easier if you'd posted the php code to create the
array, as opposed to the output of print_r. I did this:

 'food',
'children' => array(
array(
'name' => 'meat',
'children' => 
array(
array('name' => "beef", "children" => NULL),
array("name" => "pork", "children" => NULL)
)
),
array(
'name' => 'friut',
'children' => 
array(
array('name' => "pears", "children" => NULL),
array('name' => "apples", "children" => NULL),
array('name' => "oranges", "children" => NULL)
)
),
array(
'name' => 'veg',
'children' => 
array(
array('name' => "parsnips", "children" => NULL),
array('name' => "carrots", "children" => NULL),
array('name' => "tomatoes", "children" => NULL),
)
)
)
)
);

function display_array($arr, $prefix=""){
while(list($k, $v) = each($arr)){
if(is_array($ca = $arr[$k]['children'])){
display_array($ca, $prefix . $arr[$k]['name'] . ":");
} else {
echo $prefix . $arr[$k]['name'] . "\n";

}
}
}   

display_array($arr);

?>

On 5/26/05, Chris W. Parker <[EMAIL PROTECTED]> wrote:
> Marek Kilimajer 
> on Thursday, May 26, 2005 11:35 AM said:
> 
> > untested:
> >
> > function display($array, $prefix = '') {
> >   echo $prefix ':' . $array['name'] . "\n";
> >   if(is_array($array['children']) && $array['children']) {
> >   foreach($array['children'] as $child) {
> >   display($child, $prefix ':' . $array['name']);
> >   }
> >   }
> > }
> 
> Thanks Marek.
> 
> I've had one suggestion off list also and, although I haven't been able
> to test this myself, I think my major mistake is that I've been doing
> the foreach() BEFORE checking for the existence of an array. Whereas
> both suggestions so far are checking for the existence of an array
> before the foreach().
> 
> I'll report back to the list with my results. Probably tomorrow.
> 
> 
> Thanks,
> Chris.
> 
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Recursion: Ugh!

2005-05-26 Thread Chris W. Parker
Marek Kilimajer 
on Thursday, May 26, 2005 11:35 AM said:

> untested:
> 
> function display($array, $prefix = '') {
>   echo $prefix ':' . $array['name'] . "\n";
>   if(is_array($array['children']) && $array['children']) {
>   foreach($array['children'] as $child) {
>   display($child, $prefix ':' . $array['name']);
>   }
>   }
> }

Thanks Marek.

I've had one suggestion off list also and, although I haven't been able
to test this myself, I think my major mistake is that I've been doing
the foreach() BEFORE checking for the existence of an array. Whereas
both suggestions so far are checking for the existence of an array
before the foreach().

I'll report back to the list with my results. Probably tomorrow.


Thanks,
Chris.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion: Ugh!

2005-05-26 Thread Marek Kilimajer

Chris W. Parker wrote:

Hi everyone,

I've been working on a problem for a few days now and I'm not making any
headway so I think it's time I come to the list for some help (though
this really disappoints me since it appears I'm not capable of solving
this problem on my own!).

Anyway, I'm using the Modified Preorder Tree Traversal method to store
my category hierarchy. Using recursion I can build an array that
accurately depicts the layout of the categories. So far so good. What I
have not been able to do thus far is turn that array into a list that
looks like this:

Food:Fruit:Red
Food:Fruit:Green
Food:Fruit:Yellow
Food:Vegetables:Long
Food:Vegetables:Round
Food:Vegetables:Round:Spikey
Food:Vegetables:Round:Smooth

My array is included at the end of this email. (And yes I typed it by
hand so hopefully there aren't any errors in it.)

I've searched the web but haven't found anything that's helped.

Anyone have a solution?


untested:

function display($array, $prefix = '') {
echo $prefix ':' . $array['name'] . "\n";
if(is_array($array['children']) && $array['children']) {
foreach($array['children'] as $child) {
display($child, $prefix ':' . $array['name']);
}
}
}




0 => Array
   (
  [name] => Food
  [children] => Array
 (
0 => Array
   (
  [name] => Fruit
  [children] => Array
 (
0 => Array
   (
  [name] => Red
  [children] =>
   )

1 => Array
   (
  [name] => Green
  [children] =>
   )

2 => Array
   (
  [name] => Yello
  [children] =>
   )
 )
   )

1 => Array
   (
  [name] => Vegetables
  [children] => Array
 (
0 => Array
   (
  [name] => Long
  [children] =>
   )

1 => Array
   (
  [name] => Round
  [children] => Array
 (
0 => Array
   (
  [name] => Spikey
  [children] =>
   )

1 => Array
   (
  [name] => Smooth
  [children] =>
   )
 )
   )
 )
   )
 )
   )



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion to sanitize user input

2004-10-08 Thread zooming
Hi Comex

Thanks!  That worked!  Robet you almost had it but missing the $key in
$newvalue[$key].

- Original Message -
From: "Comex" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, October 08, 2004 8:05 PM
Subject: Re: [PHP] Recursion to sanitize user input


> foreach ( $userInput as $key => $value )
>{
> $newvalue[$key] = sanitize( $value );
>}
> return $newvalue;
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion to sanitize user input

2004-10-08 Thread Comex
 foreach ( $userInput as $key => $value )
   {
$newvalue[$key] = sanitize( $value );
   }
return $newvalue;

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion to sanitize user input

2004-10-08 Thread Robet Carson
this would probably be even better:

   foreach ( $userInput as $key => $value )
 {
 $newvalue[$key] = sanitize( $value ); // reassign key with
sanatized value
 }
 return $newvalue // return array with sanatized $key => $value pairs
 }
 else

My 2 cents:

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion to sanitize user input

2004-10-08 Thread Robet Carson
it should be this i think:
  
foreach ( $userInput as $key => $value )
  {
  $newvalue[] = sanitize( $value );
  }
  return $newvalue // returns an array - since this is the only
way to get all veriables from the function
  }
  else

I could be wrong but the only way it would work for the code you have
writen would be to return an array of all the sanatized strings

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion to sanitize user input

2004-10-08 Thread Curt Zirzow
* Thus wrote Yoed Anis:
> Simple your code should look like this:
> 
> ...
> if ( is_array($userInput) )
> {
> foreach ( $userInput as $key => $value )
> {
> return sanitize( $value ); //< needed to return it or
> else its not recurssive

This is wrong, only the first item of each array will ever be seen.



Curt
-- 
The above comments may offend you. flame at will.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Recursion to sanitize user input

2004-10-08 Thread Yoed Anis
Simple your code should look like this:

...
if ( is_array($userInput) )
{
foreach ( $userInput as $key => $value )
{
return sanitize( $value ); //< needed to return it or
else its not recurssive
}
}
else
{

...
.

Should work, not tested.

Best,
Yoed


-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 
Sent: Friday, October 08, 2004 5:15 PM
To: [EMAIL PROTECTED]
Subject: [PHP] Recursion to sanitize user input


I'm trying to sanitize my user input.  My sanitize function does not work if
I send a variable that's an array.  I'm using recursion to go through the
array.  The example below shows that $_POST['city'] works but $_POST['user']
doesn't work.  The array comes back blank.

Anyone see what's wrong with my code?

OUTPUT:

Array
(
[city] => New York
[user] =>
)

CODE:

 $value )
{
sanitize( $value );
}
}
else
{
if ( get_magic_quotes_gpc() )
{
return trim( $userInput );
}
else
{
return trim( addslashes($userInput) );
}
}
}

$_POST['city'] = 'New York';
$_POST['user']['firstName'] = 'Bob';
$_POST['user']['lastName'] = 'Smith';
$_POST['user']['country'] = 'USA';

foreach ( $_POST as $key => $value )
{
 $_POST[$key] = sanitize( $value );
}

echo '';
echo print_r($_POST);
echo '';

?>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion help?

2004-09-19 Thread Gerard Samuel
Marek Kilimajer wrote:
your children function can return only one child, and one parent can 
have more children. You should create an array of all children and loop 
that.

As always, thanks for the nudge in the right direction.
I was in the middle of rewriting it, and applied your suggestion to it.
So here is the final *mock* code for the archives,
if anyone else runs into a similar situation...

header('content-type: text/plain');
$array = array (
  '' =>
  array (
'id' => '',
'pid' => '0',
'name' => 'Home',
'url' => 'index.php',
  ),
  'AB4wFQI2QUewz3P7' =>
  array (
'id' => 'AB4wFQI2QUewz3P7',
'pid' => '',
'name' => 'User',
'url' => 'modules/user/index.php',
  ),
  'AB4wFQI2QU2iY4SP' =>
  array (
'id' => 'AB4wFQI2QU2iY4SP',
'pid' => 'AB4wFQI2QUewz3P7',
'name' => 'Admin',
'url' => 'modules/user/admin/index.php',
  ),
  'AB4wFQI2QU2ihsz2' =>
  array (
'id' => 'AB4wFQI2QU2ihsz2',
'pid' => 'AB4wFQI2QU2iY4SP',
'name' => 'delete',
'url' => 'modules/user/admin/list_to_delete.php',
  ),
  'AB4wFQI2QUp0uEk7' =>
  array (
'id' => 'AB4wFQI2QUp0uEk7',
'pid' => 'AB4wFQI2QUewz3P7',
'name' => 'Edit Account',
'url' => 'modules/user/edit_account.php',
  ),
  'AB4wFQI2QUp2huwX' =>
  array (
'id' => 'AB4wFQI2QUp2huwX',
'pid' => 'AB4wFQI2QUewz3P7',
'name' => 'Login',
'url' => 'modules/user/login.php',
  ),
  'AB4wFQI2QUpzujZ8' =>
  array (
'id' => 'AB4wFQI2QUpzujZ8',
'pid' => 'AB4wFQI2QUewz3P7',
'name' => 'Register',
'url' => 'modules/user/register.php',
  ),
);
function foo(&$array)
{
$data = array();
foreach($array as $key => $value)
{
$data[ $key ]['name'] = $value['name'];
$data[ $key ]['url']  = $value['url'];
$child = has_child($value['id']);
if (!empty($child))
{
$data[ $key ]['sub'] = foo($child);
}
}
return $data;
}
$data = foo($array);
var_dump($data);
function has_child($id)
{
$data = array();
foreach($GLOBALS['array'] as $key => $value)
{
if ($value['pid'] === $id)
{
$data[$key] = $GLOBALS['array'][$key];
}
}
return $data;
}
?>
Gerard Samuel wrote:
Im trying to dynamically construct a multidimensional array
to be used with PEAR's HTML_Menu.
http://pear.php.net/manual/en/package.html.html-menu.intro.php.
Basically, Im pulling the data from a DB table, and trying to
attempt to convert the original array to what is needed for HTML_Menu.
If you look at the output (at the bottom of this message),
you'll see that after "Edit Account" it breaks to hell.
Any help in this matter would be very much appreciated.
Thanks
-- Script --

header('content-type: text/plain');
$m = array (
  0 =>
  array (
'id' => '',
'pid' => '0',
'name' => 'Home',
'url' => 'index.php',
  ),
  1 =>
  array (
'id' => 'AB4wFQI2QUewz3P7',
'pid' => '',
'name' => 'User',
'url' => 'modules/user/index.php',
  ),
  2 =>
  array (
'id' => 'AB4wFQI2QU2iY4SP',
'pid' => 'AB4wFQI2QUewz3P7',
'name' => 'Admin',
'url' => 'modules/user/admin/index.php',
  ),
  3 =>
  array (
'id' => 'AB4wFQI2QU2ihsz2',
'pid' => 'AB4wFQI2QU2iY4SP',
'name' => 'delete',
'url' => 'modules/user/admin/list_to_delete.php',
  ),
  4 =>
  array (
'id' => 'AB4wFQI2QUp0uEk7',
'pid' => 'AB4wFQI2QUewz3P7',
'name' => 'Edit Account',
'url' => 'modules/user/edit_account.php',
  ),
  5 =>
  array (
'id' => 'AB4wFQI2QUp2huwX',
'pid' => 'AB4wFQI2QUewz3P7',
'name' => 'Login',
'url' => 'modules/user/login.php',
  ),
  6 =>
  array (
'id' => 'AB4wFQI2QUpzujZ8',
'pid' => 'AB4wFQI2QUewz3P7',
'name' => 'Register',
'url' => 'modules/user/register.php',
  ),
);
//var_export($m);die;
var_export(foo());
function foo($key = 0)
{
$data = array();
for($x = $key; $x < count($GLOBALS['m']); $x++)
{
$id = $GLOBALS['m'][$x]['id'];
$name = $GLOBALS['m'][$x]['name'];
$data[$id]['name'] = $name;
if (FALSE !== ($child = children( $id )))
{
$data[$id]['sub'] = foo( $child );
$x = $child;
}
else
{
break;
}
}
return $data;
}
function children($id)
{
foreach($GLOBALS['m'] as $key => $value)
{
if ($id === $value['pid'])
{
return $key;
}
}
return FALSE;
}
?>
-- End Script --
-- Start Output --
array (
  '' =>
  array (
'name' => 'Home',
'sub' =>
array (
  'AB4wFQI2QUewz3P7' =>
  array (
'name' => 'User',
'sub' =>
array (
  'AB4wFQI2QU2iY4SP' =>
  array (
'name' => 'Admin',
'sub' =>
array (
  'AB4wFQI2QU2ihsz2' =>
  array (
'name' => 'delete',
  ),
   

Re: [PHP] Recursion help?

2004-09-19 Thread Marek Kilimajer
your children function can return only one child, and one parent can 
have more children. You should create an array of all children and loop 
that.

Gerard Samuel wrote:
Im trying to dynamically construct a multidimensional array
to be used with PEAR's HTML_Menu.
http://pear.php.net/manual/en/package.html.html-menu.intro.php.
Basically, Im pulling the data from a DB table, and trying to
attempt to convert the original array to what is needed for HTML_Menu.
If you look at the output (at the bottom of this message),
you'll see that after "Edit Account" it breaks to hell.
Any help in this matter would be very much appreciated.
Thanks
-- Script --

header('content-type: text/plain');
$m = array (
  0 =>
  array (
'id' => '',
'pid' => '0',
'name' => 'Home',
'url' => 'index.php',
  ),
  1 =>
  array (
'id' => 'AB4wFQI2QUewz3P7',
'pid' => '',
'name' => 'User',
'url' => 'modules/user/index.php',
  ),
  2 =>
  array (
'id' => 'AB4wFQI2QU2iY4SP',
'pid' => 'AB4wFQI2QUewz3P7',
'name' => 'Admin',
'url' => 'modules/user/admin/index.php',
  ),
  3 =>
  array (
'id' => 'AB4wFQI2QU2ihsz2',
'pid' => 'AB4wFQI2QU2iY4SP',
'name' => 'delete',
'url' => 'modules/user/admin/list_to_delete.php',
  ),
  4 =>
  array (
'id' => 'AB4wFQI2QUp0uEk7',
'pid' => 'AB4wFQI2QUewz3P7',
'name' => 'Edit Account',
'url' => 'modules/user/edit_account.php',
  ),
  5 =>
  array (
'id' => 'AB4wFQI2QUp2huwX',
'pid' => 'AB4wFQI2QUewz3P7',
'name' => 'Login',
'url' => 'modules/user/login.php',
  ),
  6 =>
  array (
'id' => 'AB4wFQI2QUpzujZ8',
'pid' => 'AB4wFQI2QUewz3P7',
'name' => 'Register',
'url' => 'modules/user/register.php',
  ),
);
//var_export($m);die;
var_export(foo());
function foo($key = 0)
{
$data = array();
for($x = $key; $x < count($GLOBALS['m']); $x++)
{
$id = $GLOBALS['m'][$x]['id'];
$name = $GLOBALS['m'][$x]['name'];
$data[$id]['name'] = $name;
if (FALSE !== ($child = children( $id )))
{
$data[$id]['sub'] = foo( $child );
$x = $child;
}
else
{
break;
}
}
return $data;
}
function children($id)
{
foreach($GLOBALS['m'] as $key => $value)
{
if ($id === $value['pid'])
{
return $key;
}
}
return FALSE;
}
?>
-- End Script --
-- Start Output --
array (
  '' =>
  array (
'name' => 'Home',
'sub' =>
array (
  'AB4wFQI2QUewz3P7' =>
  array (
'name' => 'User',
'sub' =>
array (
  'AB4wFQI2QU2iY4SP' =>
  array (
'name' => 'Admin',
'sub' =>
array (
  'AB4wFQI2QU2ihsz2' =>
  array (
'name' => 'delete',
  ),
),
  ),
  'AB4wFQI2QUp0uEk7' =>
  array (
'name' => 'Edit Account',
  ),
),
  ),
  'AB4wFQI2QU2ihsz2' =>
  array (
'name' => 'delete',
  ),
),
  ),
  'AB4wFQI2QU2iY4SP' =>
  array (
'name' => 'Admin',
'sub' =>
array (
  'AB4wFQI2QU2ihsz2' =>
  array (
'name' => 'delete',
  ),
),
  ),
  'AB4wFQI2QUp0uEk7' =>
  array (
'name' => 'Edit Account',
  ),
)
-- End Output --
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Recursion with database tables

2003-02-19 Thread Jono Bacon
Hi all,


Thanks for your help. I now have a wealth of things to try - I was 
initially going for the bottom up approach by starting at the bottom of 
the tree and working up, but I quite like the idea of storing id's in an 
array or list, although this would mean two queries to the DB.

I am still new to this list, but thanks for your help and I hope I can 
recipricate in some way in the future.

   Jono











--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Recursion with database tables

2003-02-19 Thread Ernest E Vogelsinger
At 14:56 19.02.2003, Jono Bacon spoke out and said:
[snip]
>The code that you posed is the technique I have used at the moment. This 
>technique works fine, but like my mate said, this limits me to a single 
>parent for a comment/message. I admit that is unlikely that I would need 
>more than one parent, but what would happen if I changed the parent - 
>would this model still work fine?
[snip] 

As long as you have one-to-many relationship this model will be fine. You
can easily change the "parent" of a message by replacing the "id_topic"
column with another (hopefully valid) topic id.

If you have many-to-many you need an intermediate table to resolve:

  +-++--++---+
  |TOPIC||MSG2TOPIC ||MESSAGE|
  +-++--++---+
  | id  | <- |id_topic  || id|
  | ... ||id_message| -> | ...   |
  +-++--++---+

To select all messages for a certain topic you would
select * from MESSAGE 
where id in (select id_message from MSG2TOPIC where id_topic =
$id_topic);
or, using a join
select m.* from MSG2TOPIC rel inner join MESSAGE m on m.id = rel.id_message
where rel.id_topic = $id_topic;

To select all topics for a particular message:
select * from TOPIC 
where id in (select id_topic from MSG2TOPIC where id_message =
$id_message);


-- 
   >O Ernest E. Vogelsinger 
   (\) ICQ #13394035 
^ http://www.vogelsinger.at/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Recursion with database tables

2003-02-19 Thread Ernest E Vogelsinger
At 15:16 19.02.2003, Jono Bacon spoke out and said:
[snip]
>How would I go about recursing down the tree? Has anyone done this before?
[snip] 

Given the DB layout I sketched before, you would:

1) delete all comments
delete from COMMENT 
where id_message in (
select id from MESSAGE where id_topic in $id_topic);

2) delete all messages
delete from MESSAGE where id_topic = $id_topic;

3) delete the topic
delete from TOPIC where id = $id_topic;

Wrap the whole stuff in a transaction if your database supports it and
you'll be fine.


-- 
   >O Ernest E. Vogelsinger 
   (\) ICQ #13394035 
^ http://www.vogelsinger.at/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Recursion with database tables

2003-02-19 Thread David Otton
On Wed, 19 Feb 2003 14:16:47 +, you wrote:

>>Otherwise, yes, in MySQL you have to recurse down the tree deleting
>>comments.

>How would I go about recursing down the tree? Has anyone done this before?

I have, but it's been a while. Something like (pseudo-code) .

def delete_comments(a) :
for b in a :
c = "SELECT commentid FROM comments WHERE parentcommentid = a"
if len(c) > 0 :
delete_comments(c)
"DELETE FROM comments WHERE commentid IN (a)"
return

def main() :
a = "SELECT commentid FROM comments WHERE topicid = 1234"
delete_comments(a)
"DELETE FROM topic WHERE topicid = 1234"

where a is a list of commentids, b is a single commentid and c is a list
of child commentids. This will delete the tree in a depth-first fashion.
This is quite an intensive way of doing things (potentially many selects
and deletes) but is just-about acceptable because you probably will
delete an entire topic so infrequently.

I recommend filling out the topicid on every post as the easier
solution, though.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Recursion with database tables

2003-02-19 Thread rblack

I'm assuming the comments don't include the topid id - if they did this
would be trivial.

So I'm assuming the following structure...

Topics:
  topicid

Messages:
  messageid
  topicid

Comments:
  commentid
  messageid

So, say you want to delete topic 23, and all messages/comments associated
with it.

First, select all the message ids which apply to that topic.
eg
  SELECT messageid FROM messages WHERE topicid = '23';

Loop through the messageid fields that are returned by this, and for each
one delete any associated comments
eg if one of the messages on topic 23 had the messageid 12

  DELETE FROM comments WHERE messageid = '12';

So that's the comments gone. Now delete the messages, and finally the topic
itself

  DELETE FROM messages WHERE topicid = '23';
  DELETE FROM topics WHERE topicid = '23';

And that's you.

There's plenty of variations on this of course - rather than deleting the
comments for each message separately, you could do that in one query by
building up a list of all the messageids you need to get rid of, something
like :

  DELETE FROM comments WHERE messageid IN ('12', '22', '34', '46');

or something like that - I can't remember the exact syntax offhand, but it
would be something like that.

HTH,

Richy
==
Richard Black
Senior Developer, DataVisibility Ltd - http://www.datavisibility.com
Tel: 0141 951 3481
Email: [EMAIL PROTECTED]


   
 
  Jono Bacon   
 
 
  ahoo.co.uk>   cc:            
 
Subject:  Re: [PHP] Recursion with 
database tables  
  19/02/2003 14:16 
 
  Please respond to
 
  jonobacon_lists  
 
   
 
   
 




David Otton wrote:

>On Wed, 19 Feb 2003 13:56:49 +, you wrote:
>
>
>
>>One questions though - if I delete a topic, I need to delete all of its
>>child messages and all of the child comments from each message. What is
>>the best technique to do this? This has been driving me up the wall as
>>it seems to involve some kind of looping and recursion.
>>
>>
>
>make sure /all/ your comments have the correct topicid set. Then simply
>"DELETE FROM comment WHERE topicid = x"
>
>Otherwise, yes, in MySQL you have to recurse down the tree deleting
>comments.
>
>
How would I go about recursing down the tree? Has anyone done this before?

Jono


>
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



This email has been scanned for all viruses by the MessageLabs SkyScan
service. For more information on a proactive anti-virus service working
around the clock, around the globe, visit http://www.messagelabs.com







-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Recursion with database tables

2003-02-19 Thread Jono Bacon
David Otton wrote:


On Wed, 19 Feb 2003 13:56:49 +, you wrote:

 

One questions though - if I delete a topic, I need to delete all of its 
child messages and all of the child comments from each message. What is 
the best technique to do this? This has been driving me up the wall as 
it seems to involve some kind of looping and recursion.
   


make sure /all/ your comments have the correct topicid set. Then simply
"DELETE FROM comment WHERE topicid = x"

Otherwise, yes, in MySQL you have to recurse down the tree deleting
comments.
 

How would I go about recursing down the tree? Has anyone done this before?

   Jono



 



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Recursion with database tables

2003-02-19 Thread David Otton
On Wed, 19 Feb 2003 13:56:49 +, you wrote:

>One questions though - if I delete a topic, I need to delete all of its 
>child messages and all of the child comments from each message. What is 
>the best technique to do this? This has been driving me up the wall as 
>it seems to involve some kind of looping and recursion.

make sure /all/ your comments have the correct topicid set. Then simply
"DELETE FROM comment WHERE topicid = x"

Otherwise, yes, in MySQL you have to recurse down the tree deleting
comments.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Recursion with database tables

2003-02-19 Thread Jono Bacon
Ernest E Vogelsinger wrote:


At 14:43 19.02.2003, Jono Bacon said:
[snip]
 

At the moment I have three tables: Topic, Message, Comment. A topic has 
messages and each message can have comments. I have set this up with a 
table each and a field for the id of the parent (e.g. a comment will 
have the id of the message that it refers to).

My mate said to me that I should really put all of this in one big table 
with a parent and child field. He said that my code should then figure 
out what is the child of what and how it is displayed.
   


This are one-to-many relationships? If yes, then your mate is wrong, you
can't easily implement one-to-many using a single table.
 




Hi Ernest,

Thanks for your swift response.

The code that you posed is the technique I have used at the moment. This 
technique works fine, but like my mate said, this limits me to a single 
parent for a comment/message. I admit that is unlikely that I would need 
more than one parent, but what would happen if I changed the parent - 
would this model still work fine?

I am considering sticking with this technique - it seems to make more 
sense and I thought it would be quicker for queries as I wouldnt have to 
query a single table with a million rows.

One questions though - if I delete a topic, I need to delete all of its 
child messages and all of the child comments from each message. What is 
the best technique to do this? This has been driving me up the wall as 
it seems to involve some kind of looping and recursion.

   Jono




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Recursion with database tables

2003-02-19 Thread Ernest E Vogelsinger
At 14:43 19.02.2003, Jono Bacon said:
[snip]
>At the moment I have three tables: Topic, Message, Comment. A topic has 
>messages and each message can have comments. I have set this up with a 
>table each and a field for the id of the parent (e.g. a comment will 
>have the id of the message that it refers to).
>
>My mate said to me that I should really put all of this in one big table 
>with a parent and child field. He said that my code should then figure 
>out what is the child of what and how it is displayed.

This are one-to-many relationships? If yes, then your mate is wrong, you
can't easily implement one-to-many using a single table.

A problem like yours is usually laid out like this (pseudocode):

TABLE TOPIC
  id   integer primary key;
  topicvarchar;

TABLE MESSAGE
  id   integer primary key;
  id_topic integer foreign key references TOPIC(id);
  message  text;

TABLE COMMENT
  id   integer primary key;
  id_message integer foreign key references MESSAGE(id);
  comment  text;

>I have two questions about this:
>
>1. First, is this idea of using one big table with parent/child 
>fields better than my seperate table approach?

See above.

>2. Secondly, how would I write the logic for the single table approach.

Forget it, use the approach above :)


-- 
   >O Ernest E. Vogelsinger
   (\)ICQ #13394035
^ http://www.vogelsinger.at/



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] recursion?????

2003-02-14 Thread David Freeman

 > What I want to do is have the registration form, on submit, check the
 > data validity right then, if there is an error, redisplay
 > the form with an * next to the field that is incorrect.

This is the code logic rather than the actual working code:

---Start of PHP Page---
1. Check for form having been submitted
 a. If form submitted, check values that have been input
  i.  If valid form input, redirect to another page (or process form
data)
  ii. If not valid form input, set a 'problem flag' for each broken bit
 b. Do any other form input data manipulation that you need (strip html
or whatever)

2. Display form
 a. Do a check to see if there was a 'problem flag' set for a field - if
so display '*'
 b. If you've done it right, the post data can be reloaded into the form
---End of PHP Page---

That's the basic idea anyway - essentially, you process your form input
data at the start of the page and act on what you find.  If you want to
avoid problems with browser reloads doing another submit then it's
probably worth redirecting a successful form submit to a new page.

CYA, Dave





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] recursion?????

2003-02-14 Thread Bas Jobsen
not tested 
> Basically, have the form action call itself  hince the recursion.
> Any suggestion/examples?

 method="POST">




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Recursion

2001-10-01 Thread Sheridan Saint-Michel

Recursion means you call a function which calls itself to get the job done.
Recursive functions have two parts, the base case and the recursive method.
The base case is the goal which signals that your function stops calling
itself...
sort of like the condition in a while loop... but for a function rather than
a loop.

Here is a simple recursive script I posted to this list a while back.

function Get_Tree($directory)
{
  global $dirs;

  if (substr($directory,-1) != "/")
$directory = $directory . "/";

  $fh = opendir($directory);
  while ($file = readdir($fh))
  {
if (is_dir($directory . $file) && $file != ".." && $file != ".")
{
  $dirs[count($dirs)] = $directory . $file;
  Get_Tree($directory.$file);
}
  }
  closedir($fh);
}


When you are done $dirs will be an array of all the directory names
below $directory (the initial value of $directory).  Here the base case
is when you reach a directory without any subdirectories (every $file
fails the if statement).

Anyway, I hope this helps.  If you have any specific questions about
recursion feel free to drop me a line  =P

Sheridan Saint-Michel
Website Administrator
FoxJet, an ITW Company
www.foxjet.com

- Original Message -
From: "Andres Montiel" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Sunday, September 30, 2001 12:23 PM
Subject: [PHP] Recursion


> One of my classes next semester needs me to program using a language
> that does "recurison". I don't really know what this means, though. Can
> PHP do this?
>
> Thanks!


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]