Jump to content

Nesting Problem?


sr1200

Recommended Posts

Ive been pulling out hairs trying to get a piece of code to work.

I have a document repository that users upload files to. The files are uploaded to the appropriate folder and a record is added into the database that indexes when where by whom it was put as well as a flag for "public or private"

I got the upload portion of my program working great, now its time to display the documents that people have uploaded.
EX of database:

ID | name | file_name | file_flag | dept |
1 Bob info.txt 1 IT
2 Steve hex.doc 0 Exec
3 Sarah help.txt 0 IT
4 Tom help.txt 1 Acct


What i need to do is be able to display the data grouped by department,
so ex:
IT
info.txt
help.txt
Exec
hex.doc
Acct
help.txt

I created an sql query that counts the number of documents in each dept and returns only one value of each dept. ex:

IT - 2
Exec - 1
Acct - 1

So now i have a While statement that prints out each dept in teh database once.. (GREAT!)
however, i then cannot for some reason create another While statement WITHIN the first WHILE statement to matchup the files with the dept. heading.

I readup on some things regarding foreach, but couldnt get anything to work properly. If anyone has any example code snips that can point me in the right direction would be much appreciated.
Link to comment
https://forums.phpfreaks.com/topic/12667-nesting-problem/
Share on other sites

[!--quoteo(post=386948:date=Jun 22 2006, 04:02 PM:name=sr1200)--][div class=\'quotetop\']QUOTE(sr1200 @ Jun 22 2006, 04:02 PM) [snapback]386948[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Ive been pulling out hairs trying to get a piece of code to work.

I have a document repository that users upload files to. The files are uploaded to the appropriate folder and a record is added into the database that indexes when where by whom it was put as well as a flag for "public or private"

I got the upload portion of my program working great, now its time to display the documents that people have uploaded.
EX of database:

ID | name | file_name | file_flag | dept |
1 Bob info.txt 1 IT
2 Steve hex.doc 0 Exec
3 Sarah help.txt 0 IT
4 Tom help.txt 1 Acct
What i need to do is be able to display the data grouped by department,
so ex:
IT
info.txt
help.txt
Exec
hex.doc
Acct
help.txt

I created an sql query that counts the number of documents in each dept and returns only one value of each dept. ex:

IT - 2
Exec - 1
Acct - 1

So now i have a While statement that prints out each dept in teh database once.. (GREAT!)
however, i then cannot for some reason create another While statement WITHIN the first WHILE statement to matchup the files with the dept. heading.

I readup on some things regarding foreach, but couldnt get anything to work properly. If anyone has any example code snips that can point me in the right direction would be much appreciated.
[/quote]
I'm going to assume that you are using MySQL, however if not you can work this around to your needs
[code]$docs = mysql_query(..." ORDER BY dept");
$cur_dept = "";
while($row = mysql_fetch_assoc($docs))
{
   if($cur_dept != $row['dept'])
   {
      print $row['dept'] . "<br />";
      $cur_dept = $row['dept'];
   }
   print $row['file_name'] . "<br />";
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/12667-nesting-problem/#findComment-48592
Share on other sites

[!--quoteo(post=386966:date=Jun 22 2006, 04:25 PM:name=Eric_Ryk)--][div class=\'quotetop\']QUOTE(Eric_Ryk @ Jun 22 2006, 04:25 PM) [snapback]386966[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I'm going to assume that you are using MySQL, however if not you can work this around to your needs
[code]$docs = mysql_query(..." ORDER BY dept");
$cur_dept = "";
while($row = mysql_fetch_assoc($docs))
{
   if($cur_dept != $row['dept'])
   {
      print $row['dept'] . "<br />";
      $cur_dept = $row['dept'];
   }
   print $row['file_name'] . "<br />";
}[/code]
[/quote]

YOU ARE THE MAN!!!!

Worked Perfectly, just gotta format a bit, but thats it spot on! Thanks so much! [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /] [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]
Link to comment
https://forums.phpfreaks.com/topic/12667-nesting-problem/#findComment-48879
Share on other sites

[!--quoteo(post=387258:date=Jun 23 2006, 01:55 PM:name=sr1200)--][div class=\'quotetop\']QUOTE(sr1200 @ Jun 23 2006, 01:55 PM) [snapback]387258[/snapback][/div][div class=\'quotemain\'][!--quotec--]
YOU ARE THE MAN!!!!

Worked Perfectly, just gotta format a bit, but thats it spot on! Thanks so much! [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /] [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]
[/quote]
No problem man.
Link to comment
https://forums.phpfreaks.com/topic/12667-nesting-problem/#findComment-48907
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.