/    Sign up×
Community /Pin to ProfileBookmark

Site Map generator….

I have a DB with the following.

ID PARENT_ID LINK_NAME
1 NULL Home

2 NULL Information Technolgy
3 2 IT Task List
4 2 IT Help Desk Tickets
5 NULL Administration
6 5 Facilities
7 4 IT Help Desk Tickets sub

I use this for my site-structure. As you can see the parent_id links to the ID so You can see how the structure falls.

Example…

Administration
— Facilities
Home
Information Technolgy
— IT Help Desk Tickets
—- IT Help Desk Tickets sub
— IT Task List

Now Here is my code that generates this.

[code=php]
$dbc = db_connect(‘acridev_db’);

$q = “SELECT * FROM MENUS WHERE parent_id is NULL ORDER BY link_name”;
$r = mysql_query($q, $dbc);
$row_count = mysql_num_rows($r);
if (mysql_num_rows($r) > 0) {
while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) {
$parent_id = $row[‘id’];
echo $row[‘link_name’];
echo ‘<br>’;

$q2 = “SELECT * FROM MENUS WHERE parent_id = ‘$parent_id’ ORDER BY link_name”;
$r2 = mysql_query($q2, $dbc);
$row_count2 = mysql_num_rows($r2);
if (mysql_num_rows($r2) > 0) {
while ($row2 = mysql_fetch_array($r2, MYSQL_ASSOC)) {
$parent_id2 = $row2[‘id’];
echo ‘– ‘.$row2[‘link_name’];
echo ‘<br>’;

$q3 = “SELECT * FROM MENUS WHERE parent_id = ‘$parent_id2’ ORDER BY link_name”;
$r3 = mysql_query($q3, $dbc);
$row_count3 = mysql_num_rows($r3);
if (mysql_num_rows($r3) > 0) {
while ($row3 = mysql_fetch_array($r3, MYSQL_ASSOC)) {
echo ‘—- ‘.$row3[‘link_name’];
echo ‘<br>’;

//END OF MENU 3
} //end while
} // end if

//END OF MENU 2
} //end while
} // end if

//END OF MENU 1
} // end WHILE
} // end IF

[/code]

I know there must be a simpler way to keep drilling down then more and more SELECT STATEMENTS…

right now it can only go three levels deep. How can I make it go deeper even if I dont add more select statments.
Is there a way the program can know to drill down more or not?

Thank you

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@Sid3335Aug 12.2009 — use a recursive function:

[code=php]
function printSiteMap($parentId=NULL, $level=0)
{
$comparison = $parentId==NULL ? 'IS NULL' : '= ' . $parentId ;
$result = mysql_query("SELECT id, link_name, parent_id FROM MENUS WHERE parent_id $comparison ORDER BY link_name") ;
if(mysql_num_rows($result)>0)
{
$level++ ;
while($row = mysql_fetch_assoc($result))
{
echo str_repeat('--', $level-1) . $row['link_name'] . '<br />' ;
printSiteMap($row['id'], $level) ;
}
}
}

$conn = mysql_connect('Localhost', 'username', 'password') ;
mysql_select_db('db', $conn) ;

printSiteMap() ;

mysql_close($conn) ;
[/code]


and in my opinion you should use a zero value for an integer field not a NULL value, as testing against a null value requires changing the query (e.g the $comparison bit in the code above)
Copy linkTweet thisAlerts:
@firmanauthorAug 12.2009 — ok. Say I use 0 instead of [I]NULL[/I]


Then how would I write this?
Copy linkTweet thisAlerts:
@Sid3335Aug 12.2009 — ok. Say I use 0 instead of [I]NULL[/I]

Then how would I write this?[/QUOTE]




[code=php]
function printSiteMap($parentId=0, $level=0)
{
$result = mysql_query("SELECT id, link_name, parent_id FROM MENUS WHERE parent_id = $parentId ORDER BY link_name") ;
if(mysql_num_rows($result)>0)
{
$level++ ;
while($row = mysql_fetch_assoc($result))
{
echo str_repeat('--', $level-1) . $row['link_name'] . '<br />' ;
printSiteMap($row['id'], $level) ;
}
}
}

$conn = mysql_connect('Localhost', 'username', 'password') ;
mysql_select_db('db', $conn) ;

printSiteMap() ;

mysql_close($conn) ;
[/code]
Copy linkTweet thisAlerts:
@MindzaiAug 12.2009 — Why run a new query every time? That's going to get mighty inefficient surely? Why not just pull all of the data and iterate it rather than pulling out a bit at a time? Or even better if store the data as a Nested Set and just get the whole sitemap in one query without any PHP post-processing.
Copy linkTweet thisAlerts:
@firmanauthorAug 12.2009 — Can you post the code?
Copy linkTweet thisAlerts:
@MindzaiAug 12.2009 — You want me to write your code for you? Without even saying please? :rolleyes:
Copy linkTweet thisAlerts:
@firmanauthorAug 12.2009 — Sid,

Thank you


Maybe you can help me with one more part... How about it I want to turn it into an Outiline using <UL> and <LI>

Is this hard to do?
×

Success!

Help @firman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...