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.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="language" content="en" />
<title>Building an e-commerce application using CodeIgniter</title>
  <link rel="stylesheet" href="css/default.css" type="text/css" />
  <script type="text/javascript" src="js/jquery.js"></script>
  <script type="text/javascript" src="js/s3Slider.js"></script>
  <script type="text/javascript">
  $(document).ready(function() {
  $('#slider1').s3Slider({
  timeOut: 8000
  });
  });
  </script>
  </head>
  <body>
  <!-- wrap starts here -->
  <div id="wrap">
  <!--header -->
  <div id="header">
  <h1 id="logo-text"><a href="#">INSIC Shop</a></h1>
  <p id="slogan">E-Commerce Site Powered by CodeIgniter</p>
  </div>
  <!-- navigation -->
  <div  id="menu">
  <ul>
  <li ><a href="home">Home</a></li>
  <li ><a href="about-us">About Us</a></li>
  <li ><a href="contact">Contact Us</a></li>
  </ul>
  </div>
  <!-- content-wrap starts here -->
  <div id="content-wrap">
  <div id="main">
  <h2>Featured Product</h2>

  <div id="slider1">
  <ul id="slider1Content">
  <li class="slider1Image">
  <a href=""><img src="products/product-3.jpg" alt="1" /></a>
  <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>
  <li class="slider1Image">
  <a href=""><img src="products/product-2.jpg" alt="2" /></a>
  <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>
  <li class="slider1Image">
  <a href=""><img src="products/product-1.jpg" alt="3" /></a>
  <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>
  <div class="clear slider1Image"></div>
  </ul>
  </div>

  <h2>Our Products</h2>
  <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>
  <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>
  <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>
  <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>
  <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>
  <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>
  </div>

  <div id="sidebar">
  <h2>Product Search</h2>
  <form action="#" class="searchform">
  <p>
  <input name="search_query" class="textbox" type="text" />
  <input name="search" class="button" value="Search" type="submit" />
  </p>
  </form>
  <h2>Product Category</h2>
  <ul class="sidemenu">
  <li ><a href="#">Category 1</a></li>
  <li ><a href="#">Category 2</a></li>
  <li ><a href="#">Category 3</a></li>
  <li ><a href="#">Category 4</a></li>
  </ul>
 </div>
  <!-- content-wrap ends here -->
  </div>
  <!--footer starts here-->
  <div id="footer">
  <p> &copy; 2008-2009 <a href="http://insicdesigns.com">INSIC Designs.</a> |
  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>
  </div>
  <!-- wrap ends here -->
  </div>
  </body>
  </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.


* { margin: 0; padding: 0; outline: 0; }

body {
	background: #caced1 url(../images/body-bg.gif) repeat-x top;
	font: 70%/1.5em Verdana, Tahoma, arial, sans-serif;
	color: #777;
	text-align: center;
	margin: 0;
}

/* links */
a, a:visited {
	text-decoration: none;
	background: inherit;
	color: #FB9233;
}
a:hover {
	text-decoration: underline;
	background: inherit;
	color: #93C600;
}

/* headers */
h1, h2, h3 { font-family: 'Trebuchet MS', Tahoma, Sans-serif;  }
h1 {
	font-size: 180%;
	font-weight: normal;
	color: #555;
}

h2 {
	font-size: 160%;
	color: #88ac0b;
	font-weight: normal;
}
h3 {
	font-size: 135%;
	color: #666666;

}

/* images */
img {
	background: #fff;
   border: 1px solid #E5E5E5;
	padding: 5px;
}

img.float-right { margin: 5px 0px 10px 10px;  }
img.float-left { margin: 5px 10px 10px 0px; }

h1, h2, h3, p {
	margin: 10px 15px;
	padding: 0;
}

ol {
	margin: 5px 15px;
	padding: 0 25px;
}

/* search form */
.searchform {
	background-color: transparent;
	border: none;
	margin: 0;
	padding: 5px 0 5px 0;
	width: 180px;
}
.searchform p { margin: 0; padding: 0; }
.searchform input.textbox {
	width: 110px;
	color: #777;
	padding: 2px;
}
.searchform input.button {
	width: 55px;
}

/********************************************
   LAYOUT
********************************************/
#wrap {
	width: 790px;
	margin: 0 auto;
	text-align: left;
}
#content-wrap {
	clear: both;
	width: 760px;
	margin: 5px auto;
	padding: 0;
}
#header {
	background:url(../images/header.gif) no-repeat top left;
	position: relative;
	height: 144px;
	padding: 0;
	color: #fff;
}

#header h1#logo-text {
	margin: 0;
	padding: 45px 0 0 25px;
}

#header h1#logo-text a {
	margin: 0;
	padding: 0;
	font: bold 40px   Arial, Sans-serif;
	color: #d7f000;
	text-transform: none;
	text-decoration: none;
	background: transparent;

}
#header p#slogan {
	margin: 0;
	padding-left: 25px;
	font: bold 13px 'Trebuchet MS', Arial, Sans-serif;
	text-transform: none;
	color: #c2c2c2;

}

/* navigation */
#menu {
	clear: both;
	margin: 0 auto; padding: 0;
	background: #81C524 url(../images/menu.gif) repeat-x;
	font: bold 13px/40px Arial, Tahoma, Sans-serif;
	height: 43px;
	width: 790px;
}
#menu ul {
	float: left;
	list-style: none;
	margin:0; padding: 0 0 0 20px;
}
#menu ul li {
	display: inline;
}
#menu ul li a {
	display: block;
	float: left;
	padding: 0 12px;
	color: #323232;
	text-decoration: none;

}
#menu ul li a:hover {
	color: #323232;
	text-decoration:underline;
	background:#d2d2d2;
}
#menu ul li#current a {
	color: #323232;
	text-decoration:underline;
}

/* Main Column */
#main {
	float: left;
	width: 555px;
	margin: 0; padding: 20px 0 0 0;
	display: inline;
	background: #fff;
	padding-bottom: 25px;
	border:1px solid #b4b8bb;
}
#main h2 {
	font: normal 180% 'Trebuchet MS', Tahoma, Arial, Sans-serif;
	padding: 0;
	margin-bottom: 0;
	color: #2666c3;
}
#main h2 a {
	color: #2666c3;
	text-decoration: none;
}

#main p, #main h1, #main h2, #main h3, #main ol,
#main blockquote, #main table, #main form {
	margin-left: 20px;
	margin-right: 25px;
}
#main ul li {
	list-style-image: url(bullet.gif);
}

/* Sidebar */
#sidebar {
	float: right;
	width: 195px;
	padding: 0; margin: 10px 0 0 0;
	color: #777;
}
#sidebar h2 {
	margin: 15px 5px 10px 5px;
	font: bold 1.4em 'Trebuchet MS', Tahoma, Sans-serif;
	color: #555;
}
#sidebar p {
	margin-left: 5px;
}

#sidebar ul.sidemenu {
	list-style: none;
	text-align: left;
	margin: 7px 10px 8px 0; padding: 0;
	text-decoration: none;
	border-top: 1px solid #b4b8bb;
}
#sidebar ul.sidemenu li {
	list-style: none;
	padding: 4px 0 4px 5px;
	margin: 0 2px;
	color: #777;
	border-bottom: 1px solid #e2e2e2;
}
* html body #sidebar ul.sidemenu li {
	height: 1%;
}
#sidebar ul.sidemenu li a {
	text-decoration: none;
	color: #1773BC;
}
#sidebar ul.sidemenu li a:hover {
	color: #333;
}
#sidebar ul.sidemenu ul { margin: 0 0 0 5px; padding: 0; }
#sidebar ul.sidemenu ul li { border: none; }

/* Footer */
#footer {
	color: #323232;
	background: #caced1;
	clear: both;
	width: 790px;
	height: 57px;
	text-align: left;
	font-size: 90%;
	padding-left: 25px;
}
#footer p {
	padding: 10px 0;
	margin: 0;
}
#footer a {
	color: #0076ce;
	text-decoration: none;
}

/* alignment classes */
.float-left  { float: left; }
.float-right { float: right; }
.align-left  { text-align: left; }
.align-right { text-align: right; }

/* display and additional classes */
.clear { clear: both; }

.submitf{
	padding:10px 0 0 45px;
}

.box-list { float:left; width: 130px; height:200px; margin:20px;}
.error, .notice, .success {padding:.8em;margin-bottom:1em;border:2px solid #ddd;}
.error {background:#FBE3E4;color:#8a1f11;border-color:#FBC2C4;}
.notice {background:#FFF6BF;color:#514721;border-color:#FFD324;}
.success {background:#E6EFC2;color:#264409;border-color:#C6D880;}
.error a {color:#8a1f11;}
.notice a {color:#514721;}
.success a {color:#264409;}

/* s3Slider Styles*/

#slider1 {
    width: 512px; /* important to be same as image width */
    height: 278px; /* important to be same as image height */
    position: relative; /* important */
	overflow: hidden; /* important */
	background: #fff;
	border: 2px solid #323232;
	margin: 20px 20px;
}

#slider1 img{
	border: none;
}
#slider1Content {
    width: 512px; /* important to be same as image width or wider */
    position: absolute;
	top: 0;
	margin:0;
}
.slider1Image {
    float: left;
	display: none;
	border: none;
}
.slider1Image span {
    position: absolute;
	font: 10px/15px Arial, Helvetica, sans-serif;
    padding: 10px 13px;
    width: 512px;
    background-color: #000;
    filter: alpha(opacity=70);
    -moz-opacity: 0.7;
	-khtml-opacity: 0.7;
    opacity: 0.7;
    color: #fff;
    display: none;
	margin-left: 0;
}
.clear {
	clear: both;
}
.slider1Image span strong {
    font-size: 14px;
}

.bottom {
	bottom: 0;
	left: 0;
	height: 70px;
	width:512px; !important
}
ul {
	list-style-type: none;
	margin: 0;
	padding: 0;
}

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.


	<?php

class TestView extends Controller{

	function TestView ()
	{
		parent::Controller();
		$this->load->library('parser');
		$this->load->helper('url');
	}

	function index()
	{
		$data = array();

		$this->parser->parse('template', $data);
	}
}

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.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
  <base href="{ base_url }" />
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="language" content="en" />
<title>Building an e-commerce application using CodeIgniter - {title}</title>
  <link rel="stylesheet" href="public/css/default.css" type="text/css" />
  <script type="text/javascript" src="public/js/jquery.js"></script>
  <script type="text/javascript" src="public/js/s3Slider.js"></script>
  <script type="text/javascript">
  $(document).ready(function() {
  $('#slider1').s3Slider({
  timeOut: 8000
  });
  });
  </script>
  </head>
  <body>
  <!-- wrap starts here -->
  <div id="wrap">
  <!--header -->
  <div id="header">
  <h1 id="logo-text"><a href="#">INSIC Shop</a></h1>
  <p id="slogan">E-Commerce Site Powered by CodeIgniter</p>
  </div>
  <!-- navigation -->
  <div  id="menu">
  <ul>
  <li ><a href="home">Home</a></li>
  <li ><a href="about-us">About Us</a></li>
  <li ><a href="contact">Contact Us</a></li>
  </ul>
  </div>
  <!-- content-wrap starts here -->
  <div id="content-wrap">
  <div id="main"> <!-- Start of Main -->

  <!-- Load Our page template here -->

  <?php $this->load->view($template); ?>

  </div> <!--Main End -->

  <div id="sidebar">
  <h2>Product Search</h2>
  <form action="home/search" class="searchform">
  <p>
  <input name="search_query" class="textbox" type="text" />
  <input name="search" class="button" value="Search" type="submit" />
  </p>
  </form>
  <h2>Product Category</h2>
  <ul class="sidemenu">

  {categories}

  </ul>
 </div>
  <!-- content-wrap ends here -->
  </div>
  <!--footer starts here-->
  <div id="footer">
  <p> &copy; 2008-2009 <a href="http://insicdesigns.com">INSIC Designs.</a> |
  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>
  </div>
  <!-- wrap ends here -->
  </div>
  </body>
  </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.

PG

Author: insic

Subscribe in my RSS Feed for more updates on Web Design and Development related articles. Follow me on twitter or drop a message to my inbox.

Related Post

Delicious

45 Responses to “Building a basic PHP E-Commerce Application using CodeIgniter Framework – Part 3”

  1. The mockup looks really good! Very clean and user-friendly.

    This e-commerce tutorial is great. Good work.

  2. The demo looks great! Awesome Tutorial. Looking forward for the next part.

  3. Really nice and clean template. 

  4. the parser library is very cool!

  5. Thanks for the great tutorial.

    I’m looking forward to the next one in the series.

  6. This series is getting better. Added your blog yo my bookmark.

  7. Yay!! Thanks for the post… can’t wait for the next one!

  8. Good Job guys! Can’t wait for the next posts.

  9. nice templates, very clean..
    waiting for the next part…

  10. waiting for the next part…²

  11. So I can add this to Joomla’s list of e carts?

    Just kidding.
    Nice tutorial, hope you make part 4 soon…

  12. I think you must take a look at the CodeIgniter version that is at the SVN repository.
    It has a new library called “Cart” and it will make this kind of things a lot easier.

  13. Hello Insic,
    Is this demo link actualy working? It says me “The web page is not available.” Please confirm. I am excited to see this live.

  14. @Dileep K Sharma Yes, the demo link is working.

  15. good tutorial. when is part 4 coming out? looking forward to it!

  16. Waw, I love this article because I need it for my online shop web…
    Thank’s Insinc design…. It is good job

    Regard

  17. This tutorial is great!
    I really want to read the rest of it!

    Keep the great job!!

  18. so far well, but the functional sections to come very soon yarrrr, because still it didn’t complete the topic!!

    so please!

  19. I am impressed with the design images you provide for each article.  How much time does that take? I have no graphic design talent :(

  20. Hi :-)
    Very good tutorial. I’m working with your tut. to get a connection on this (http://www.thewojogroup.com/2008/10/simplecartjs-paypal-e-commerce-in-minutes/) shopping cart into codeigniter.

    Keep up the very good work. Two thumbs up ! :-)

  21. hi,

    great tutorial!  especially for beginners like me.

    is there any site that have a step by step tutorial regarding CRUD and
    user management?

    thanks… waiting for part 4

  22. Congratulations for the post! When going out to party 4?

  23. Hello, It’s nice to know you have a place to share one of my fav. framework. I just found your blog and also excited to see the next part.

  24. Thank you for this tutorial.  I am looking forward to the next part in the series.  We use codeigniter to develop all of our web applications and have really loved the flexibility.

  25. Thank’s a lot for this tutorial, just what I needed.
    I’m definitely looking forward to the next part!

  26. Hmm.. Just one problem. There is no public/css folder, so where should i create it? I’ve tried some different locations, but none of them have given a succesful result! Please help :)

  27. Oh, sorry.. The first sentence of this tutorial solved it, lol.

  28. Wee! this looks as beautiful as u r XD

  29. thanks for u’r tutorial
    I’ll try and implement this online store ^^

  30. thank for tutorial. Can’t wait for next one :-)

  31. Aww its not out yet. Darn. But its a great tutorial

  32. I like ur tutorial.. thank a lot !!! ^_^

  33. waiting for next part

  34. Geat Tutorial but whats happened to part 4? Does any one have part 4?

  35. Good tutorial. Thank.

  36. Thankx for the post, when will you post the next tutorial?

  37. That´s a great tutorial i can´t wait for the rest of this useful tut. Thanks

  38. hey! we need the part 4 please!!
    i can’t stand end this great tutorial!!! so close the end of the excellent codeigniter resource

    btw can someone upload or give a working link to the zip file the “download” link is dead :(

    thanx for your work, it could be my codeigniter bible :D

  39. great tut thanx, the download link isnt working, may somebody pls post a working link? can wait to see it.

  40. i searched the whole web for hours but didn’t find nor the archives nor the part 4…i’m so sad :(
    what i can give you are just some dummies image i quickly generated for it. Place them in /images/ dir.
    just “images/body-bg.gif” (first css line) is missing
    anyway here’s the link to these files:
    http://www.filesavr.com/CXqiDrz2

    but we would appreciate someone having the real files to upload them! thanx

  41. very very goooooooooooooooooooooooooooooooodd….

    I’m wait next tutorial..

    thanks..

  42. The demo isn’t working anymore, and also the download link also.

    who has it?please send at ciprianrpp@gmail.com

    Thanks

  43. Really very nice tutorials. It’s very help full for us plz I want to see your next part i mean part 4.

    @@@ The demo and download link not working

Leave a Reply