Hello,

I was wondering if someone could help me. I am new to mysql/php and I was wondering if there is a way that I can have php create pages for me. What I want is that I no longer need to create new page, so I will have a template and all of the fields will come out of my db. And if I add a new person 'Bob', It will automatically create a new page. Like this


this will be my template: www.mysite.com/person/template.php

and it will create:
www.mysite.com/person/bob.php
www.mysite.com/person/joe.php

Thanks,

Aaron

Recommended Answers

All 6 Replies

This would be the absolute wrong way to go about this.
You would be left with multiple copies of outdated code as soon as your updated your template to resolve any bugs/security holes etc.

I'm not sure what you're pages will be displaying, I'm going to assume its for a profile-like system.

In which case you should create a single point of entry, index.php (or any file) and use a variable in the url to switch between users.

e.g.
mysite.com/person/?u=bob (assuming index.php)
mysite.com/person/bob (using .htaccess & mod_rewrite)
mysite.com/person/profile.php?u=1 (non-index.php access)

This way as you uncover security issues, bugs etc. and you will. You can modify the code in one place. Also makes it easier to roll out updates or new features etc.

<?php

if( !isset($_GET['u']) ){
  echo 'Must specify a user';
  exit;
}

$user = $_GET['u'];

//Do some kind of validation/filtering on the user variable
if ( !is_valid($user) ){
   echo 'Invalid user!';
   exit;
}

//Compile down all information from user
//Do some SQL queries, load images, etc.

//Display the output to the user
echo $content;

**NOTE: This is NOT functional/working code. Just illustrates the basic concept of how this would work.

Hi mschroeder,

Thank you, but I don't think that I did a very good job explaining my situation. what am trying to do is this, I have a db that has award winners in it. So I have fields in my db "Award, year, name and bio". I was wondering how can I use a template to generate a new page for each person.

so it would look like this

www.mysite.com/winner-2009.php
www.mysite.com/winner-2010.php
www.mysite.com/winner-2011.php


can the code that you sent me before be used in this circumstance as well?

thank you,

Aaron

This would be the absolute wrong way to go about this.
You would be left with multiple copies of outdated code as soon as your updated your template to resolve any bugs/security holes etc.

I'm not sure what you're pages will be displaying, I'm going to assume its for a profile-like system.

In which case you should create a single point of entry, index.php (or any file) and use a variable in the url to switch between users.

e.g.
mysite.com/person/?u=bob (assuming index.php)
mysite.com/person/bob (using .htaccess & mod_rewrite)
mysite.com/person/profile.php?u=1 (non-index.php access)

This way as you uncover security issues, bugs etc. and you will. You can modify the code in one place. Also makes it easier to roll out updates or new features etc.

<?php

if( !isset($_GET['u']) ){
  echo 'Must specify a user';
  exit;
}

$user = $_GET['u'];

//Do some kind of validation/filtering on the user variable
if ( !is_valid($user) ){
   echo 'Invalid user!';
   exit;
}

//Compile down all information from user
//Do some SQL queries, load images, etc.

//Display the output to the user
echo $content;

**NOTE: This is NOT functional/working code. Just illustrates the basic concept of how this would work.

That goes against the general greatness of using something like PHP to create a website. You would use PHP so you only need one page that has it's content change dynamically, not so that it creates more files that you have to manually manage.

Hi Pixelsoul,

Thanks, I don't necessarily need to create files, but do you know how I could create the urls that I could link to. Any help is appreciated.

Thanks,

Aaron

That goes against the general greatness of using something like PHP to create a website. You would use PHP so you only need one page that has it's content change dynamically, not so that it creates more files that you have to manually manage.

Aaron,

The concept I was illustrating is very basic in theory but very powerful in implementation.

Essentially you use variables in the url to control what is generated by the page. So in your case, a url might be something like mydomain.com/index.php?u={user_id}&y={year}

When a user would access that page it would validate on the $_GET to make sure they entered a valid user and then it would use the value of $_GET to find any awards for the year.

The URL's can be configured to show the way you want using mod_rewrite or htaccess but that would be the last thing I would be concerned with. I would be working more towards getting the pages setup to display the data that you want and then worry about how your url's are looking later.

mschroeder above gave a good example of using PHP to dynamically work with data on a page but it will be up to you to take it further and in the direction you want to go.

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.