Php: dynamic web gallery
Webworks – A Workshop Series in Web Design (Session #3)
Table of Contents: 1. Introduction – Theory of PHP based layout 2. Advanced PHP Control Structures 3. Variables 4. Looking at the Code 5. Further Resources
1. Introduction
For those unfamiliar with PHP (Hypertext PreProcessor), it is important to emphasize that it is a web based language that is usuallyintermixed with HTML. Pages with PHP code are processed first by the server and then sent to the browser of a viewer. So called “dynamic” sites are created because the page is processed beforehand according to certain variables set or information present to dictate how the page or content is to be served. When using PHP to design layouts such as web galleries, the basic principal is: 1. You set avariable 2. This variable is processed to relate how a page is displayed 3. The corresponding HTML code or any other code is then written into the page and sent for viewing. Now it’s just up to you how you achieve certain effects.
2. Advanced PHP Control Structures
During this workshop, we will be using several control structures that may differ from other languages. Custom Functions A customfunction is written in the format: function NameOfFunction(Argument){ CodeForFunction; } Example: function show_image_id($image){ echo ‘This is image’.$image; } As your pages become more complicated, it is good programming habit to keep any specialized feature that is repeated in a function.
Webworks: Dynamic Web Gallery
Page 2 of 11
Callback Functions Callback functions are functions whichevaluate a given variable and return a corresponding response. Typical uses deal with arrays containing data that needs filtering or altering. A callback function is used to do the filtering or altering. For today’s workshop, we will be using callback functions with the built in array_filter() function. Array_filter() The array_filter() function basically filters a given array according to rulesgoverned in a callback function. The format is: NewArray = array_filter(ArrayToBeFiltered,”callbackfunction”); Example: //Callback function function positiveNumber($var){ return $var>0; } //your array of numbers $array_to_be_filtered = array(1, -2, 4, 7, -10); //using array_filter() $newarray = array_filter($array_to_be_filtered,”positiveNumber”); //your new array would look like this: $newarray = (1,4, 7); Array_search() The array_search() function accepts a string and searches an array for that element. When it finds it, it returns the corresponding key. If no element is found, FALSE is returned. The format is: NewArray = array_search(“string”,ArrayOrString); Example: //Your Array $target = array(“one”, “two”, “three”); //Search $search = array_search(“two”,$target); $search would benumber: 1 (Note: Remember array keys begin with 0) To access the searched element, you would then just use: $target[$search];
Workshop Committee of Technology Assist By Students
Webworks: Dynamic Web Gallery
Page 3 of 11
Opendir() The opendir() function accepts a string that is a directory path and opens the directory into a handle to which you can then call other directory relatedfunctions, such as readdir() which we will get to. Format is: Handle = opendir(‘directoryPath’); Example: $dir = opendir(‘images/icons/’); (Note: make sure you include a ‘/’ after your
last folder)
Readdir() The readdir() function accepts a string that is a handle for a directory and return the filename of the next file in the folder. If there is no next file, it will return a FALSE and stop the loop.Format is: $file = readdir(directoryHandle); Example: //Open a directory $dir = opendir(‘images/icons/’); //Now read a file from it $file = readdir($dir); Note: The first two values of readdir() will return the values ‘.’ , ‘..’ which are directory navigation entities. For our project here and for usual projects, you will not need them, so you can use the unset() function to delete them....
Regístrate para leer el documento completo.