Saurav Chaudhary
Saurav Chaudhary

Reputation: 89

write php page for every record on the database

I am just learning PHP by working on a simple project of employees. I want to learn how to make a php page for every record in the database. I have use while loop to create links in a page to every employee page. But I cant possibly manually create pages of every employee. i read this thread: How do I create file on server?

I want to run a loop function , inside there will be a layout of where every detail should be, like name here, id here, etc. and the function should loop for every record in the database, writing php files. And it should be saved as employeename.php, whatever the name may be. How is it possible?

Upvotes: 0

Views: 135

Answers (3)

GiantCowFilms
GiantCowFilms

Reputation: 1103

You can use a single PHP file, using a get variable. for example:

URL: example.com/employees/?employee=Jhon_Doe

Code:

$employee = $_GET["employee"];

Finally, you use that name to recognize your particular employees record, and display their information.

As such:

<h1><?php echo $employeeData["name"]?></h1>

Note, the following advice, is advanced and may be to difficult for a beginner, I added it just for interest's sake. If you would like to have clean urls(no ? marks) so it is not obvious that your using a single file, you can use rewrite rules:

add this to your .htacces file, which goes in the root directory of your server.

RewriteEngine on RewriteRule ^/employees/([.]+)/$ /employees/?employee=$1

Upvotes: 0

luttkens
luttkens

Reputation: 1264

I know this answer is not addressing your question directly, but when reading your question and the comments that followed I would like to give you an answer that I wish I got when I was a PHP-beginner:

Learn PHP by using a framework, such as Yii Framework

Pros starting with a framework

  1. You get a good structure and learning PHP- best practices
  2. You will have many examples - and I found it easy to learn from examples.

Cons

  1. The learning curve is a lot steeper. It took me a few intensive days to get things to work, but in the end it was worth it.

Upvotes: 0

wavemode
wavemode

Reputation: 2116

Instead of creating many PHP files, you'll likely want to have a single PHP script that can read the $_GET global, and loads employee data conditionally based on the script argument. Here's some pseudo-code:

   /* employee.php */
    switch($_GET['e']) {
        case 'john-smith':
            $info = getInfoFromDatabase('John Smith');
            break;
        case 'jane-doe':
            $info = getInfoFromDatabase('Jane Doe');
            break;
        /* ... etc ... */
    }

    displayInfo($info);

As you can see, this fictional file will load John Smith's information when accessed at employee.php?e=john-smith and Jane Doe's info at employee.php?e=jane-doe and so on. You can then have as many employee records in the database as you want and you never have to change anything.

Upvotes: 1

Related Questions