sr1200 Posted June 22, 2006 Share Posted June 22, 2006 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 IT2 Steve hex.doc 0 Exec3 Sarah help.txt 0 IT4 Tom help.txt 1 AcctWhat i need to do is be able to display the data grouped by department, so ex:IT info.txt help.txtExec hex.docAcct help.txtI created an sql query that counts the number of documents in each dept and returns only one value of each dept. ex:IT - 2Exec - 1Acct - 1So 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 Link to comment https://forums.phpfreaks.com/topic/12667-nesting-problem/ Share on other sites More sharing options...
Eric_Ryk Posted June 22, 2006 Share Posted June 22, 2006 [!--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 IT2 Steve hex.doc 0 Exec3 Sarah help.txt 0 IT4 Tom help.txt 1 AcctWhat i need to do is be able to display the data grouped by department, so ex:IT info.txt help.txtExec hex.docAcct help.txtI created an sql query that counts the number of documents in each dept and returns only one value of each dept. ex:IT - 2Exec - 1Acct - 1So 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] Quote Link to comment https://forums.phpfreaks.com/topic/12667-nesting-problem/#findComment-48592 Share on other sites More sharing options...
sr1200 Posted June 23, 2006 Author Share Posted June 23, 2006 [!--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\" /] Quote Link to comment https://forums.phpfreaks.com/topic/12667-nesting-problem/#findComment-48879 Share on other sites More sharing options...
Eric_Ryk Posted June 23, 2006 Share Posted June 23, 2006 [!--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. Quote Link to comment https://forums.phpfreaks.com/topic/12667-nesting-problem/#findComment-48907 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.