Menus

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



Tuesday, 4 June 2019

Android Drop Down List Tutorial



Spinner in Android application is equivalent of ComboBox

Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.


You can add a spinner to your layout with the spinner.

For example

    <Spinner
        android:id="@+id/spnCountry"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </Spinner>



List of Items in Spinner

Open “res/values/strings.xml” file, define the list of items that will display in Spinner 

File : res/values/strings.xml
<resources>
    <string name="app_name">Layouts</string>

    <string name="prompt_country">Select a Country</string>

    <string-array name="country_list">
        <item>India</item>
        <item>United States</item>
        <item>Singapore</item>
        <item>Malaysia</item>>
        <item>France</item>
        <item>Italy</item>
        <item>New Zealand</item>
    </string-array>

</resources>


Spinner  selection items will be defined in xml file


<Spinner
        android:id="@+id/spnCountry"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/country_list"
         >
</Spinner>

Other method :

<Spinner
        android:id="@+id/spnCountry"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/prompt_country"
        android:entries="@array/country_list"
        android:spinnerMode="dialog"
         >

 </Spinner>

Spinner selection items will be defined in code

add code in onCreate method

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Spinner spnCountry = (Spinner) findViewById(R.id.spnCountry);

        ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.country_list));
        myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spnCountry.setAdapter(myAdapter);


    }
}


Video





How to set selected item of Spinner programmatically in Android

Get spinner selected items text?

This example enplane value display on button click 

Java code
       Button btnSubmit = (Button) findViewById(R.id.btnSubmit);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                         Toast.makeText(TestActivity.this,String.valueOf(spnCountry.getSelectedItem()),Toast.LENGTH_LONG).show();
            }

        });

Example 2


Spinner spnCountry = (Spinner)findViewById(R.id.spnCountry);
String text = spnCountry.getSelectedItem().toString();


Handle spinner click event 


Spinner setOnItemSelectedListener


           spnCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(TestActivity.this,parent.getItemAtPosition(position) + " Selected",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


Set Key and Value in spinner

SpinnerCountry.java

package com.example.root.spinner;

/**
 * Created by root on 8/6/19.
 */
public class SpinnerCountry {

    public String id;
    public String country;

    public SpinnerCountry(String id,String country) {
        this.country = country;
        this.id = id;
    }

    @Override
    public String toString() {
        return country;
    }

}


MainActivity.java


package com.example.root.spinner;
import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Spinner spnCountry = (Spinner) findViewById(R.id.spnCountry);

        ArrayList<SpinnerCountry> countryList = new ArrayList<SpinnerCountry>();
        //add coutries        countryList.add(new SpinnerCountry("1","India"));
        countryList.add(new SpinnerCountry("2","France"));
        countryList.add(new SpinnerCountry("3","United States"));

        //fill data in spinner
        ArrayAdapter<SpinnerCountry> spinnerAdapter = new ArrayAdapter<SpinnerCountry>(MainActivity.this,
                android.R.layout.simple_spinner_dropdown_item,countryList);
        spnCountry.setAdapter(spinnerAdapter);

// And this is how we can retrieve it upon item selection

        spnCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
            @Override            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                SpinnerCountry spn = (SpinnerCountry) parent.getItemAtPosition(position);

                Toast.makeText(MainActivity.this,spn.id,Toast.LENGTH_LONG).show();
        
            }

            @Override            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });


    }
}


Video





Sunday, 19 May 2019

Android date picker dialog



Android App to Choosing a date from Datepicker dialog

How to popup datepicker when click on edittext




XML file:

<?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.datedialogpopup.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="Birthday" />

        <EditText
            android:id="@+id/edtBirthday"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Click to enter Birthday"
            />

    </LinearLayout>


</RelativeLayout>


Java Code:

package com.example.root.datedialogpopup;

import android.app.DatePickerDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.icu.util.Calendar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CalendarView;
import android.widget.DatePicker;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {


    private EditText edtBirthday;
    private DatePickerDialog.OnDateSetListener dateSetListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edtBirthday = (EditText)findViewById(R.id.edtBirthday);

        edtBirthday.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {

                Calendar cal = Calendar.getInstance();
                //get current date and to set popup date picker

                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH);
                int day = cal.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog dialog = new DatePickerDialog(
                        MainActivity.this,
                        android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                        dateSetListener,
                        year,month,day
                );

                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog.show();

            }
        });


        dateSetListener = new DatePickerDialog.OnDateSetListener(){
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int day) {

                month=month+1;
                String date = month + "/" + day + "/" + year;

                edtBirthday.setText(date);

            }
        };


    }
}