CodeIgniter for Rapid PHP Application Development Improve your PHP coding productivity with the free compact open source MVC CodeIgniter framework 1st Edition David Upton pdf download
CodeIgniter for Rapid PHP Application Development Improve your PHP coding productivity with the free compact open source MVC CodeIgniter framework 1st Edition David Upton pdf download
https://ebookname.com/product/multi-tier-application-programming-
with-php-1st-edition-david-wall/
https://ebookname.com/product/soa-and-ws-bpel-composing-service-
oriented-architecture-solutions-with-php-and-open-source-
activebpel-1st-ed-edition-yuli-vasiliev/
https://ebookname.com/product/building-php-applications-with-
symfony-cakephp-and-zend-framework-1st-edition-bartosz-porebski/
https://ebookname.com/product/dipole-quadrupole-theory-of-
surface-enhanced-raman-scattering-1st-edition-a-m-polubotko/
The Healing Echo Discovering Homeopathic Cell Salt
Remedies Schuessler Tissue Salts 1st Edition Vinton
Mccabe
https://ebookname.com/product/the-healing-echo-discovering-
homeopathic-cell-salt-remedies-schuessler-tissue-salts-1st-
edition-vinton-mccabe/
https://ebookname.com/product/an-introduction-to-english-
phonetics-3rd-edition-richard-ogden/
https://ebookname.com/product/the-dragon-has-two-tongues-essays-
on-anglo-welsh-writers-and-writing-glyn-jones/
https://ebookname.com/product/taste-of-modernity-sufism-
salafiyya-and-arabism-in-late-ottoman-damascus-itzchak-weismann/
https://ebookname.com/product/essential-app-engine-building-high-
performance-java-apps-with-google-app-engine-1st-edition-adriaan-
de-jonge/
Ireland For Dummies 4th edition Dummies Travel
Elizabeth Albertson
https://ebookname.com/product/ireland-for-dummies-4th-edition-
dummies-travel-elizabeth-albertson/
CodeIgniter for Rapid PHP
Application Development
David Upton
BIRMINGHAM - MUMBAI
CodeIgniter for Rapid PHP Application Development
Improve your PHP coding productivity with the free compact
open-source MVC CodeIgniter framework!
All rights reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of
the information presented. However, the information contained in this book is sold
without warranty, either express or implied. Neither the author, Packt Publishing,
nor its dealers or distributors will be held liable for any damages caused or alleged to
be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
ISBN 978-1-847191-74-8
www.packtpub.com
Reviewers Indexer
Rick Ellis Bhushan Pangoankar
Derek Allard
Proofreader
Development Editor Chris Smith
Douglas Peterson
Production Coordinator
Assistant Development Editor Shantanu Zagade
Nikhil Bangera
Cover Designer
Technical Editor Shantanu Zagade
Ajay S
Editorial Manager
Dipali Chittar
About the Author
I’d like to thank Rick Ellis for writing CI and for making it available,
free. This spirit of generosity with such valuable intellectual
property is what makes the Open Source movement a success, and
an example to the rest of us.
I’d also like to thank Rick, and Derek Allard, for undertaking a
technical review of the book and making many helpful suggestions.
Lastly, but not least, my thanks to Julia, John, and James for their
love, support, and patience.
About the Reviewers
Rick Ellis is the founder and CEO of EllisLab.com, the company that develops
CodeIgniter and several other widely used web applications. Rick Ellis has a diverse
background in media technology, having worked in creative and technical capacities
on interactive projects for Disney, to feature films for Oliver Stone, and almost every
kind of web-based project in-between.
[ ii ]
Table of Contents
My 'Display' Model 78
CI's Validation Class: Checking Data Easily 79
Set Up Validation 80
Set Up the Controller 81
Set Up the Forms 81
Summary 83
Chapter 6: Simplifying Sessions and Security 85
Starting to Design a Practical Site with CI 85
Moving Around the Site 86
Security/Sessions: Using Another CI Library Class 91
Turning Sessions into Security 94
Security 96
Summary 98
Chapter 7: CodeIgniter and Objects 99
Object-Oriented Programming 99
Working of the CI 'Super-Object' 100
Copying by Reference 103
Adding Your own Code to the CI 'Super-Object' 105
Problems with the CI 'Super-Object' 106
Summary 109
Chapter 8: Using CI to Test Code 111
Why Test, and What For? 111
CI's Error Handling Class 113
CI's Unit Test Class 115
When to Use Unit Tests 117
Example of a Unit Test 118
CI's Benchmarking Class 121
CI's Profiler Class 122
Testing with Mock Databases 123
Control and Timing 124
Summary 125
Chapter 9: Using CI to Communicate 127
Using the FTP Class to Test Remote Files 127
Machines Talking to Machines Again—XML-RPC 129
Getting the XML-RPC Server and Client in Touch with Each Other 131
Formatting XML-RPC Exchanges 132
Debugging 134
Issues with XML-RPC? 135
Talking to Humans for a Change: the Email Class 136
Summary 139
[ iii ]
Table of Contents
[ iv ]
Table of Contents
[]
Preface
This book sets out to explain some of the main features of CI. It doesn't cover them
all, or cover any of them in full detail. CI comes with an excellent on-line User Guide
that explains most things. This is downloaded with the CI files.
This book doesn't try to duplicate the User Guide. Instead it tries to make it easier for
you to pick up how the CI framework works, so you can decide whether it is right
for you, and start using it quickly.
In some places, this book goes beyond the User Guide, though, when it tries to
explain how CI works. (The User Guide is more practically oriented.) This means
that there are some fairly theoretical chapters in between the "here's how" pages. I've
found that it helps to understand what CI is doing under the hood; otherwise you
sometimes get puzzling error messages that aren't easy to resolve.
I've tried to use a 'real-world' example when showing sections of CI code. I want
to show that CI can be used to develop a serious website with a serious purpose.
I'm currently running several websites for clients, and I want a program that will
monitor them, test them in ways I specify, keep a database of what it has done, and
let me have reports when I want them.
The examples in this book don't show it in full detail, of course: but they do, I hope,
demonstrate that you can use CI to make pretty well any common coding simpler,
and some uncommon stuff as well.
This book steps you through the main features of CodeIgniter in a systematic way,
explaining them clearly with illustrative code examples.
Preface
Chapter 2 explains what happens when you install the site, and which files will be
created. It gives a detailed overview of the required software, and explains the basic
configuration of CodeIgniter.
Chapter 3 explains how MVC helps to organize a dynamic website. It goes further
to explain the process by which CodeIgniter analyzes an incoming Internet request
and decodes which part of your code will handle it. Then CodeIgniter syntax rules
and the different types of files or classes you can find—or write for yourself—on a
CodeIgniter site are explained. At the end of the chapter, some practical hints on site
design are given.
Chapter 4 looks at how you set up a database to work with CodeIgniter, and then
how you use the Active Record class to manipulate the database.
Chapter 5 covers various ways of building views, how to create HTML forms quickly,
and how to validate your forms using CodeIgniter's validation class.
Chapter 6 looks at one of the basic questions affecting any website i.e. session
management and security; we also explore CodeIgniter's session class.
Chapter 7 covers the way in which CodeIgniter uses objects, and the different ways in
which you can write and use your own objects.
Chapter 8 covers CodeIgniter classes to help with testing: Unit tests, Benchmarking,
the 'profiler' and ways in which CodeIgniter helps you to involve your database in
tests without scrambling live data.
Chapter 9 looks at using CodeIgniter's FTP class and email class to make
communication easier, and then we venture into Web 2.0 territory using XML-RPC.
Chapter 10 talks about CodeIgniter classes that help in overcoming problems arising
regularly when you are building a website, for example, the date helper, the text and
inflector helpers, the language class, and the table class.
Chapter 11 looks at several useful CodeIgniter functions and helpers: file helper,
download helper, file upload class, image manipulation class, and the ZIP class.
Chapter 12 covers exploring your config files, using diagnostic tools, and potential
differences between servers, along with some notes on security.
[]
Preface
Chapter 13 shows you how to generalize CRUD operations so that you can do them
with two classes: one for the controller, and one for the CRUD model.
Chapter 14 looks at some coding examples, bringing together a lot of the functions
that have been discussed bit by bit in the preceding chapters.
Chapter 15 looks at some of the resources available to you when you start to code
with CodeIgniter, such as the libraries for AJAX and JavaScript, authentication,
charting, and CRUD.
Conventions
In this book, you will find a number of styles of text that distinguish between
different kinds of information. Here are some examples of these styles, and an
explanation of their meaning.
There are three styles for code. Code words in text are shown as follows: "We can
include other contexts through the use of the include directive."
When we wish to draw your attention to a particular part of a code block, the
relevant lines or items will be made bold:
</head>
<body>
<h1 class='test'><?php echo $mytitle; ?> </h1>
<p class='test'><?php echo $mytext; ?> </p>
</body>
[]
Preface
New terms and important words are introduced in a bold-type font. Words that you
see on the screen, in menus or dialog boxes for example, appear in our text like this:
"clicking the Next button moves you to the next screen".
Reader Feedback
Feedback from our readers is always welcome. Let us know what you think about
this book, what you liked or may have disliked. Reader feedback is important for us
to develop titles that you really get the most out of.
If there is a book that you need and would like to see us publish, please send
us a note in the SUGGEST A TITLE form on www.packtpub.com or
email [email protected].
If there is a topic that you have expertise in and you are interested in either writing
or contributing to a book, see our author guide on www.packtpub.com/authors.
Customer Support
Now that you are the proud owner of a Packt book, we have a number of things to
help you to get the most from your purchase.
[]
Preface
Errata
Although we have taken every care to ensure the accuracy of our contents, mistakes
do happen. If you find a mistake in one of our books—maybe a mistake in text or
code—we would be grateful if you would report this to us. By doing this you can
save other readers from frustration, and help to improve subsequent versions of
this book. If you find any errata, report them by visiting http://www.packtpub.
com/support, selecting your book, clicking on the Submit Errata link, and entering
the details of your errata. Once your errata are verified, your submission will be
accepted and the errata added to the list of existing errata. The existing errata can be
viewed by selecting your title from http://www.packtpub.com/support.
Questions
You can contact us at [email protected] if you are having a problem with
some aspect of the book, and we will do our best to address it.
[]
Introduction to CodeIgniter
Most of us just want to write applications that work well, and to do it as simply and
easily as we can. This book is about CodeIgniter, a tool for making PHP easier to use.
If you need to produce results, if you think that the details and intricacies of coding
are for geeks, then you should look at CodeIgniter (CI to its friends).
CI is free, lightweight, and simple to install, and it really does make your life much
easier. Just read this chapter to find out how:
That's quite a big claim. You have already spent some time learning PHP, HTML,
CSS, a database, and several other acronyms' worth of geek speak. You need a basic,
but not necessarily an expert, knowledge of PHP to benefit from CI.
Introduction to CodeIgniter
Save Time
CI doesn't take long to learn, and it quickly pays for your effort in the time saved
later on. Let's look at a simple measure:
This is not just good for the lazy. The less you type, the fewer mistakes you make,
and the less time you spend debugging your code. The smaller your code is, the
faster it loads and less space it takes up.
Here are two examples (which are explained later on in this book, so don't worry
now about how they work!).
Imagine you are writing a database query. This is how you might write a function
within your PHP programme to query a MySQL database:
$connection = mysql_connect("localhost","fred","12345");
mysql_select_db("websites", $connection);
$result = mysql_query ("SELECT * FROM sites", $connection);
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
foreach ($row as $attribute)
print "{$attribute[1]} ";
}
Compare the character counts: 244 for the traditional syntax; 112 for CI.
[]
Chapter 1
Now let's imagine that you are writing a data entry form in HTML, and you want
a drop-down query box. Let's say this drop-down query box shows three options
and allows the user to select one of them. In HTML, a drop-down box can be created
like this:
<select name="type">
<option value="1">www.this.com</option>
<option value="2">www.that.com</option>
<option value="3" selected>www.theother.com</option>
</select>
CI's version is both shorter and, because it works from an array, more adapted to
PHP processing:
$urlarray = array(
'1' => 'www.this.com',
'2' => 'www.that.com',
'3' => 'www.theother.com',
);
Then, you decide to move the site to another URL. That means you have to go
painstakingly through your code, looking for each URL, and re-writing it, or else
none of your links will work.
[]
Introduction to CodeIgniter
CI also encourages you to put the URL of your site in a configuration file that the
rest of your site can access. CI's anchor function that we've used here automatically
refers to that configuration file. So, when you come to move your site, you only
need to change that one entry in the configuration file, and all your hyperlinks
update automatically.
$temp = '__TEMP_AMPERSANDS__';
$str = htmlspecialchars($str);
return $str;
}
[ 10 ]
Chapter 1
This is code that handles special characters like '&'; so that they don't cause confusion
while your form is being submitted. As you can see, there is some quite tricky regex
code in there.
Possibly you like typing out regexes. Some people like lying on beds of nails, some
like listening to ABBA; it's a free country. (Well, it is where I'm writing this.) But
if you don't like these things, you can let CI do them for you (the regexes, I mean,
not ABBA), and you needn't even be aware of the code that's working away in the
background for you, every time you write that one simple line of code:
echo form_input('username', 'johndoe');
CI does the rest. Working behind the scenes, for example, is a function that sorts out
MIME types for nearly hundred different types of attachment. So it knows that
your photo, photo1.jpg,��������������������������������������������������������
is an 'image/jpeg' MIME type. It remembers to generate
[ 11 ]
Introduction to CodeIgniter
boundary delimiters in the right places around your attachments. It takes care of
wrapping your text, and it allows you to easily mark out chunks of text you don't
want wrapped.
Run this, and you find a ZIP archive on your C drive containing one file. Your ZIP
filer reader will unzip it and produce the original data for you.
People who use your site won't know that you've produced this impressive result
so easily. They'll be impressed! Your site will save bandwidth. You did it in minutes
rather than hours.
At one level, a framework is just that: lots of chunks of code, stored in separate files,
which simplify the coding of repetitive operations.
[ 12 ]
Exploring the Variety of Random
Documents with Different Content
The Project Gutenberg eBook of The Tale of a
Field Hospital
This ebook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this ebook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.
Language: English
BY
SIR FREDERICK TREVES, BART.
G.C.V.O., C.B., LL.D.
NEW EDITION
CONTENTS
CHAPTER
I
THE FIELD HOSPITAL
II
FRERE CAMP
It was from Frere Camp that the army under General Buller started
for the Tugela River, and the Hospital pitched its tents in that camp
on the evening of Monday, December 11th, 1899. We went up from
Pietermaritzburg by train. The contents were soon emptied out on
the line, some little way outside Frere Station, and close to the
railway the Hospital was put up. That night we all slept under
canvas--many for the first time--and all were well pleased that we
had at last arrived at the front.
Frere is merely a station on the line of rail which traverses
Natal, and as it consists only of some three or four houses and a few
trees it can hardly be dignified by the name of hamlet. Frere is
simply a speck--a corrugated iron oasis--on the vast undulating
plains of the veldt. These plains roll away to the horizon, and are
broken only by kopjes and dongas and the everlasting ant-hills.
On the way towards Ladysmith are a few kopjes of large size,
from any one of which the line of the Tugela can be seen, with the
hills beyond, occupied by the Boer entrenchments, and over them
again the hills which dominate Ladysmith. On the way towards
Estcourt winds a brown road, along which an endless train of ox-
wagons rumble and are lost in the wilderness of the camp.
The river which is reputed to "run" through Frere has long since
ceased to run. The water is retained by certain dams, and the pools
thus formed are uninviting. The water is the colour of pea-soup, and
when in a glass is semi-opaque and of a faint brownish colour. The
facetious soldier, as he drinks it, calls it "khaki and water."
In the lowest pool, immediately above the iron railway bridge
which has been blown up by the Boers, Tommy Atkins bathes with
gusto in what is seemingly a light-coloured mud. Here also he
washes his socks and his shirts.
The centre of the camp is the railway station, and that of Frere
is the smallest and most unpretending that any hamlet could
pretend to. It is, however, crowded out of all reason, and its
platform of hard earth is covered with boxes and baggage and sacks
and saddles in as much disorder as if they had been thrown in panic
from a burning train. Between the little goods shed and the little
booking-office are several stands of rifles. A sentry, proud apparently
in his covering of dust, is parading one end of the platform, while at
the other end a motley crowd of perspiring soldiers are filling water-
bottles at the tank which supplies the engine. In the waiting-room a
tumbled mass of men are asleep on the floor, while on a bench in
front of it two men-of-war's men are discussing an English paper six
weeks old.
Outside the station are ramparts of provision boxes and cases of
ammunition, and iron water cisterns and mealie bags, and to the
fragments of a railing which surrounds the station horses, of all
kinds and in all stages of weariness, are tied.
A ragged time-table on the wall, dealing with the train service to
Pretoria, and with the precise hour of the arrival of the trains there,
seems but a sorry jest. The stationmaster's house has been looted,
and the little garden in front of it has been trampled out of being,
save for two or three red geraniums which still bloom amidst the
dirt. This house is, for the time, the general's headquarters, and
before it waves the Union Jack.
When we reached the camp it was stated that 30,000 men were
under canvas. A camp of this size must of necessity present an
endless scene of bustle and movement. Nothing seemed at rest but
the interminable array of white tents and the rows of baggage
wagons. Cavalry would be moving in one direction and infantry in
another. Here a mounted patrol would be riding out or a couple of
scouts coming in. There would be a long line of Kaffirs carrying bales
and boxes to a temporary depot, and here a troop of eager horses
hurrying to the river to drink. Gallopers would be seen in all
directions, and everywhere would be struggling teams of oxen or of
mules enveloped in clouds of dust and urged on by sweating men
and strange oaths, and by the shrill yells of the Kaffir drivers, whose
dust-dried throats gave out noises like the shrieks of parrots.
There was no shade of any kind, and the camp during the day
lay dry, dusty, parched and restless under a blazing sun, but at night
there was a cool wind and cheery camp fires, and a darkness which
blotted out the dusty roads, the dried-up river, the dismal piles of
stores, and the general picture of a camp in a desert of baked earth.
Every night a search-light was at work sending dispatches to
Ladysmith, and almost every morning could be heard the Boer guns
thundering over that unhappy place.
The British soldier looked very smart in his khaki suit when
embarking at Southampton, but at Frere he showed the effects of
wear, and his tunic, his belt, his pouches, his boots and his face, had
all toned down to one uniform tint of dirt colour. He was of the earth
earthy. He was unshaven. His clothes had that abject look of want of
"fit" that is common to clothes which have been slept in, which have
been more than once soaked through, and which have more than
once dried upon the body of the owner.
III
THE HOSPITAL DOG
IV
THE MORNING OF COLENSO
V
THE HOSPITAL UNDER THE RIDGE
VI
INSIDE AN OPERATION-TENT
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com