Jump to content

replace number in directory path with corresponding name from database


rbarnett

Recommended Posts

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.

Link to comment
Share on other sites

try this for one way

 

{

                      $courseName = $row['fullname'];

                      $courseID = $row['id'];             

                      $courseFile = preg_replace("/[0-9]\//","$courseName/",$file,1); 

  echo $courseFile.'<br />';

    }

}

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.