Menus

Tuesday, 13 August 2019

Codelgniter - Models



What is a Model?

Models are PHP classes that are designed to work with information in your database.
You might have a model class that contains functions to insert, update, and retrieve your blog data.


Anatomy of a Model

Model classes are stored in your application/models/ directory. 
The basic prototype for a model class is this:
class Model_name extends CI_Model {

}
Where Model_name is the name of your class. 
Class names must have the first letter capitalized with the rest of the name lowercase. Make sure your class extends the base Model class.
The file name must match the class name. For example, if this is your class:
class User_model extends CI_Model {

}

Loading a Model

Your models will typically be loaded and called from within your controller methods.


$this->load->model('model_name');
If your model is located in a sub-directory, include the relative path from your models directory. For example, if you have a model located at application/models/blog/Queries.php you’ll load it using:
$this->load->model('blog/queries');
Once loaded, you will access your model methods using an object with the same name as your class:
$this->load->model('model_name');

$this->model_name->method();
parameter of the loading method
$this->load->model('model_name', 'foobar');

$this->foobar->method();



Connecting to your Database


  • You can tell the model loading method to auto-connect by passing TRUE (boolean) via the third parameter, and connectivity settings, as defined in your database config file will be used:
    $this->load->model('model_name', '', TRUE);
    
  • You can manually pass database connectivity settings via the third parameter:
    $config['hostname'] = 'localhost';
    $config['username'] = 'myusername';
    $config['password'] = 'mypassword';
    $config['database'] = 'mydatabase';
    $config['dbdriver'] = 'mysqli';
    $config['dbprefix'] = '';
    $config['pconnect'] = FALSE;
    $config['db_debug'] = TRUE;
    
    $this->load->model('model_name', '', $config);

Example:

Controller
<?php
class Blog extends CI_Controller {

public function index()
{
//gather information here from models
$this->load->model('blog_model');
echo $this->blog_model->test_blog();
}
}
?>

Model
<?php
class Blog_model extends CI_Model
{
function test_blog()
{
echo "This is model test funtion";
}
//Database function write here
}
?>


Here is an example of what such a model class might look like:

class Blog_model extends CI_Model {

        public $title;
        public $content;
        public $date;

        public function get_last_ten_entries()
        {
                $query = $this->db->get('entries', 10);
                return $query->result();
        }

        public function insert_entry()
        {
                $this->title    = $_POST['title']; // please read the below note
                $this->content  = $_POST['content'];
                $this->date     = time();

                $this->db->insert('entries', $this);
        }

        public function update_entry()
        {
                $this->title    = $_POST['title'];
                $this->content  = $_POST['content'];
                $this->date     = time();

                $this->db->update('entries', $this, array('id' => $_POST['id']));
        }

}
Note
The methods in the above example use the Query Builder database methods.
More Details


Monday, 12 August 2019

Codelgniter - Controllers


Controllers

Controllers are the heart of your application, as they determine how HTTP requests should be handled.


A Controller is simply a class file that is named in a way that can be associated with a URI.
Consider this URI:
example.com/index.php/blog/
Controller name as Blog.php
The file must be called ‘Blog.php’, with a capital ‘B’.
Class names must start with an uppercase letter.

* index function works by default when the controller is loaded.


Let’s try it: Hello World!


Let’s create a simple controller so you can see it in action. Using your text editor, create a file called Blog.php, and put the following code in it:
<?php
class Blog extends CI_Controller {

        public function index()
        {
                echo 'Hello World!';
        }
}
Then save the file to your application/controllers/ directory.
Now visit the your site using a URL similar to this:
example.com/index.php/blog/

Methods

In the above example the method name is index(). The “index” method is always loaded by default if the second segment of the URI is empty. Another way to show your “Hello World” message would be this:
example.com/index.php/blog/index/


Add a new method to your controller:
<?php
class Blog extends CI_Controller {

 public function index()
 {
  echo "Hello World!";  
 }

 public function test()
 {
  echo "Test method";
 }
}
Now load the following URL to see the comment method:
example.com/index.php/blog/test/

Call one method to anther

<?php
class Blog extends CI_Controller {

   public function index()
 {
  echo "Hello World!"; 
  $this->test(); 
 }

 public function test()
 {
  echo "Test method";
 }
}


Passing URI Segments to your methods

If your URI contains more than two segments they will be passed to your method as parameters.
For example, let’s say you have a URI like this:
example.com/index.php/blog/index/32/Shaneesh
Your method will be passed URI segments 3 and 4 (“sandals” and “123”):
<?php

class Blog extends CI_Controller {

 public function index($id,$name)
 {
  echo $id ." - " ,$name;
 } 

}

?>

Defining a Default Controller

To specify a default controller, open your application/config/routes.php file and set this variable:
$route['default_controller'] = 'blog';
by default loaded index method in blog controller.

Class Constructors

If you intend to use a constructor in any of your Controllers, you MUST place the following line of code in it:
parent::__construct();
The reason this line is necessary is because your local constructor will be overriding the one in the parent controller class so we need to manually call it.
Example:
<?php
class Blog extends CI_Controller {

        public function __construct()
        {
                parent::__construct();
                // Your own constructor code
        }
}

Private methods

private function _utility()
{
        // some code
}

Processing Output

CodeIgniter has an output class that takes care of sending your final rendered data to the web browser automatically.
public function _output($output)
{
        echo $output;
}

Remapping Method Calls

CodeIgniter permits you to override this behavior through the use of the _remap() method:
public function _remap()
{
        // Some code here...
}

public function _remap($method)
{
        if ($method === 'some_method')
        {
                $this->$method();
        }
        else
        {
                $this->default_method();
        }
}


More details


Thursday, 8 August 2019

Introduction to Codelgniter



Welcome to CodeIgniter



CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications.





Download

CodeIgniter is an Application Development Framework - a toolkit - for people who build web sites using PHP. Its goal is to enable you to develop projects much faster than you could if you were writing code from scratch, by providing a rich set of libraries for commonly needed tasks, as well as a simple interface and logical structure to access these libraries. CodeIgniter lets you creatively focus on your project by minimizing the amount of code needed for a given task.

Who is CodeIgniter For?

CodeIgniter is right for you if:
  • You want a framework with a small footprint.
  • You need exceptional performance.
  • You need broad compatibility with standard hosting accounts that run a variety of PHP versions and configurations.
  • You want a framework that requires nearly zero configuration.
  • You want a framework that does not require you to use the command line.
  • You want a framework that does not require you to adhere to restrictive coding rules.
  • You are not interested in large-scale monolithic libraries like PEAR.
  • You do not want to be forced to learn a templating language (although a template parser is optionally available if you desire one).
  • You eschew complexity, favoring simple solutions.
  • You need clear, thorough documentation.

Installation Instructions

Tutorial


Thursday, 25 July 2019

how to get the number of days of the current month in PHP ?




cal_days_in_month — Return the number of days in a month for a given year and calendar

Description 

cal_days_in_month ( int $calendar , int $month , int $year ) : int
This function will return the number of days in the month of year for the specified calendar.
The length in days of the selected month in the given calendar

More details


example

             $m1=date('Y-m-d',strtotime($date_of_join));            
           
             $mon_num = date("m",strtotime($m1));
             $curr_year = date("Y",strtotime($m1));

            $num = cal_days_in_month(CAL_GREGORIAN, $mon_num, $curr_year);



PHP code for find number of days form given month and year ?



<?php

$month = 2;
$year = 2020;

$days = $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);

echo $days;

?>




Monday, 1 July 2019

Android Scrolling text in Textview


Android auto scrolling text

This tutorial explain to scroll text message on activity.

How to make Marquee text in Android?


Property as marquee
android:ellipsize = "marquee"
android:fadingEdge = "horizontal"
android:marqueeRepeatLimit = "marquee_forever"
android:scrollHorizontally = "true"
android:singleLine = "true"






Add the following code to res/layout/activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.root.scrolling.MainActivity">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >



                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Hello World!" />

                <TextView
                    android:id="@+id/txtScroll"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="30dp"
                    android:background="#D0D0D0"
                    android:text="http://yii2ideas.blogspot.com/"

                    android:focusable="true"

                    android:focusableInTouchMode="true"
                    android:singleLine="true"
                    android:scrollHorizontally="true"
                    android:ellipsize="marquee"
                    android:marqueeRepeatLimit="marquee_forever"
                    />
             
        </LinearLayout>
     
</RelativeLayout>




Useful Links
Android Drop Down List Tutorial

Android date picker dialog