I have a "New Employee" script, that when you enter the details it creates a folder according to their username, using mkdir, this all works fine,

I now have managed to import 200 entries into my DB from a spreadsheet, but I need to create a folder for all the people now in the database, I was about to do it manually, then realised, I could grab all the usernames from the DB and loop through each one creating a folder?

can someone help?
thanks

How do I do it? I am rubbish with the for each loops! I dont know how to write the statement, I can do the SQL to grab all the usernames, but then what should I do? I assume its a for each mkdir($username) or something? thanks

Look at the code from your first thread 11 months ago. The loop is there, use it. Inside the loop you can do the mkdir.

Ive read thogh that , but it doesnt really help me in this case, how does the below look to you please?

$username= "select * from training";
$results = mysql_query($username);
while($row = mysql_fetch_array($results))
{
mkdir($row[username]);
}

Ill try it now, many thanks for the assistance,

didnt work with this code:

<?
$con = mysql_connect("localhost","root","");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("training", $con);
$sql = "SELECT * FROM servicedesk";  
$result = mysql_query($sql);  
    while($row = mysql_fetch_array($result))
    {
    $thisdir = "../data/";
    mkdir($thisdir . $row["username"]);
    echo $row["username"];
    }
?>
Member Avatar for diafol

Does the dir have write rights?

Just curious. This is a difficult one for maintenance. Creating a new folder for everybody now - ok. What happens when you add new users? To prevent problems with existing dirs?

$thisdir = "../data/";
while(...){
    $path = $thisdir . $row['username'];
    if (file_exists($path)){
        continue;    
    }else{
        mkdir($path);
    }
}

Check the mod status of the data directory. You may need to change it. Are you able to create a directory under it by a different script (not using the username)?

it all works perfectly, thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.