Calling a view "mid-script" with Codeigniter...can it be done? - php

I'm using the Codeigniter framework and have tried to stick with the MVC philosophy as best I can. I've recently run into an issue that I'm not entirely sure how to handle.
I have a PHP script running that takes a fairly long time (creating an index for my search engine). What I want to do is have the html show up as it's running.
If I just put echo statements into my controller, and echo every time a document is indexed, it does what I want. I can watch the progress as it happens.
But that doesn't correspond with an MVC philosophy, and I also don't get any of the nice styles and html formatting that is set up for my view classes.
The only way I know to interact with my view is to run the code in the controller, and store the log info into a variable, then pass it to the view when I'm done. But that means I just sit waiting for ages, and finally a big log display shows up (nicely formatted, mind you).
Does anyone know how to do what it is I'm trying to achieve? Use a View, but before the Controller is "done" yet?

You can load multiple view's in a controller, how about loading and displaying a view first and then doing more processing.
There is even the possibility of loading a page (like an outside frame or something) and then using JQuery to load the content that is taking a long time to load.

Related

Ajax (client) vs PHP (server), Loading DB Data

I am embarking on part of a project now that has me planning on how to load a dynamic table data from a DB. I have uncovered two basic methods.
I believe that I can use url query strings to communicate with the php backend of my phpbb3 forum. And it can load the appropriate data and ship it off to the user in full static page chunks. So I would have something like /stats.php?page=3&orderby=name&dir=desc.
Or I can just send the same empty page to everyone and the browser can dynamically load anything that the user wants using ajax.
Or some combination of the two.
What is best practice? What are the downsides and upsides of both?
It really depends what you're trying to do. For simplicity's sake, I would say that the first option (just load it with the appropriate query string variables in the URL) is better.
Rendering a page using AJAX is pretty much always more complicated. However, it also gives you much more control over the UI if you know what you're doing. From my experience, if you want your page to be more like a "web app" with dynamic things happening everywhere, it is much easier to simply load JSON data from the server via AJAX and to dynamically create views via some sort of templating system. Otherwise you're stuck with loading the DOM with PHP, and then somehow communicating that data to your JavaScript, either by using data-XXX attributes on DOM elements, having PHP output a JSON string at the top of a page and assign it to a JavaScript variable, etc. It could get very complicated and convoluted.
In your case it looks like you're just trying to allow users to view certain data from your forum. Barring any additional requirements, I would recommend going with the first option because it will be much easier. This is simple enough that you don't seem to need to load anything dynamically.
A good rule of thumb is the more complicated and dynamic your UI, the more you should think about moving to a "web app" framework, and just let the server act as a REST server.

Block webpages which are only used for dynamic content retrieval through AJAX. (.htaccess?)

I would like to know if it's possible to make pages invisible for users with .htaccess or something else.
Some background info and some other questions :
I'm building a web application which does a lot of dynamic content loading. At the moment i'm just doing ajax calls on a ajax.php file and i give a POST parameter with the name : action. Depening on this action I call a method of a class, this class then echo's JSON or HTML.
But i'm starting to get at the point, that i have functions who return almost a complete html page (SQL and php in between) which kinda looks dirty. Is it a better option to build seperate .php pages for this content, and then load the content just from that page? (I use Jquery, so that would be the .load function as call back)
The problems i see with this method is that people can find those .php pages and access them, and then see information that's not relevant and has no lay out or something. So i want to block these pages. How do I do this with .htaccess?
Now if u think i'm doing stuff completely wrong, just tell. A basic situation in my implementation :
Fill in a client number in a search form -> press enter -> ajax call with post parameters action : getclientinfo , id : the value in the field -> ajax.php gets the correct class and method -> .class.php executes his method and checks if the input is ok, if so it constructs a page with content from the database -> All this gets returned as JSON something like :
{ success : OK; content : the complete content }
-> then with jquery i check the success boolean, and then load the complete content into the current page.
Is this a good method, or am i doing things bad ?
The thing i'm not feeling myself ok with, is the ajax.php page. What's usually done ?
Thanks in advance. I got many answers from this website, first time ever I'm asking one myself.
Seems like you have two problems: access control and organization.
It'd be best to lock access to pages with a rudimentary authentication level programmatically (in PHP, using session data). Otherwise, the most rudimentary form of access control is to restrict with apache's .htaccess file, but, rules for query string / post can be confusing. See this example: Is it possible to redirect post data?
Since that kind of locking down is complex, it might be logical to dump AJAX and render full, complete pages instead.
In either way, what you seem to be conceptually illustrating is a need for separating of content and logic. This is good. However, it's time to take your development process to the next level.
Rewrite your code to where only logic and data querying takes place in PHP files. Secondly, output all HTML in your pages by using the Smarty library http://www.smarty.net/docs/en/
The Smarty templating system is pretty extensible. It has inheritance capabilities so you can produce page variations, template includes, and otherwise run circles around HTML. A handful of Smarty statements can make your life so much easier.

simple ajax mvc type structure

How do you go about designing/structuring a completely ajaxed MVCish style site? I guess what confuses me the most is that there would only be one view that adapts and changes to the user actions...
I'm looking to build a really simple app, both front end and back end in this style.
Yeah, you'd be building a "front controller" of sorts that would route the views (there would be more than one) to the page for rendering in divs. My company does this with two of its apps and it's lightning fast and a great way to go for a simple app...users love it. Key to the success is a well-formulated layout with well-defined divs to receive the content. As you'll be repeatedly writing and re-writing to them, you have to ensure ahead of time that they are able to handle data of various sizes and amounts as you'll have very few ways to effect layout per page on the overall container....besides after-the-fact hacks (just say no!)
Do as you would with controllers and models and the views (the front-end view code) In some cases our code will use an intermediary page (we call it a mid-model) to generate Jquery data in a JSON string format. It's not quite a model in that case, as it takes action like a model but sometimes returns more than just JSON depending on the needs of the Jquery element.
An interesting offshoot of this system is the use of a program called XAJAX. It's a PHP library that facilitates AJAX called directly to PHP functions, so it eliminates the need to do intermediary JSON generating pages like Jquery uses. For those who understand PHP but struggle with Javascript, this can be an easier solution to grasp. While the documentation hasn't proven very strong, it's a very powerful tool.
I would do all your views as usual (except they would only be HTML fragments that would be inserted into the page) and then have an extra "special" controller and view that loads your views via Ajax. When a link is clicked or something like that the JavaScript makes an Ajax request to the special controller with the view you want to load. The special controller then renders the view and sends it back to be inserted into the page.

Caching one section of a CodeIgniter page

I'm using codeigniter to create a web app. I'm creating the drop down menus in the header. These are included on every page using load->view(). To populate the menus some quite complex SQL is used. The contents on the menu will change infrequently (once/twice a week), whereas the rest of the data on the page is constantly changing.
Therefore I don't want the overhead of running this SQL everytime a page is loaded. I looked at using CI caching but its on a page level. I really only want to cache a small segment.
What do you think the best approach to this is? I thought about writing this to a text file, but then you'd have to manually run the code to write this every so often.
Take a look at the query caching page, or alternatively you can use a caching library to cache sections of a page (preferred method IMHO) take a look at KhCache.
Another good caching library I have found is from Phil Sturgeon. http://github.com/philsturgeon/codeigniter-cache
It allows caching models, libraries, or just partial caching like you describe.

Migrating from POST forms to CSS

I've been tasked to migrate a web application to a more 'modern feeling' AJAX web 2.0 deal. The application as is currently uses PHP to pull data from the database, present the user with forms, and then update the database based on those form submissions. Frames are used to have a persistent main navigation menu, and a content area where the actual
So each php script basically looks for $_POST information; if there is none, it shows the user database data, otherwise it updates data ( provided it is proper data ) and then show the user the result. There are simple get navigations that show subsets.
To migrate this to a AJAX site with a css layout with the content changes happening inside a div, I'm precluded from using POST, because that refreshes the whole page, right? ( I mean I could, but that would be wasteful -- I don't need to regenerate the whole page when only a small part changes.) So basically, the whole task is using Javascript to read the form information, send an XML HTTP Request, and display results? That sounds like a lot of re-writing the existing php funcitonality in javascript, which I would hope to avoid.
Have I understood the task correctly? Are there libraries or frameworks that can help me?
You have two problems here, which are related in some ways, but shouldn't be simply lumped together.
CSS for layout
Only loading part of pages when forms are submitted
I'd work on the separately (while keeping the other in mind as you do so)
First, I suggest you focus on moving to web standards based pages — without introducing Ajax.
It is true that there are some inefficiencies to be had when reloading the whole page, but this approach is simple and relatively easy to debug.
While you do this, consider separating out your display and business logic. The MVC pattern works well for this.
CakePHP is a popular MVC framework that might help.
Once you have a working system, then you can worry about using Ajax. Follow the principles of progressive enhancement.
If you have separated our your display logic from the business logic you should find it relatively simple to reuse your existing code with a different View that provides the data you care about in a JavaScript friendly format (such as JSON).
You can process this to update the parts of the page you care about.
Frameworks that can help you include YUI and jQuery.
I wrote a simple example last year. Lines 51 onwards of the main script pump data into either an HTML template for processing directly by the browser or a JSON module for processing with JS. There isn't a great deal of duplicate effort there since the code for looking at the parameters sent by the user and extracting data from the DB based on it is shared.

Categories