Click to See Complete Forum and Search --> : [RESOLVED] Get Parent Folders - Database
dclamp
Apr 3rd, 2008, 01:12 PM
Ok, i have a file manager page, and i created a 'move files' page.
This page lists all of the members folders from the database.
Here is the DB structure:
folderID | parentID | memberID | folder_name | password | sharesetting
1 | 0 | 2 | Test Folder | NULL | 0
2 | 1 | 2 | Test Child Folder | NULL | 0
3 | 1 | 2 | Test Child Folder numeo 2 | NULL | 0
So according to that database, folder 1 is a parent folder shown at ROOT. Folders 2 & 3 are child folders of 1.
What i want to do, is have it so that when it lists all of the members folders it will show it as a Path. EG:
Test Folder
Test Folder / Test Child Folder
Test Folder / Test Child Folder numero 2
But it is not going to be limited to 2 folders. it could be a lot more than that.
manavo11
Apr 3rd, 2008, 01:18 PM
Keep getting the parent of your current page (loop), until you get a parent id of 0 (you should use NULL as a value and add foreign keys).
dclamp
Apr 3rd, 2008, 01:36 PM
here is my loop:
<?php
$sql = "SELECT * FROM `folders` WHERE memberID='{$_SESSION['member_id']}'";
$query = mysql_query($sql);
?>
<h2>Mass Move Files</h2>
Where do you want to move these files?<br>
<form action='index.php?do=movedelete&move=1' method='POST'>
<input type="hidden" value='<?php print_r($_POST['checkfiles']); ?>' name='checkfiles'>
<select name='moveto'>
<?php while($row = mysql_fetch_array($query)){ ?>
<option value='<?php echo $row['folderID']; ?>'><?php echo $row['folder_name']; ?></option>
<?php } ?>
</select>
<input type='submit' name='submit' value='Move Files!'>
</form>
<?php
I dont understand what you mean keep getting them.
manavo11
Apr 3rd, 2008, 02:08 PM
To print the path I guess it would be something like this (not tested) :
$sql = "SELECT * FROM `folders` WHERE memberID='".$_SESSION['member_id']."'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$path = $row['folder_name'];
$parent_id = $row['parentID'];
while ($parent_id != 0)
{
$sql_parent = "SELECT * from `folders` where `parentID` = '".$parent_id."'";
$result_parent = mysql_query($sql_parent);
$row_parent = mysql_fetch_assoc($result_parent);
$parent_id = $row_parent['parentID'];
$path = $row_parent['folder_name'].' / '.$path;
}
I'm sure it doesn't work, but hopefully you get the idea :D
dclamp
Apr 3rd, 2008, 02:28 PM
your code is confusing the hell outta me. I have been looking at it for the past 10 minutes trying to figure out how to make it work, and i have no idea what i am doing.
Also, can you use not
manavo11
Apr 3rd, 2008, 02:29 PM
So I'll assume it's not working? :D
Can you post an sql dump for that table? Maybe i can try a bit more? Maybe even test it this time? :D
dclamp
Apr 3rd, 2008, 02:34 PM
attached... saved as .txt for upload compatibility
go online ;)
manavo11
Apr 4th, 2008, 11:16 AM
OK, try and analyze this now :D
<?php
$sql = "SELECT * FROM `folders` WHERE `memberID`='".$_GET['member_id']."' and `folderID` = '".$_GET['folder_id']."'";
$result = mysql_query($sql) or die($sql);
$row = mysql_fetch_assoc($result);
$path = $row['folder_name'];
$parent_id = $row['parentID'];
while ($parent_id != 0) {
$sql_parent = "SELECT * from `folders` where `folderID` = '".$parent_id."'";
$result_parent = mysql_query($sql_parent);
$row_parent = mysql_fetch_assoc($result_parent);
$parent_id = $row_parent['parentID'];
$path = $row_parent['folder_name'].' / '.$path;
}
echo $path;
This was the link I used : http://localhost/dylan.php?member_id=2&folder_id=5
I don't know what member ID is for.
dclamp
Apr 4th, 2008, 01:45 PM
got it to work. with a little modification, it works great
<?php
$sql = "SELECT * FROM `folders` WHERE `memberID`='".$_GET['member_id']."'";
$result = mysql_query($sql) or die($sql);
while($row = mysql_fetch_assoc($result)) {
$path = $row['folder_name'];
$parent_id = $row['parentID'];
while ($parent_id != 0) {
$sql_parent = "SELECT * from `folders` where `folderID` = '".$parent_id."'";
$result_parent = mysql_query($sql_parent);
$row_parent = mysql_fetch_assoc($result_parent);
$parent_id = $row_parent['parentID'];
$path = $row_parent['folder_name'].' / '.$path;
}
echo "Path: ".$path. "<br>";
}
manavo11
Apr 4th, 2008, 05:56 PM
Ah, to print the path for all the folders of a member? I suppose that's what you're doing... Glad you got it working :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.