rbarnett Posted March 2, 2009 Share Posted March 2, 2009 I am printing a directory listing to the screen and the path shows a number for one of the directories within the path. For example, the printout will look like: /var/data/moodleclient/testsite/3/backupdata/ExportFile_HOL-310-02-Biology_20080901034238.zip /var/data/moodleclient/testsite/4/backupdata/restorelog.html /var/data/moodleclient/testsite/2/res00027/!4368617074657220342d31.mp3 The numbers (in bold) correspond to ids from a mysql database table. What I would like to do is print out the directory listing as shown above but replace the numbers with their corresponding course name. For example, if you were to query the table you could do: SELECT coursename FROM course WHERE id = 4. My code to do this is below, however, my problem is that I always get the last course name from the database for every directory listing instead of having the course name that corresponds to the number (id) I am trying to replace <?php $site = GetFileDir($_SERVER['PHP_SELF']); $initPath = '/var/data/moodleclient'; $path = $initPath.$site; $dirROOT = '/var/www/html'.$site; require_once($dirROOT . "config.php"); require_login(); require_once($dirROOT."lib/dmllib.php"); $prefix = $CFG->prefix; function GetFileDir($php_self) { $filename = explode("/admin/report/datausage/", $php_self); // THIS WILL BREAK DOWN THE PATH INTO AN ARRAY for( $i = 0; $i < (count($filename) - 1); ++$i ) { $filename2 .= $filename[$i].'/'; } return $filename2; } function ListFiles($dir) { global $prefix; if($dh = opendir($dir)) { $files = Array(); $inner_files = Array(); while($file = readdir($dh)) { if($file != "." && $file != ".." && $file[0] != '.') { if(is_dir($dir . "/" . $file)) { $inner_files = ListFiles($dir .'/'. $file); if(is_array($inner_files)) $files = array_merge($files, $inner_files); } else { array_push($files, $dir . "/" . $file); } } } closedir($dh); return $files; } } foreach (ListFiles($path) as $key=>$file) { $query = 'SELECT fullname, id FROM ' . $prefix. 'course WHERE id ='.$key; $result = mysql_query($query); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $courseName = $row['fullname']; $courseID = $row['id']; } $courseFile = preg_replace("/[0-9]\//","$courseName/",$file,1); echo $courseFile.'<br />'; } ?> Thanks in advance for any help. Quote Link to comment Share on other sites More sharing options...
phpdragon Posted March 2, 2009 Share Posted March 2, 2009 try this for one way { $courseName = $row['fullname']; $courseID = $row['id']; $courseFile = preg_replace("/[0-9]\//","$courseName/",$file,1); echo $courseFile.'<br />'; } } Quote Link to comment Share on other sites More sharing options...
rbarnett Posted March 2, 2009 Author Share Posted March 2, 2009 Thanks, but that did not work. Now it replaces the number with a blank. Quote Link to comment 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.