How would I make PHP get the contents of a .php file, populate some fields on the .php via the DOM, and then save the new file to a new location while leaving the old template file intact? I've read a bit about the DOM for PHP but it doesn't seem to be working for me.
You would use http://php.net/manual/en/function.fopen.php to open the existing file... check associated functions like fread while you are there then regular expressions to replace the content then fwrite and fclose to create and close the copy. If the php file is really a template its good practice to give it a non-php file extension so that people can't inadvertently run it. Safer still to have the files below the root.
I was thinking about using fopen but then I couldn't figure out an easy way to get the elements by ID becasue I don't know how to use regexps very well. Do you think you could give me an example of how use regexps to get an element by its id? Thanks.
me neither I just know that's what you'd need to do. If it's just templating you are trying to do why don't you use SMARTY or one of the other templating tools? If you don't want to use them then they'd still be worth a look at the source code to see how they work.
Well. Do you think that PHP's DOM commands would work or is that just for XML files? Mabye some regexps pro could help me create a regexps that would work like Javascripts document.getElementById('id');
What exactly will you be storing in the file? If it is just a data structure (array, object etc) then you can serialize the data the be stored in your external file. When you read this file back in, unserialize the data and you will have your data structure in tact which you can then output elsewhere. You could also look into storing it as XML or another interchangeable format.
No. I trying to create an entire PHP page that is populated with values and object from a database and elsewhere. The problem is that once I have create the file I have to save it. This is the reason I am trying to use PHP DOM to get the ID's so I can easily populate the fields.
I must reinvent the wheel!!! But seriously, when you think about it, once you start using premade scripts like JQuery and Smarty who can really call you a programmer. Basicly, programmers who use programming libraries or premade scripts to generate their entire code, can't really call themselves programmers because they don't know how to solve programmers problems. Now a good portion of the programming world are people who use real programmers codes to make most of their script. Which is sad. I prefer, to go old school, and to generate all my code without using premade scripts. So do you think you could tell me if PHP DOM would work and if not could you answer this question? Do PHP variables have a max size limit or can I put any number of bytes in them? Thanks!
Its a whole other debate but in the real world most of us don't have clients with unlimited budgets so that we can research security, efficiency etc. Even on my own projects I have to weigh up the return on investment (the cost of my time) and decide what is the best approach. You have done well to find a client who is happy to let you start from scratch.
Unless your writing your site in Machine code you'd be a hypocrite to criticize code-reuse. You're wasting time and money if your rejecting perfectly good solutions just because you have a sadistic fetish. There are a billion and one unsolved problems that you are keeping yourself from solving by.... I could go on all day, but I think I shouldn't, that would stop me from solving some problems.
Yes. I suppose that time and money is of a concern but if people just piece together a site with pre-made scripts *without modifying them* and call themselves great programmers, I just find that wrong.