I have multiple index.php files in different directories. I've created a script which goes through all of them (around 1000). What I need to do is to replace specific div (by it's class) with php code. The most important thing here is that source code remains unchanged (ON UNCHANGED PARTS).
I am beginning to create a pattern library from BareBones. It uses PHP to pull in external HTML files to create the different sections. I would like to be able to have a heading and description for each section. BareBones seems to use a separate text file for the usage descriptions, which I could do for the heading too. This would mean I would have 3 files for each section though. Is there a better way to do this? Some thoughts I had, but haven't been able to figure out are:
Have heading as commented out first line of HTML file and then pull the first line via PHP
Create a variable in the HTML doc somehow and use it to store the heading and call it from the main file. I think to do this I would need to make all of the snippets PHP files though.
Any thoughts?
Here is the flow I would like to achieve:
I have a set of template .php files on server.
Visitor of the website chooses some options
Options are added to template files as pieces of code
Visitor downloads customized files (.zip should contain both default template files if not customised, and customized ones)
Original files remain intact for the next visitor
The flow is similar to bootstrap customisation, only difference is that on bootstrap visitors are editing .less files, and my visitors would edit .php files
I would appreciate if you could point me to general direction how to execute steps 3,4 and 5, baring in mind that user input will be stored in objects and arrays with javascript.
My php knowledge is very rudimentary, so I don't know where would I start.
If you need any clarifications please do not hesitate to ask.
Thanks!
If the users only modify configuration options it seems the best way forward would be to use JSON as it is very easy to encode/decode with JS & PHP with $.parseJSON (jQuery) and json_decode() & json_encode() respectively.
You would provide a form and then convert these user inputs into the JSON format and store it in a .json file which can then be zipped up and sent as a download to your user.
This would also allow users to upload their zip files and have the form inputs defaulted to their original options.
So lets run through how bootstrap does it.
Page to customize the bootstrap file, you would do the same with a similar form.
Once the form is POSTed, take the array of form inputs and validate them for malicious input, correct data types such as a number etc and remove any form inputs that should not be there.
Convert the input into JSON, without validation this is easy as json_encode($_POST).
Write that to a JSON file and allow the user to download it (zip it up if you want).
In your case you have multiple PHP files, this seems a bit off and you should not be using PHP files to hold this sort of variable configuration data, JSON seems a lot better suited.
Take a look at this SO question: What is the best file format for configuration file?
Three methods are mentioned, PHP's way, JSON & XML. Take your pick, but I'm willing to bet that the easiest one for you is JSON.
If you have not used JSON before, create a new PHP script and try out the functions with multi-dimensional arrays and have a play around.
PHP Documention for JSON
If you are using JavaScript to store user selections etc., I recommend you don't actually use php for any of the functionality you described. So do it like this:
Store templates as static text file with demarcated sections to insert user options. Make the demarcated sections different from php syntax like ###Your option here###.
This is easy.
Fetch the relevant template using AJAX and do a simple string replace in JavaScript on the demarcated sections.
Put the resulting php code into a <textarea> using JavaScript for them copy and paste into Notepad or something
Original files are static and code is client-side, so this is fine
So I understand you want to ship php scripts customised for the user.
Consider having php files that cater for all possible user inputs. Then capture the user options into a configuration file where the structure is predictacable and easy to create.
For example
# config.inc.php
# Created by script that captures user options
$config['allow_shipping'] = true;
then in the php script
# file1.php
include config.inc.php
if ($config[$config['allow_shipping'] === true) {
some_php_code;
}
Your steps will be
I have a set of .php scripts files on server.
Visitor of the website chooses some options
Options are sent to a server script which creates a config file.
Visitor downloads customized files (.zip should contain both script files and config file)
Original files remain intact for the next visitor (less the config file)
Some posts to guide you
How I can create installer for website. PHP mysql
and
PHP Installer Script
I would consider making default code snippets and have every "option" link to various required snippets. That way every time an option is selected it uses the right code.
To continue with #Crafter's example, you could have the following code snippets
if (
$config[
]
===
) {
}
I have done this once before when I tried something similar in Java, and even though it seems to be more work than it should, it will end up quite useful once you get a sizeable "database" of code snippets.
Hope it was of any use?
-Peter
I have a file that prints a variable from mysql database via php say it is called independent.php. if we open the file via localhost/independent.php the file displays the information with html.
Is it possible to include independent.php in another php file, and only display the contents generated via php and not the html of independent.php so that using ajax the new html appear?
If you want to use only specific parts of your code, make it modular. Break it down into functions and/or classes. Separate the HTML output from the part that fetches data so you can call both independently. There's no other sane way.
Is there some utility that could be called via command line to produce a doc(x) file? The source file would be HTML and CSS.
I am trying to generate Word documents on the fly with PHP. I am only aware of phpdocx library, which is very low level and not much use for me (I already have one poor implementation of Word document generation).
What I need from a document:
TOC
Images
Footers/Headers (they could be manually made on each HTML page)
Table
Lists
Page break (able to decide what goes to which page, eg one HTML file per page, join multiple HTML files to produce the entire document.)
Paragraphs
Basic bold/etc styles
I didn't find PHPDOCX very useful either. An alternative could be PHPWord, i think it covers what you need. According the website it can do these things:
Insert and format document sections
Insert and format Text elements
Insert Text breaks
Insert Page breaks
Insert and format Images and binary OLE-Objects
Insert and format watermarks (new)
Insert Header / Footer
Insert and format Tables
Insert native Titles and Table-of-contents
Insert and format List elements
Insert and format hyperlinks
Very simple template system (new)
In your case that isn't enough, but there is a plugin available to convert (basic) HTML to Docx and it works very good in my opinion. http://htmltodocx.codeplex.com/
I am using this for a year or two now and am happy with it. Altough i have to add that the HTML can't be to complex.
The way I usually do these is to have a word document template file with the parts I want to replace using keywords (usually something like "{FIRSTNAME}").
This allows you to read the file via PHP then simply do str_replace on all the parts you want to replace, then write that to another file.
Dynamic tables using this method are a bit more tricky, as you need a sub template for a row, which you can then include inside the main template as many times as required.
I'm not sure if this is the best solution, it's always seemed very fiddly to me and every time I'm asked to do this I get frustrated with it, but I guess it works. So if anyone knows a better solution I'd love to hear it too!