Thursday, 1 October 2015

MVC Architecture In PHP

MVC Architecture


mvc

Model
The Model is the name given to the permanent storage of the data used in the overall design. It must allow access for the data to be viewed, or collected and written to, and is the bridge between the View component and the Controller component in the overall pattern.
One important aspect of the Model is that it’s technically “blind” – by this I mean the model has no connection or knowledge of what happens to the data when it is passed to the View or Controller components. It neither calls nor seeks a response from the other parts; its sole purpose is to process data into its permanent storage or seek and prepare data to be passed along to the other parts.
The Model's job is to represent the problem domain, maintain state, and provide methods for accessing and mutating the state of the application. The Model layer is typically broken down into several different layers:
  • Service layer - this layer provides cohesive, high-level logic for related parts of an application. This layer is invoked directly by the Controller and View helpers.
  • Data Access layer - (ex. Data Gateway, Data Access Object) this layer provides access to the persistence layer. This layer is only ever invoked by Service objects. Objects in the data access layer do not know about each other.
  • Value Objects layer - this layer provides simple, data-oriented representations of "leaf" nodes in your model hierarchy.

View
The View is where data, requested from the Model, is viewed and its final output is determined. Traditionally in web apps built using MVC, the View is the part of the system where the HTML is generated and displayed. The View also ignites reactions from the user, who then goes on to interact with the Controller. The basic example of this is a button generated by a View, which a user clicks and triggers an action in the Controller.
The View's job is to translate data into a visual rendering for response to the Client (ie. web browser or other consumer). The data will be supplied primarily by the Controller; however, the View may also have a helper that can retrieve data that is associated with the rendering and not necessarily with the current request

Controller
The final component of the triad is the Controller. Its job is to handle data that the user inputs or submits, and update the Model accordingly. The Controller’s life blood is the user; without user interactions, the Controller has no purpose. It is the only part of the pattern the user should be interacting with.
The Controller can be summed up simply as a collector of information, which then passes it on to the Model to be organized for storage, and does not contain any logic other than that needed to collect the input. The Controller is also only connected to a single View and to a single Model, making it a one way data flow system, with handshakes and signoffs at each point of data exchange.
It’s important to remember the Controller is only given tasks to perform when the user interacts with the View first, and that each Controller function is a trigger, set off by the user’s interaction with the View.
The Controller's job is to translate incoming requests into outgoing responses. In order to do this, the controller must take request data and pass it into the Service layer. The service layer then returns data that the Controller injects into a View for rendering. This view might be HTML for a standard web request; or, it might be something like JSON (JavaScript Object Notation) for a RESTful API request.


Saturday, 18 July 2015

JSON Introduction

JSON - JavaScript Object Notation

               JSON is a lightweight text-based open standard data-interchange format. It is human readable. JSON is derived from a subset of JavaScript programming language. It is entirely language independent and can be used with most of the modern programming languages.
Key Points:

1.    There are four basic data types strings, numbers, booleans and null.
2. Objects are wrapped within ‘{’ and ‘}’. It is list of label – value pair.
3. Array are enclosed by ‘[’ and ‘]’. It is list of values.
4. Both object and array can be nested.
with the reference for more

json structure
 W3resources
Advantages of JSON:
1. Faster -:  JSON syntax is very easy to use. Which provide us a easy parsing of the data and faster execution of the data.
2. Portable -: It has wide range of supported browser compatibility with the operating systems so doesn’t require much effort to make it all browser compatible. JSON is portable because parsers and writers are available for many, many languages. It is the best way to transmit complex structures like arrays and objects, and have it still be compatible with multiple languages.
3. Server Parsing -: JSON server side parsing is strong point that indicates us to use the JSON on server side.
4. Tool for sharing data -: JSON is the best tool for the sharing data of any size even audio, video etc. This is because JSON stores the data in the arrays so data transfer makes easier. For this reason, JSON is a superior file format for web APIs and for web development.
5. Structure -: JSON provides structure because the data you transmit with it can have consistent formatting. This is instead of transmitting back plain-text (i.e. unformatted) data, like comma-separated or delimited data.
6. API -: API is the most widely used area where JSON is used for data exchange. Specially, web applications those have a social face, it has become obvious now that they have an API, so that developers can consume the huge amount of data collected by the app and then can create derivative apps.

Wednesday, 3 June 2015

Upload multiple images using Ajax

Upload multiple images using Ajax

index.html

<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function (e) {
    $("#uploadF").on('submit',(function(e) {
        e.preventDefault();
        $.ajax({
            url: "upload.php",
            type: "POST",
            data:  new FormData(this),
            contentType: false,
            cache: false,
            processData:false,
            success: function(data){
            $("#gallery").html(data);
            },
              error: function(){}            
       });
    }));
});
</script>
</head>
<body>
    <div class="gallery-bg">
            <form id="uploadF" action="" method="post">
                <div id="gallery">No Images in Gallery</div>
                       <div id="uploadFormLayer">
                            <p class="txt-subtitle">Upload Multiple Image:</p>
                            <!-- <p><input name="userImage" type="file" class="inputFile" /><p>-->
                            <p><input name="userImage[]" type="file" /><p>
                          <p><input name="userImage[]" type="file" /><p>
                          <p><button id="Submit" value="Submit" name="Submit">Submit</button><p>
</div>
</form>

<input type="submit" value="Submit" />
</div>
</body>
</html>




upload.php


<?php
if(is_array($_FILES))
{
    foreach ($_FILES['userImage']['name'] as $name => $value)
    {
        if(is_uploaded_file($_FILES['userImage']['tmp_name']))
        {
            $sourcePath = $_FILES['userImage']['tmp_name'];
            $targetPath = "images/".$_FILES['userImage']['name'];
            if(move_uploaded_file($sourcePath,$targetPath))
            {
?>
                <img src="<?php echo $targetPath; ?>" width="150px" height="180px" />
<?php
            }
        }
    }
}
?>

Thursday, 21 May 2015

Codeigniter Basic and Imp Thinks

Codeigniter Basic


1. Perfect MVC Architecture
The model, view, controller architecture is nothing new. It seems like all the coding frameworks are MVC now a days, and if they aren’t it can be configured easily. The MVC way of doing things offers nice code separation and keeps things clean. Some frameworks force you to do things by the books but CI lets you use MVC in a way that makes sense you. 

2. Little to no server requirements.
Unlike other PHP frameworks, CI works with both PHP 4 and 5. That makes the lives of someone like me who has to be able to work seamlessly between the two environments much easier.

3. Easy to understand and extend.
CI is the first framework that I used that actually makes sense to me. I was able to get up and running with CI the quickest. CI is also easy to write new libraries, change the behavior of existing libraries and just change the overall behavior of the framework with little effort.

4. All the tools in one little package.
Calendar, e-mail, zip encoding, validation, uploading, sessions, unit testing… that is just a few of the built in libraries that come with CI. It also includes a boat load of default helpers for things like forms, file handling, arrays, strings, cookies, directories and more. Plus, if that wasn’t enough, you can create your own libraries and helpers or use code that has been developed by the CI community.

5. No “installation” necessary.
CI fits this requirement nicely. No need for PEAR packages or server modifications to get the framework up and running. Just upload the files to your server and your off.

6. Built in security tools.
CI allows you to implement as much or as little security as you feel is necessary for your app. It does some things by default like unsetting all global variables regardless of PHPs register_globals directive and turning off the magic_quotes_runtime directive during system initialization so that you don’t have to remove slashes when retrieving data from your database. Other things can be enabled like cookie encryption, handling session data with a database and automatically escaping SQL queries.

7. Database abstraction.
Every decent framework has a database abstraction layer now a days and CI is no exception. You can easily create insert, update and delete statements without needing to write raw SQL. Handle connections to multiple databases within one application

8. Active user community.
There is a nice and big user community to work with when you have a problem or question. The CI website has a forum and wiki when you’re looking for answers. No confusing group mailing lists or chat channels just to get a quick answer to a question.

Sunday, 17 May 2015

Working of Ajax


AJAX

AJAX = Asynchronous JavaScript and XML.
AJAX is a technique for creating fast and dynamic web pages.
AJAX can be used for interactive communication with a database.
AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.
Classic web pages, (which do not use AJAX) must reload the entire page if the content should change.


How AJAX Works
ajax working