473,378 Members | 1,407 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

binary tree, long && difficult

Hi, I have this table:
*
CREATE TABLE `osservatorio` (
**`id`*int(10)*unsigned*NOT*NULL*auto_increment,
**`testo`*varchar(255)*NOT*NULL*default*'',
**`parent`*int(11)*default*NULL,
**`allegato`*varchar(255)*default*NULL,
**`descrizione`*varchar(255)*default*NULL,
**`online`*enum('true','false')*NOT*NULL*default*' true',
**PRIMARY*KEY**(`id`),
**KEY*`parent`*(`parent`),
**KEY*`online`*(`online`)
) TYPE=MyISAM AUTO_INCREMENT=13 ;

#
# Dumping data for table `osservatorio`
#

INSERT INTO `osservatorio` VALUES (1, 'Monitoraggio Ambientale', 0, NULL,
'', 'true');
INSERT INTO `osservatorio` VALUES (2, 'Lotto 1.1', 1, NULL, '', 'true');
INSERT INTO `osservatorio` VALUES (3, 'Lotto 1.2', 1, NULL, '', 'true');
INSERT INTO `osservatorio` VALUES (4, 'Lotto 1.2.1', 3, NULL, '', 'true');
INSERT INTO `osservatorio` VALUES (5, 'Lotto 1.2.2', 3, NULL, '', 'true');
INSERT INTO `osservatorio` VALUES (6, 'Lotto 1.2.2.3', 3, NULL, '', 'true');
INSERT INTO `osservatorio` VALUES (7, 'Programma Attivitą', 5, NULL, '',
'true');
INSERT INTO `osservatorio` VALUES (8, 'Programma Attivitą', 6, NULL, '',
'true');
INSERT INTO `osservatorio` VALUES (9, 'Relazione Rumore Corso D\'Opera', 6,
NULL, '', 'true');
INSERT INTO `osservatorio` VALUES (10, 'Lotto 1.3', 1, NULL, '', 'true');
INSERT INTO `osservatorio` VALUES (11, 'Lotto 1.4', 1, NULL, '', 'true');
INSERT INTO `osservatorio` VALUES (12, 'Relazione Metodologica', 1, NULL,
'', 'true');

with a simple "while" i can get this array

(please look at the keys)

Array
(
****[0]*=>*Array
********(
************[1]*=>*Monitoraggio*Ambientale
********)

****[1]*=>*Array
********(
************[2]*=>*Lotto*1.1
************[3]*=>*Lotto*1.2
************[10]*=>*Lotto*1.3
************[11]*=>*Lotto*1.4
************[12]*=>*Relazione*Metodologica
********)

****[3]*=>*Array
********(
************[4]*=>*Lotto*1.2.1
************[5]*=>*Lotto*1.2.2
************[6]*=>*Lotto*1.2.2.3
********)

****[5]*=>*Array
********(
************[7]*=>*Programma*Attivitą
********)

****[6]*=>*Array
********(
************[8]*=>*Programma*Attivitą
************[9]*=>*Relazione*Rumore*Corso*D'Opera
********)

)

I'd, like to have every element of the array in a hierarchical way.
i still can't get any solution for this.
any help will be appreciated

--
Sat_
Jul 17 '05 #1
12 2839
sathia wrote:
I'd, like to have every element of the array in a hierarchical way.
i still can't get any solution for this.
any help will be appreciated


I think you need a recursive solution:

<?php
function recurse_db(&$arr, $parent) {
$index = 0;
$sql = "select id, testo, parent, allegato, descrizione, online "
. "from test.osservatorio "
. "where parent = $parent";
$recs = mysql_query($sql) or die($sql . ' ==> ' . mysql_error());

while ($rec = mysql_fetch_row($recs)) {
$arr[$index] = $rec;
recurse_db($arr[$index]['children'], $rec[0]);
++$index;
}

mysql_free_result($recs);
}

mysql_connect() or die('==>' . mysql_error());
recurse_db($arr, 0); # <== start with the records for which the parent is 0
mysql_close();
print_r($arr);
?>
Happy Coding :-)
--
USENET would be a better place if everybody read: | to mail me: simply |
http://www.catb.org/~esr/faqs/smart-questions.html | "reply" to this post, |
http://www.netmeister.org/news/learn2quote2.html | *NO* MIME, plain text |
http://www.expita.com/nomime.html | and *NO* attachments. |
Jul 17 '05 #2
Thank you very much, i never tried to use several queries.
on my own i thought it would be possible just using one query.

you made my day :)

lovely solution.

Sat_
Jul 17 '05 #3
sathia wrote:
Thank you very much, i never tried to use several queries.
on my own i thought it would be possible just using one query.
It might be better :-)

Do one query and select all the rows you're interested in; put them in a
simple array, and recurse the array instead of doing a lot of
mysql_query()

I guess that as your database grows larger my last solution will grow
unacceptably slow.

lovely solution.


Thank you!
--
USENET would be a better place if everybody read: | to mail me: simply |
http://www.catb.org/~esr/faqs/smart-questions.html | "reply" to this post, |
http://www.netmeister.org/news/learn2quote2.html | *NO* MIME, plain text |
http://www.expita.com/nomime.html | and *NO* attachments. |
Jul 17 '05 #4
Pedro Graca wrote:
sathia wrote:
Thank you very much, i never tried to use several queries.
on my own i thought it would be possible just using one query.


It might be better :-)

Do one query and select all the rows you're interested in; put them in a
simple array, and recurse the array instead of doing a lot of
mysql_query()


I'm gonna try this this evening. now i'm on something else (even more
boring).

Tomorrow I'll let you know.

regards

--
Sat_
Jul 17 '05 #5
Sathia Musso wrote:
I'm gonna try this this evening. now i'm on something else (even more
boring).

Tomorrow I'll let you know.


Well ... I couldn't stop thinking about how to do it :)

<?php
error_reporting(E_ALL);

function fetch_db_data($sql) {
$conn = mysql_connect() or die('==>' . mysql_error());
$res = mysql_query($sql) or die($sql . ' ==> ' . mysql_error());
while ($row = mysql_fetch_row($res)) {
$data[] = $row;
}
mysql_free_result($res);
mysql_close($conn);
return $data;
}

/* maybe this global data and the two functions could be done in OOP */
/* and change the calls inside recurse_db_array() */

/* GLOBAL data */
$GBL_filter_value = 0;

/* uses global data!!! */
function set_filter_value($x) {
global $GBL_filter_value;
$GBL_filter_value = $x;
}

/* uses global data!!! */
function filter($row) {
global $GBL_filter_value;
return $row[2] == $GBL_filter_value;
}

/* pass data by reference, but it never changes */
/* if max_level is < 0, do a full recursion on all the data */
function recurse_db_array(&$data, &$arr, $parent, $max_level) {
if ($max_level-- == 0) return;
$index = 0;

/* maybe use a class??? */
/* I'm not used to OOP -- I am only used to "oops" */
set_filter_value($parent);
$children = array_filter($data, 'filter');

foreach ($children as $child) {
$arr[$index] = $child;
recurse_db_array($data, $arr[$index]['children'], $child[0], $max_level);
++$index;
}
}

$sql = 'select id, testo, parent, allegato, descrizione, online '
. 'from test.osservatorio '
. 'where 1=1';
$sql_data = fetch_db_data($sql);
recurse_db_array($sql_data, $arr, 0, -1);
print_r($arr);

/* another example */
unset($arr);
recurse_db_array($sql_data, $arr, 3, 1);
print_r($arr);
?>
--
USENET would be a better place if everybody read: | to mail me: simply |
http://www.catb.org/~esr/faqs/smart-questions.html | "reply" to this post, |
http://www.netmeister.org/news/learn2quote2.html | *NO* MIME, plain text |
http://www.expita.com/nomime.html | and *NO* attachments. |
Jul 17 '05 #6
grazie :)

it's very good now!

later i'll study it.
i've already tryed it and it works like a charm.
thank you very much indeed

Ciao dall'italia!
where are you from?
--
Sat_
Jul 17 '05 #7
Ciao Sathia

Sathia Musso wrote:
it's very good now!
Glad you like it :-)
[snip] where are you from?


It's all in my headers ... but I'll spare you :)

X-Location: Third rock from the Sun, Europe, Portugal, Coimbra
--
USENET would be a better place if everybody read: | to mail me: simply |
http://www.catb.org/~esr/faqs/smart-questions.html | "reply" to this post, |
http://www.netmeister.org/news/learn2quote2.html | *NO* MIME, plain text |
http://www.expita.com/nomime.html | and *NO* attachments. |
Jul 17 '05 #8
Thank you very much, i studied it and it works perfectly.

unfortunately i can't let you see what i done, 'cause it's under password.

Thank you very much, and see you soon :)
Jul 17 '05 #9
oooops

it seems broken, here's the entire discussion

http://groups.google.it/groups?hl=it....de%26rnum%3D6

--
man man ; man cd; man ls; man bash

fatto non succede nulla

Jul 17 '05 #10
Sathia Musso wrote:
oooops

it seems broken ...


It works for me.

The only change I did to the posted code was for the mysql_connect()
call: I included the parameters for my specific MySQL installation.

Ah! I also included an echo "\n\n--------\n\n"; between the two
examples.

mysql> select * from test.osservatorio;
+----+--------------------------------+--------+----------+-------------+--------+
| id | testo | parent | allegato | descrizione | online |
+----+--------------------------------+--------+----------+-------------+--------+
| 1 | Monitoraggio Ambientale | 0 | NULL | | true |
| 2 | Lotto 1.1 | 1 | NULL | | true |
| 3 | Lotto 1.2 | 1 | NULL | | true |
| 4 | Lotto 1.2.1 | 3 | NULL | | true |
| 5 | Lotto 1.2.2 | 3 | NULL | | true |
| 6 | Lotto 1.2.2.3 | 3 | NULL | | true |
| 7 | Programma Attivitą | 5 | NULL | | true |
| 8 | Programma Attivitą | 6 | NULL | | true |
| 9 | Relazione Rumore Corso D'Opera | 6 | NULL | | true |
| 10 | Lotto 1.3 | 1 | NULL | | true |
| 11 | Lotto 1.4 | 1 | NULL | | true |
| 12 | Relazione Metodologica | 1 | NULL | | true |
+----+--------------------------------+--------+----------+-------------+--------+
12 rows in set (0.05 sec)
php$ php sathia.php

Array
(
[0] => Array
(
[0] => 1
[1] => Monitoraggio Ambientale
[2] => 0
[3] =>
[4] =>
[5] => true
[children] => Array
(
[0] => Array
(
[0] => 2
[1] => Lotto 1.1
[2] => 1
[3] =>
[4] =>
[5] => true
[children] =>
)

[1] => Array
(
[0] => 3
[1] => Lotto 1.2
[2] => 1
[3] =>
[4] =>
[5] => true
[children] => Array
(
[0] => Array
(
[0] => 4
[1] => Lotto 1.2.1
[2] => 3
[3] =>
[4] =>
[5] => true
[children] =>
)

[1] => Array
(
[0] => 5
[1] => Lotto 1.2.2
[2] => 3
[3] =>
[4] =>
[5] => true
[children] => Array
(
[0] => Array
(
[0] => 7
[1] => Programma Attivitą
[2] => 5
[3] =>
[4] =>
[5] => true
[children] =>
)

)

)

[2] => Array
(
[0] => 6
[1] => Lotto 1.2.2.3
[2] => 3
[3] =>
[4] =>
[5] => true
[children] => Array
(
[0] => Array
(
[0] => 8
[1] => Programma Attivitą
[2] => 6
[3] =>
[4] =>
[5] => true
[children] =>
)

[1] => Array
(
[0] => 9
[1] => Relazione Rumore Corso D'Opera
[2] => 6
[3] =>
[4] =>
[5] => true
[children] =>
)

)

)

)

)

[2] => Array
(
[0] => 10
[1] => Lotto 1.3
[2] => 1
[3] =>
[4] =>
[5] => true
[children] =>
)

[3] => Array
(
[0] => 11
[1] => Lotto 1.4
[2] => 1
[3] =>
[4] =>
[5] => true
[children] =>
)

[4] => Array
(
[0] => 12
[1] => Relazione Metodologica
[2] => 1
[3] =>
[4] =>
[5] => true
[children] =>
)

)

)

)
--------

Array
(
[0] => Array
(
[0] => 4
[1] => Lotto 1.2.1
[2] => 3
[3] =>
[4] =>
[5] => true
[children] =>
)

[1] => Array
(
[0] => 5
[1] => Lotto 1.2.2
[2] => 3
[3] =>
[4] =>
[5] => true
[children] =>
)

[2] => Array
(
[0] => 6
[1] => Lotto 1.2.2.3
[2] => 3
[3] =>
[4] =>
[5] => true
[children] =>
)

)
--
Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
== ** ## !! !! ## ** ==
TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
bypass the spam filter. I will answer all pertinent mails from a valid address.
Jul 17 '05 #11
oooops i meant "broken" talking about my NG server who keeps thread for 2
weeks only...

the script is great;)

Bye
Jul 17 '05 #12
Sathia Musso wrote:
oooops i meant "broken" talking about my NG server who keeps thread for 2
weeks only...


Ah! Sorry for the misunderstanding :-)
--
Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
== ** ## !! !! ## ** ==
TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
bypass the spam filter. I will answer all pertinent mails from a valid address.
Jul 17 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: qwweeeit | last post by:
Hi all, I am developing in Python (as a GUI I choosed Qt). To increase my expertise, besides reading manuals & tutorials, I am studying a big program developed in the language of my choice, and...
6
by: Otto Wyss | last post by:
I've the following function declaration: wxTree GetLastChild (const wxTree& item, long& cookie) const; I'd like to make the cookie parameter optional, i.e. "long& cookie = ....", without...
4
by: muthu | last post by:
In the following code it gives the error "error: invalid operands to binary &" Why it is happening #include <signal.h> #include <errno.h> #define SIGBAD(signo) ((signo) <= 0 || (signo) >=...
4
by: joe | last post by:
how to resize an upload image and then change to binary & insert to db
17
by: Terry | last post by:
I have some old VB5 functions that specify types of Long and Double that make calls to an unmanaged 3rd party DLL probably just as old. The source for the DLL is not available. I'm getting some...
1
by: Richard Eich | last post by:
gcc (GCC) 3.4.6 20060404 (Red Hat 3.4.6-3) source snippet: .... int i = 17 ; if ( 0x03 & i ) ....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.