Jquery UI Tabs: Strategy for using included files - php

I have an application using Jquery's UI Tabs for an overall menu, and they're great. However, I've come to a strategy question when implementing the new format.
First, the concept:
An index.php file includes several class files (also PHP) and calls the tabs
Each tab pulls in one file per page via the script's "ajax loading" feature (As described here)
Each tab's page contains a combination of static text, content loaded server-side at display, and content that's dynamically updated via jQuery's Ajax.
Now, the challenge:
Everything is working as expected except that pages that are pulled into the tabs don't have access to the aforementioned included php files on the index page. I'm able to use that content if I do a separate includes on each of the ajax included pages, but that could get out of hand in a hurry. So, I'm seeking a strategy to get one set of included files to persist across all my pages.
Any ideas for a graceful solution to this challenge?

PHP (well, the entire web) is stateless, meaning once the PHP interpreter has parsed a file, it spits it out and is done with it. There is no way for it to persist includes parsed in one instance to another instance.
The only way for the pages to gain access to files included in the "main" page is to include those files themselves. Like you said though, that could get out of hand and be pain-staking to maintain, which is why a lot of people resort to a registry file. Your registry file loads the includes you need, and you only need to include the registry file on all of your pages.

Related

PHP, HTML, jQuery website file- and code structure

I'm quite new to web developement and I'm working on a school project where I am to create a simple educational video platform. My upcomming task is to make my site a single page application. From what I've gathered, jQuery and AJAX will make this happen. While browsing examples and tutorials about these languages, I'm starting to wonder if my HTML/PHP/Script code structure needs to change. Picture contains the typical structure of my files.
I've been writing the PHP and HTML in the same .php file for each job the site has to do (e.g. upload video form data). I've included all of these in my main.php file. To make it a single page application, will I need to separate HTML and PHP from each include file, into different files? Also I'd like some feedback (opinions) on how I've structured this so far.
The high-level of what you want to do is move your php logic out of your html files. Here's how a single-page app should handle data flow:
In file.HTML: forms and fields take user input (anything from text to files)
In file.JS: You use jQuery to listen for events that are happening, essentially using your page's dom elements to pass dynamically entered data to your server through AJAX calls
In controller.php: The ajax call goes to a method in one of your controller files where all your logic will happen, items will be added to your database, etc.
In file.JS: The ajax call finishes by receiving data from your controller method, and makes the necessary changes to your view file (again using jQuery)
In file.html: The jQuery updates show a user the results of their input
The big change to make is separating your php and html files. Since you're doing a single-page application, you can probably get away with one html file, but you should have generally have a controller for every class (assuming you are using class and model interchangeably).
One thing that also might be missing is routes, though I've seen people getting by passing files as the 'url' field (a required field when making an AJAX call).
As far as file structure, most web frameworks will pair cs and js folders into a parent folder called public.
When it comes to jQuery, this is the best cheat sheet around. Make sure you have at least a basic understanding of the dom before going in too deep though.
You may want to learn about MVC pattern
you can start here
The MVC Pattern and PHP

Overwriting files properly

I am trying to manage caching on heavily used webpage written in PHP. I have marked some cacheable sections of PHP code, which I want to execute only pre-cache when administrator make changes in CMS. For this, I use this method:
I have file (for example "index-source.php") with some marked ares of PHP code, which are interpretable alone. When admin change some settings, these marked parts are executed and replaced with result (for example MySQL queries which reads menu items from DB are replaced with generated HTML menu). Resulted file is saved as new "index.php", which still have some PHP code, which can't be optimized by caching.
Now to my problem
If we assume, that this server is heavilly load, which means there is for example 100 requests per second, which in PHP requires file index.php. If I will use file_put_contents() to overwrite this index.php with new pre-cached version, is there any risk, that some requests will be interrupted, because of locked/not fully overwritten file? Basically I want to somehow update my PHP file and assure that PHP will include complete old or complete new version of that file or wait few milliseconds until file is overwritten. I dont't want PHP to fail require or load partially overwritten file.
Is that possible? Thanks
file_put_contents is not what you want.
Have a look at this project, and dive into the source to get a feel for what challenges you may have to face as well as the solution chosen.
https://github.com/PHPSocialNetwork/phpfastcache

Direction needed - how to create a customizable files?

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

Dynamic webpage content

I have a website with a jQuery UI navigation, and I've got ajax working to dynamically change content between html files. I'm basically just replacing the contents of certain div ids with the contents of other div ids.
This still requires numerous HTML files and I was wondering if there was a way to have a single html file and load content into the main page from it instead? What about an xml file?
Thanks for the tips and advice about best practices and possible solutions.
Using HTML:
Template Views-- Using this method you create separate html files for each piece of your site that you want to be able to load dynamically. This is More efficient, but it means you need to be able to manage your files properly.
Single View-- This method involves loading the entire page again, and extracting out the desired data in order to update the page. This is not very elegant or efficient, and I don't recommend it.
Using XML/JSON
This usually involves making ajax requests for data and using it to render the page on the client.
XML/JSON is typically more lightweight than html markup which decreases the amount of cpu time and bandwidth used on your server, however it does require that your users have computers that can handle doing some DOM manipulation.
This also requires that you have a good way of organizing your ajax modules. A pattern that I use looks like this.
$PROJ_DIR/ajax/{MODULENAME}.php
Then a module with the name calendar might accept an actions like, getYears, getTerms, setCurrTerm.
And within that script you switch thought the action to output the correct data, and or make the correct DB changes.
Multiple html files for views isn't a bad thing if you've structured it nicely.

loading css or javascript from non-public directory

is there a way to load css and/or javascript files from outside of the public web directory?
for example on my hosting service i have /public_html but don't want these files to exist in the public directory and want them in a directory outside of the public directory in a sibling directory /system (i am using codeigniter) within the /system/application/view/
Ultimately, Javascript and Stylesheets are processed on the client side. For that reason, there is no solution that would truly hide your javascript or CSS from the public.
One possible solution is to load the required CSS/ Javascript file via PHP using something like file_get_contents() and then outputting that directly to the page using inline styles / scripts.
This doesn't really solve your problem of hiding the code / styles from the public though. It would give you the option of filtering all code and styles through some kind of packer or obfuscatory, although there's no reason you couldn't do that with your static files (and at much less of a processing expense)
Yes -- in a way -- and Minify [http://code.google.com/p/minify/] is one approach.
Look at line 39 of the config file [http://code.google.com/p/minify/source/browse/trunk/min/config.php]. Here you will see where your minified cache sits outside of the web root. Now, I do not know if the source JS and CSS can sit in the same directory as the cache.
Not without a public facing proxy.
You will need to file_get_contents() or include them and then serve them to your page.
You can not just do ../../system and get above the DOCROOT.
They need to be processed by the browser, so they need to be accessible.
If you want to hinder people viewing your source in a human readable way, check out CSS minify and JS packer. These of course are only obfuscating the code. Anyone determined will be able to read your JavaScript and see what it does.
Why don't you want people to read your CSS or JavaScript?
I know what you mean twmulloy, it seems inconsistent to have 'view' related information in different places. However, consider that the JS and CSS files are resources that support the views, rather than parts of the view themselves.
That said, you can achieve what you want in a number of ways. One might be to write a controller that accepts requests for your JS/CSS assets and outputs a header and data from the relevant place (a view file, the database, anywhere in fact). However, this is inefficienty compared to just accepting the 'untidiness' of popping the files in a subfolder of the root level public_html. I, like many commentors above, feel this is the best solution for its speed and appropriateness; just having an 'assets' directory at the same level as the 'system' one, with images, css, js etc inside. You could use an alias or virtual folder to make things feel better for you...
However, there is a third way. There are libraries that do something JUST like what you want, with the added benefit of Minify (from the accepted answer) and compression, or whatever you fancy. The two libraries I know of are called AssetLibPro and Carabiner, and these allow you to specify an asset path (as you want), and then you load your JS and CSS files (with groups e.g. screen, print if needed). They then serve up all related CSS/JS etc as one file; compressed, minified, cached... whatever you need.
Carabiner: http://codeigniter.com/wiki/Carabiner/
AssetLibPro: http://codeigniter.com/forums/viewthread/78931/

Categories