Create Mini Student Websites from Array Using Foreach

Hello,

You folks have been lovely to me over the years. I’m so glad that there is a beginners area. I have tried using a certain chat site and I have found that other participants just expect your code from day one to be expert standard. Some people don’t learn like that. My learning style is 1. Write code, get it to function how I intended. 2. Get to grips with syntax and the language. 3. Improve technique once I have the confidence to write code and use PHP more widely. That might not be everybody’s way but it’s my way of learning. Me and my bestie are both studying PHP together and neither of us are the best readers. The PHP Manual is confusing to say the least (at our level).

I have a sneeky suspicion that the above won’t be needed in this forum where people have been lovely.

My project

I have set myself a project of using a basic array of over 30 names and my goal is to create each one of them a) their own directory b) copy an index.php webpage into it that uses the directory name (equal to their first name stored in the array) to say hello to them.

My effort

I have read up on creating directories and also found a handy tip to use dir to ensure that the directory is created where intended. I’ve discovered that permissions are equivalent to chmod. That’s awesome. I’m using a shared website hosting environment. I successfully created a directory using my script and then I got to looking at how the foreach loop would be constructed.

In the answers section for this question Stack Overflow I saw the following that confused me as it was not obvious what in the code was a placeholder and, if they are, what each placeholder would be for. For example, i’m not using an associative array so there is no key value pair:

foreach($folders as $folder)
{
if (!file_exists($folder)) {
    mkdir($folder, 0777, true);
}
}

// The following is my work....

<?php

$newStudents = array("Caroline", "David", "Johnny", "Finley", "Skye", "Abigail", 
"Jessica", "Toby", "Polly", "Charley", "Gizmo", "Mal", "Killian", "Joseph", "Maria", "Marie", "Tara", "Timothy", "Sebastion", "Paul", "Katherine", "Catherine", "Kate", "Joe", "Charles", "Charlie", "Tom", "Thomas", "Laura", "Stacey", "Melanie", "Jennie", "Jenny", "Stuart", "Steve", "Steven", "Stephen");


foreach ($newStudents as &$eachStudentInArray) {
    mkdir(__DIR__ . /student/$eachStudentInArray/, 0755, true);

?>

Thanks and Kind Regards,

bgareth.

It would be far simpler to create a database table preferably with PHP PDO and tailoring the page to the specific student rather than create physical directories. You still can learn how to make directories by placing the images of students in certain directories, plus you would learn other aspects of PHP. Just my opinion. Personally, I like have control of my structural directories and in my opinion introducing variables is making it less secure. I know it’s just practice and it’s coming from an array, but if this was a real world scenario the names would be coming from user input or from a database.

1 Like

As mentioned, going database driven is the better solution. Template out the “site” and inject the variables into the page. Think in terms of Amazon, each product doesn’t have its own page.

If you’re very new to PHP, learning your way around databases may be overloading your horse. If I understand you correctly, your array is a simple numeric one, so you just need to loop through as many times are there are elements, and create each directory in turn:

$path = <where ever>;
$permissions = <whatever>;
$c = count($newStudents); // how many are there?
for ($i = 0 ; $i < $c ; $i++) { // remember arrays are base (start at) 0
  mkdir($path . $newStudents[$i], $permissions, ...);
}

Of course you’ll want to add some error checking, e.g. what if the mkdir fails, etc.

Sponsor our Newsletter | Privacy Policy | Terms of Service