Professional CodeIgniter

(singke) #1

Chapter 3: A 10,000 - Foot View of CodeIgniter


73


Creating the Product View


The header file (header.php) is very simple, consisting of an image, some links to pages, and a form
widget:

< a href=” < ?php echo base_url();? > ” >
< img src=” < ?php echo base_url();? > /images/logo.jpg” border=”0”/ >
< /a >
< div > < ?php echo anchor(“welcome/about_us”,”about us”);? > & nbsp;
< ?php echo anchor(“welcome/contact”, “contact”);? >
< ?php
echo form_open(“welcome/search”);
$data = array(
“name” = > “term”,
“id” = > “term”,
“maxlength” = > “64”,
“size” = > “30”
);
echo form_input($data);
echo form_submit(“submit”,”search”);
echo form_close();
? >
< /div >

In this simple header, you ’ re using the anchor() function to create links to the contact and about_us
pages (remember the functions set up in the controller?), and then using a variety of form helper
functions to create a simple search form.

Specifically, the form_open() function helps you create the form tag. The argument you pass to this
function is the name of the controller and function that handles the form ’ s input. This search form also
uses a form_input() function to build the simple text field that accepts user input. In this case, you ’ re
setting up a simple array containing different values (the field ’ s name, ID, maxlength, and size
attributes) and then passing that into the form_input() function to create the form input.

Finally, you ’ re also making use of the form_submit() and form_close() functions to finish the form.

Most beginning CodeIgniter developers always ask the same question when it comes to such helpers as
anchor() and form_open() , and that is: Why use them? Most developers come to CodeIgniter with a
certain level of comfort with HTML, so they feel that working with these helpers is somehow
unnecessary. Rest assured, however, that once you start using these shortcuts, you will learn to
appreciate them.

For example, any time you use the anchor() tag, the passed - in argument (representing a controller/
function path) stays constant regardless of what server it may reside on. CodeIgniter takes care of
figuring out all the paths.

Some things about this include aren ’ t quite right (e.g., once you load it into your controller you ’ ll see
that the logo image isn ’ t in quite the right place), but for right now, it ’ s good enough to keep going. One
of the beauties of iterative work is the ability to go back and tweak quickly as you need to.

Save this file as header.php in the /system/application/views folder.
Free download pdf