i want to auto create a subdomain from an existing project.

primary i want to access the primary domain push a button and:
1. new subdomain will created from existing domain by using same source (and directing to same source)
2. new empty db will be created which belongs to the subdomain

so apache will handle incoming requests and resolve depending on the name to a virtual host.
is it possible to create some kind of layer so that i can committ maybe an id from apache or the incoming domain to the index script of the source.
so it would be possible to solve the problem which db will be called...

other ideas to create subdomain from one source or project with special database depending on subdomain?