Thursday, March 5, 2015

Step to Configure Codeigniter Application

            Steps for configuring Codeigniter Project

step-1: First download source for codeigniter from here  Click Here or visit site,
            www.codeigniter.com.


step-2: extract codeigniter zip file in www folder rename default name with your
             project name.


step-3: Now open application folder from your project.
             here you find config folder.


             inside config folder find config.php file and open it.



step-4: change your application base_url  configuration in config file application/config.php.

            $config['base_url'] = '';


                                                   To

           $config['base_url'] = 'http://localhost/codeigniter/';
          //replace project name codeigniter with your project name


                                                     OR

              Run your project and then copy url from browser and paste in config.php file
             



Now you complete your codeigniter application configuration. Thanks for the visiting this blog.

Tuesday, March 3, 2015

remove index.php from url codeigniter

If you don't know  how to setup your codeigniter project click here 

Create .htaccess(including the dot) in your project root folder



Go through below procedure

By default index.php file will be included in your url. 

       http://localhost/codeigniter/index.php/first                   

       Sample project code  click here
   
You can easily remove this file by using  .htaccess file writing some rewrite rules. 
put below code inside your .htaccess file.


<IfModule mod_rewrite.c>
     RewriteEngine on
     RewriteCond $1 !^(index\.php)
     RewriteRule ^(.*)$ /codeigniter/index.php/$1 [L]
</IfModule>

   
step-1  RewriteCond $1 !^(index\.php|images|css|js|robots\.txt) give permission using Rewrite Rule i.e. jsjavascript library ), images etc.




step-2  Replace codeigniter(application name) with your application name .
Run your project now you can see in the url index.php is removed.

       http://localhost/codeigniter/first

Thanks for visiting my blog, help your friends by sharing this post. 

Monday, March 2, 2015

get and display post data codeigniter

Configure Your Codeigniter Application Click Here

Here you find, how to get post data in controller and sent back to view.
Create Controller postdata.php in controller folder put the below code inside the file.


<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Postdata extends CI_Controller {

//constructor
function __construct()
{
parent::__construct();
$this->load->helper('url');
}
//index() method
public function index()
{
$this->load->view('postData');
}
//view() method
}
 ?>


Create view postData.php in view folder put the below code inside the file.
here we creating a form for getting basic info about user. form action(url where you get post data) and method(post) need to specify.


<html>
<head><title>Post Data Example</title></head>
<style type="text/css">
.title { font-family: Arial, Helvetica, sans-serif; text-align:center;width:350px; }
.tableDesign { font-size:16px;font-family:Arial, Helvetica, sans-serif; width:350px; height: 270px; }
table { background-color:#CCC; }
table input { height:25px;font-size:16px; }
table tr { height:2px; }
.errorMessage { color:red;text-align:center; }</style>
<body>
   <form id="form1" name="form1" method="post" action="<?php echo base_url(); ?>index.php/postdata/view">
      <label><div class="title"><u>Post Data Example</u></div></label>
      <table style="" class="tableDesign">
         <tr><td colspan="2"><?php if(isset($_GET['err_message'])){ echo "<div class='errorMessage'>".$_GET['err_message'].'</div>'; } ?></td></tr>
         <tr><td>First Name: </td><td><input type="text" name="txtFirstName" /></td></tr>
         <tr><td>Last Name: </td><td><input type="text" name="txtLastName" /></td></tr>
         <tr><td>Gender:</td><td><input type="radio" name="txtGender" value="Male" checked />
            Male&nbsp;<input type="radio" name="txtGender" value="Female" />Female</td></tr>
         <tr><td>Mobile No.: </td><td><input type="text" name="txtMobileNo" /></td></tr>
         <tr><td>E-mail: </td><td><input type="text" name="txtEmail" /></td></tr>
         <tr><td>&nbsp;</td><td><input type="submit" name="submit" value="Submit" /></td></tr>
    </table>
</form>
</body>
</html>


Now, Open your browser go through below url

http://localhost/codeigniter/index.php/postdata



here you can see html form input fields to collect basic info.

Put below code inside controller postdata.php after index method(after //view() method).

Method get the post data and sent back to view for display data. In this example whole post data sent, also possible to get single request and displayed on view.


         public function view()
{
/* $data['firstName'] = $this->input->post('txtFirstName');
$data['lastName'] = $this->input->post('txtLastName');
$data['gender'] = $this->input->post('txtGender');
$data['mobileNo'] = $this->input->post('txtMobileNo');
$data['email'] = $this->input->post('txtEmail'); */

$data['postData'] = $this->input->post();
$this->load->view('postDataDisplay', $data);
}


Create view postDataDisplay.php in view folder put the below code inside the file for displaying post data result on the browser.


<html>
<head>
<title>Post Data Example</title>
</head>
<style type="text/css">
.title { font-family: Arial, Helvetica, sans-serif; text-align:center;width:350px; }
.tableDesign { font-size:16px;font-family:Arial, Helvetica, sans-serif; width:350px; height: 210px; }
table { background-color:#CCC; }
table input { height:25px;font-size:16px; }
table tr { height:2px; }
.errorMessage { color:red;text-align:center; }</style>
<body>
  <label><div class="title"><u>Post Data Display</u></div></label>
  <table style="" class="tableDesign">

  <!--<tr><td>First Name: <?php echo $firstName; ?> </td></tr> single field data displayed -->

     <!-- display post data result  -->
    <tr><td>First Name: <strong><?php echo $postData['txtFirstName']; ?></strong></td></tr>
    <tr><td>Last Name:  <strong><?php echo $postData['txtLastName']; ?></strong></td></tr>
    <tr><td>Gender: <strong><?php echo $postData['txtGender']; ?></strong></td></tr>
    <tr><td>Mobile No.:  <strong><?php echo $postData['txtMobileNo']; ?></strong></td></tr>
    <tr><td>E-mail:  <strong><?php echo $postData['txtEmail']; ?></strong></td></tr>
  </table>
</form>
</body>
</html>


Now you done try submitting form data and see the output.