Sunday, February 6, 2011

Building a basic PHP E-Commerce Application using CodeIgniter Framework – Part 3

Part III – Creating the site mockup and Preparing our View.

part iii
In this part of the series we will create a design mockup for our e-commerce site. And create a reusable component of our view.
First we need to know how our site should looks like. And how we showcase our products. Basically our site should have a navigation menu, a header with a logo, a content body, a footer, and a search form.

In our homepage we need to show some of our products and put a featured product area where we can show three or more items, a main menu for our ‘home’, ‘about’, ‘contact us’ and other necessary page we need later as we go on this tutorial, and a side navigation menu for our product categories.
Take a look the site mockup I have created.

Creating the Mockup Template.

We need to create a folder where we put our files needed in making the template such as the css, images, and JavaScripts files. Now, go to the root of your CI installation then create a folder and name it as you like, in this tutorial we name it ‘public’ then inside the public folder we need to create the following folders ‘css, ‘images’, ‘js’, and ‘products’ folder for the images of our products and a ‘thumb’ folder inside our products folder for the product thumbnails.
Directory structure we have just created.

Open system/application/views folder, create a new file named it to template.php, open it in your text editor then populate the code below. Or you can go download my code here.
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  
  2.   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
  3.   <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  
  4.   <head>  
  5.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  6.   <meta name="language" content="en" />  
  7. <title>Building an e-commerce application using CodeIgniter</title>  
  8.   <link rel="stylesheet" href="css/default.css" type="text/css" />  
  9.   <script type="text/javascript" src="js/jquery.js"></script>  
  10.   <script type="text/javascript" src="js/s3Slider.js"></script>  
  11.   <script type="text/javascript">  
  12.   $(document).ready(function() {  
  13.   $('#slider1').s3Slider({  
  14.   timeOut: 8000  
  15.   });  
  16.   });  
  17.   </script>  
  18.   </head>  
  19.   <body>  
  20.   <!-- wrap starts here -->  
  21.   <div id="wrap">  
  22.   <!--header -->  
  23.   <div id="header">  
  24.   <h1 id="logo-text"><a href="#">INSIC Shop</a></h1>  
  25.   <p id="slogan">E-Commerce Site Powered by CodeIgniter</p>  
  26.   </div>  
  27.   <!-- navigation -->  
  28.   <div  id="menu">  
  29.   <ul>  
  30.   <li ><a href="home">Home</a></li>  
  31.   <li ><a href="about-us">About Us</a></li>  
  32.   <li ><a href="contact">Contact Us</a></li>  
  33.   </ul>  
  34.   </div>  
  35.   <!-- content-wrap starts here -->  
  36.   <div id="content-wrap">  
  37.   <div id="main">  
  38.   <h2>Featured Product</h2>  
  39.   
  40.   <div id="slider1">  
  41.   <ul id="slider1Content">  
  42.   <li class="slider1Image">  
  43.   <a href=""><img src="products/product-3.jpg" alt="1" /></a>  
  44.   <span class="bottom"><strong>Product 3</strong><br />Praesent eros nibh, In risus nunc, tincidunt nec, bibendum ac, adipiscing vitae, enim. Aliquam faucibus volutpat elit. Aenean eu lacus. Sed arcu. Mauris aliquam.<br /><a href="#">Buy Now</a> &raquo;</span></li>  
  45.   <li class="slider1Image">  
  46.   <a href=""><img src="products/product-2.jpg" alt="2" /></a>  
  47.   <span class="bottom"><strong>Product 2</strong><br />Praesent eros nibh, In risus nunc, tincidunt nec, bibendum ac, adipiscing vitae, enim. Aliquam faucibus volutpat elit.<br /><a href="#">Buy Now</a> &raquo;</span></li>  
  48.   <li class="slider1Image">  
  49.   <a href=""><img src="products/product-1.jpg" alt="3" /></a>  
  50.   <span class="bottom"><strong>Product 1</strong><br />Praesent eros nibh, In risus nunc, tincidunt nec, bibendum ac, adipiscing vitae, enim.<br /><a href="#">Buy Now</a> &raquo;</span></li>  
  51.   <div class="clear slider1Image"></div>  
  52.   </ul>  
  53.   </div>  
  54.   
  55.   <h2>Our Products</h2>  
  56.   <div class="box-list"><img  src="products/thumb/product-1.jpg" width="120px"><strong><a href="#">Product 1</a></strong><br />Short description goes here<br /><span class="button"><a href="#">Buy Now</a> &raquo;</span></div>  
  57.   <div class="box-list"><img  src="products/thumb/product-2.jpg" width="120px"><strong><a href="#">Product 2</a></strong><br >Short description goes here<br /><span class="button"><a href="#">Buy Now</a> &raquo;</span></div>  
  58.   <div class="box-list"><img  src="products/thumb/product-3.jpg" width="120px"><strong><a href="#">Product 3</a></strong><br >Short description goes here<br /><span class="button"><a href="#">Buy Now</a> &raquo;</span></div>  
  59.   <div class="box-list"><img  src="products/thumb/product-3.jpg" width="120px"><strong><a href="#">Product 4</a></strong><br />Short description goes here<br /><span class="button"><a href="#">Buy Now</a> &raquo;</span></div>  
  60.   <div class="box-list"><img  src="products/thumb/product-1.jpg" width="120px"><strong><a href="#">Product 5</a></strong><br >Short description goes here<br /><span class="button"><a href="#">Buy Now</a> &raquo;</span></div>  
  61.   <div class="box-list"><img  src="products/thumb/product-2.jpg" width="120px"><strong><a href="#">Product 6</a></strong><br >Short description goes here<br /><span class="button"><a href="#">Buy Now</a> &raquo;</span></div>  
  62.   </div>  
  63.   
  64.   <div id="sidebar">  
  65.   <h2>Product Search</h2>  
  66.   <form action="#" class="searchform">  
  67.   <p>  
  68.   <input name="search_query" class="textbox" type="text" />  
  69.   <input name="search" class="button" value="Search" type="submit" />  
  70.   </p>  
  71.   </form>  
  72.   <h2>Product Category</h2>  
  73.   <ul class="sidemenu">  
  74.   <li ><a href="#">Category 1</a></li>  
  75.   <li ><a href="#">Category 2</a></li>  
  76.   <li ><a href="#">Category 3</a></li>  
  77.   <li ><a href="#">Category 4</a></li>  
  78.   </ul>  
  79.  </div>  
  80.   <!-- content-wrap ends here -->  
  81.   </div>  
  82.   <!--footer starts here-->  
  83.   <div id="footer">  
  84.   <p> &copy; 2008-2009 <a href="http://insicdesigns.com">INSIC Designs.</a> |  
  85.   Valid <a href="http://validator.w3.org/check?uri=referer"> XHTML </a> | <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a> </p>  
  86.   </div>  
  87.   <!-- wrap ends here -->  
  88.   </div>  
  89.   </body>  
  90.   </html>  
If you noticed there are three include files for our template first the default.css and the two javascript file, the jQuery and the s3slider.js. I am using the s3slider Plugin. We will be using this javascripts to showcase our featured products. This files are also included in the downloads.
Lets create our css file, open public/css folder and create a new file named it to default.css then populate the css code below.
  1. * { margin: 0; padding: 0; outline: 0; }  
  2.   
  3. body {  
  4.     background#caced1 url(../images/body-bg.gif) repeat-x top;  
  5.     font: 70%/1.5em VerdanaTahomaarialsans-serif;  
  6.     color#777;  
  7.     text-aligncenter;  
  8.     margin: 0;  
  9. }  
  10.   
  11. /* links */  
  12. a, a:visited {  
  13.     text-decorationnone;  
  14.     background: inherit;  
  15.     color#FB9233;  
  16. }  
  17. a:hover {  
  18.     text-decorationunderline;  
  19.     background: inherit;  
  20.     color#93C600;  
  21. }  
  22.   
  23. /* headers */  
  24. h1, h2, h3 { font-family'Trebuchet MS'TahomaSans-serif;  }  
  25. h1 {  
  26.     font-size: 180%;  
  27.     font-weightnormal;  
  28.     color#555;  
  29. }  
  30.   
  31. h2 {  
  32.     font-size: 160%;  
  33.     color#88ac0b;  
  34.     font-weightnormal;  
  35. }  
  36. h3 {  
  37.     font-size: 135%;  
  38.     color#666666;  
  39.   
  40. }  
  41.   
  42. /* images */  
  43. img {  
  44.     background#fff;  
  45.    border1px solid #E5E5E5;  
  46.     padding5px;  
  47. }  
  48.   
  49. img.float-rightright { margin5px 0px 10px 10px;  }  
  50. img.float-left { margin5px 10px 10px 0px; }  
  51.   
  52. h1, h2, h3, p {  
  53.     margin10px 15px;  
  54.     padding: 0;  
  55. }  
  56.   
  57. ol {  
  58.     margin5px 15px;  
  59.     padding: 0 25px;  
  60. }  
  61.   
  62. /* search form */  
  63. .searchform {  
  64.     background-colortransparent;  
  65.     bordernone;  
  66.     margin: 0;  
  67.     padding5px 0 5px 0;  
  68.     width180px;  
  69. }  
  70. .searchform p { margin: 0; padding: 0; }  
  71. .searchform input.textbox {  
  72.     width110px;  
  73.     color#777;  
  74.     padding2px;  
  75. }  
  76. .searchform input.button {  
  77.     width55px;  
  78. }  
  79.   
  80. /******************************************** 
  81.    LAYOUT 
  82. ********************************************/  
  83. #wrap {  
  84.     width790px;  
  85.     margin: 0 auto;  
  86.     text-alignleft;  
  87. }  
  88. #content-wrap {  
  89.     clearboth;  
  90.     width760px;  
  91.     margin5px auto;  
  92.     padding: 0;  
  93. }  
  94. #header {  
  95.     background:url(../images/header.gif) no-repeat top left;  
  96.     positionrelative;  
  97.     height144px;  
  98.     padding: 0;  
  99.     color#fff;  
  100. }  
  101.   
  102. #header h1#logo-text {  
  103.     margin: 0;  
  104.     padding45px 0 0 25px;  
  105. }  
  106.   
  107. #header h1#logo-text a {  
  108.     margin: 0;  
  109.     padding: 0;  
  110.     fontbold 40px   ArialSans-serif;  
  111.     color#d7f000;  
  112.     text-transformnone;  
  113.     text-decorationnone;  
  114.     backgroundtransparent;  
  115.   
  116. }  
  117. #header p#slogan {  
  118.     margin: 0;  
  119.     padding-left25px;  
  120.     fontbold 13px 'Trebuchet MS'ArialSans-serif;  
  121.     text-transformnone;  
  122.     color#c2c2c2;  
  123.   
  124. }  
  125.   
  126. /* navigation */  
  127. #menu {  
  128.     clearboth;  
  129.     margin: 0 autopadding: 0;  
  130.     background#81C524 url(../images/menu.gif) repeat-x;  
  131.     fontbold 13px/40px ArialTahomaSans-serif;  
  132.     height43px;  
  133.     width790px;  
  134. }  
  135. #menu ul {  
  136.     floatleft;  
  137.     list-stylenone;  
  138.     margin:0; padding: 0 0 0 20px;  
  139. }  
  140. #menu ul li {  
  141.     displayinline;  
  142. }  
  143. #menu ul li a {  
  144.     displayblock;  
  145.     floatleft;  
  146.     padding: 0 12px;  
  147.     color#323232;  
  148.     text-decorationnone;  
  149.   
  150. }  
  151. #menu ul li a:hover {  
  152.     color#323232;  
  153.     text-decoration:underline;  
  154.     background:#d2d2d2;  
  155. }  
  156. #menu ul li#current a {  
  157.     color#323232;  
  158.     text-decoration:underline;  
  159. }  
  160.   
  161. /* Main Column */  
  162. #main {  
  163.     floatleft;  
  164.     width555px;  
  165.     margin: 0; padding20px 0 0 0;  
  166.     displayinline;  
  167.     background#fff;  
  168.     padding-bottom25px;  
  169.     border:1px solid #b4b8bb;  
  170. }  
  171. #main h2 {  
  172.     fontnormal 180% 'Trebuchet MS'TahomaArialSans-serif;  
  173.     padding: 0;  
  174.     margin-bottom: 0;  
  175.     color#2666c3;  
  176. }  
  177. #main h2 a {  
  178.     color#2666c3;  
  179.     text-decorationnone;  
  180. }  
  181.   
  182. #main p, #main h1, #main h2, #main h3, #main ol,  
  183. #main blockquote, #main table, #main form {  
  184.     margin-left20px;  
  185.     margin-right25px;  
  186. }  
  187. #main ul li {  
  188.     list-style-imageurl(bullet.gif);  
  189. }  
  190.   
  191. /* Sidebar */  
  192. #sidebar {  
  193.     floatrightright;  
  194.     width195px;  
  195.     padding: 0; margin10px 0 0 0;  
  196.     color#777;  
  197. }  
  198. #sidebar h2 {  
  199.     margin15px 5px 10px 5px;  
  200.     fontbold 1.4em 'Trebuchet MS'TahomaSans-serif;  
  201.     color#555;  
  202. }  
  203. #sidebar p {  
  204.     margin-left5px;  
  205. }  
  206.   
  207. #sidebar ul.sidemenu {  
  208.     list-stylenone;  
  209.     text-alignleft;  
  210.     margin7px 10px 8px 0; padding: 0;  
  211.     text-decorationnone;  
  212.     border-top1px solid #b4b8bb;  
  213. }  
  214. #sidebar ul.sidemenu li {  
  215.     list-stylenone;  
  216.     padding4px 0 4px 5px;  
  217.     margin: 0 2px;  
  218.     color#777;  
  219.     border-bottom1px solid #e2e2e2;  
  220. }  
  221. * html body #sidebar ul.sidemenu li {  
  222.     height: 1%;  
  223. }  
  224. #sidebar ul.sidemenu li a {  
  225.     text-decorationnone;  
  226.     color#1773BC;  
  227. }  
  228. #sidebar ul.sidemenu li a:hover {  
  229.     color#333;  
  230. }  
  231. #sidebar ul.sidemenu ul { margin: 0 0 0 5pxpadding: 0; }  
  232. #sidebar ul.sidemenu ul li { bordernone; }  
  233.   
  234. /* Footer */  
  235. #footer {  
  236.     color#323232;  
  237.     background#caced1;  
  238.     clearboth;  
  239.     width790px;  
  240.     height57px;  
  241.     text-alignleft;  
  242.     font-size: 90%;  
  243.     padding-left25px;  
  244. }  
  245. #footer p {  
  246.     padding10px 0;  
  247.     margin: 0;  
  248. }  
  249. #footer a {  
  250.     color#0076ce;  
  251.     text-decorationnone;  
  252. }  
  253.   
  254. /* alignment classes */  
  255. .float-left  { floatleft; }  
  256. .float-rightright { floatrightright; }  
  257. .align-left  { text-alignleft; }  
  258. .align-rightright { text-alignrightright; }  
  259.   
  260. /* display and additional classes */  
  261. .clear { clearboth; }  
  262.   
  263. .submitf{  
  264.     padding:10px 0 0 45px;  
  265. }  
  266.   
  267. .box-list { float:leftwidth130pxheight:200pxmargin:20px;}  
  268. .error, .notice, .success {padding:.8em;margin-bottom:1em;border:2px solid #ddd;}  
  269. .error {background:#FBE3E4;color:#8a1f11;border-color:#FBC2C4;}  
  270. .notice {background:#FFF6BF;color:#514721;border-color:#FFD324;}  
  271. .success {background:#E6EFC2;color:#264409;border-color:#C6D880;}  
  272. .error a {color:#8a1f11;}  
  273. .notice a {color:#514721;}  
  274. .success a {color:#264409;}  
  275.   
  276. /* s3Slider Styles*/  
  277.   
  278. #slider1 {  
  279.     width512px/* important to be same as image width */  
  280.     height278px/* important to be same as image height */  
  281.     positionrelative/* important */  
  282.     overflowhidden/* important */  
  283.     background#fff;  
  284.     border2px solid #323232;  
  285.     margin20px 20px;  
  286. }  
  287.   
  288. #slider1 img{  
  289.     bordernone;  
  290. }  
  291. #slider1Content {  
  292.     width512px/* important to be same as image width or wider */  
  293.     positionabsolute;  
  294.     top: 0;  
  295.     margin:0;  
  296. }  
  297. .slider1Image {  
  298.     floatleft;  
  299.     displaynone;  
  300.     bordernone;  
  301. }  
  302. .slider1Image span {  
  303.     positionabsolute;  
  304.     font10px/15px ArialHelveticasans-serif;  
  305.     padding10px 13px;  
  306.     width512px;  
  307.     background-color#000;  
  308.     filter: alpha(opacity=70);  
  309.     -moz-opacity: 0.7;  
  310.     -khtml-opacity: 0.7;  
  311.     opacity: 0.7;  
  312.     color#fff;  
  313.     displaynone;  
  314.     margin-left: 0;  
  315. }  
  316. .clear {  
  317.     clearboth;  
  318. }  
  319. .slider1Image span strong {  
  320.     font-size14px;  
  321. }  
  322.   
  323. .bottombottom {  
  324.     bottombottom: 0;  
  325.     left: 0;  
  326.     height70px;  
  327.     width:512px; !important  
  328. }  
  329. ul {  
  330.     list-style-typenone;  
  331.     margin: 0;  
  332.     padding: 0;  
  333. }  
Images used in stylesheet is also included in the download file. Stylesheet quite long. Coz i already included all the css styles we will be using for the entire tutorial.
For us to preview our template.php, we need to load it in our controller. Create a test controller, you can name it anything you want. For example lets create a ‘testView’ controller open system/application/controllers create a new file testview.php then populate the sample code below.
  1. <?php  
  2.   
  3. lass TestView extends Controller{  
  4.   
  5. function TestView ()  
  6. {  
  7.     parent::Controller();  
  8.     $this->load->library('parser');  
  9.     $this->load->helper('url');  
  10. }  
  11.   
  12. function index()  
  13. {  
  14.     $data = array();  
  15.   
  16.     $this->parser->parse('template'$data);  
  17. }  
As you can see we load the url helper in this controller because if you have noticed in our template. We have this line <base href="<?= base_url(); ?>" />. This simply return the base url of our applications installation as what we set in in part 1 of this tutorial.
Now open your browser and type the url http://localhost/tutorial/testview/ . You must now see our template in action. See the online demo here.

Creating our View File

My style in creating a view in codeIgniter is having a master template then include those files that are frequently change or updated during page transitions.
Out of our mockup template above we create a master template of our view. We take those part which can be reused in the entire site like the header, menu, side menu and the footer. This technique is really common in developing a PHP application.
Here we only have to prepare our view files for the next part of the tutorial. Open system/application/views folder, create a new file and we name it home_view.php, open it in a text editor and then populate the code below.
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  
  2.   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
  3.   <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  
  4.   <head>  
  5.   <base href="{ base_url }" />  
  6.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  7.   <meta name="language" content="en" />  
  8. <title>Building an e-commerce application using CodeIgniter - {title}</title>  
  9.   <link rel="stylesheet" href="public/css/default.css" type="text/css" />  
  10.   <script type="text/javascript" src="public/js/jquery.js"></script>  
  11.   <script type="text/javascript" src="public/js/s3Slider.js"></script>  
  12.   <script type="text/javascript">  
  13.   $(document).ready(function() {  
  14.   $('#slider1').s3Slider({  
  15.   timeOut: 8000  
  16.   });  
  17.   });  
  18.   </script>  
  19.   </head>  
  20.   <body>  
  21.   <!-- wrap starts here -->  
  22.   <div id="wrap">  
  23.   <!--header -->  
  24.   <div id="header">  
  25.   <h1 id="logo-text"><a href="#">INSIC Shop</a></h1>  
  26.   <p id="slogan">E-Commerce Site Powered by CodeIgniter</p>  
  27.   </div>  
  28.   <!-- navigation -->  
  29.   <div  id="menu">  
  30.   <ul>  
  31.   <li ><a href="home">Home</a></li>  
  32.   <li ><a href="about-us">About Us</a></li>  
  33.   <li ><a href="contact">Contact Us</a></li>  
  34.   </ul>  
  35.   </div>  
  36.   <!-- content-wrap starts here -->  
  37.   <div id="content-wrap">  
  38.   <div id="main"> <!-- Start of Main -->  
  39.   
  40.   <!-- Load Our page template here -->  
  41.   
  42.   <?php $this->load->view($template); ?>  
  43.   
  44.   </div> <!--Main End -->  
  45.   
  46.   <div id="sidebar">  
  47.   <h2>Product Search</h2>  
  48.   <form action="home/search" class="searchform">  
  49.   <p>  
  50.   <input name="search_query" class="textbox" type="text" />  
  51.   <input name="search" class="button" value="Search" type="submit" />  
  52.   </p>  
  53.   </form>  
  54.   <h2>Product Category</h2>  
  55.   <ul class="sidemenu">  
  56.   
  57.   {categories}  
  58.   
  59.   </ul>  
  60.  </div>  
  61.   <!-- content-wrap ends here -->  
  62.   </div>  
  63.   <!--footer starts here-->  
  64.   <div id="footer">  
  65.   <p> &copy; 2008-2009 <a href="http://insicdesigns.com">INSIC Designs.</a> |  
  66.   Valid <a href="http://validator.w3.org/check?uri=referer"> XHTML </a> | <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a> </p>  
  67.   </div>  
  68.   <!-- wrap ends here -->  
  69.   </div>  
  70.   </body>  
  71.   </html>  
Our master view template is now ready. As you can see, its merely an html with one line of php code (<?php $this->load->view($template); ?>) that calls our page template view file which we will be adding soon as we go on in developing our application.
The next part of this tutorial, we will be making use of our template just made. We have to make our site’s hompage work dynamically. The product feature slideshow must be dynamically generated in our application.

No comments:

Post a Comment