Menus

Wednesday 1 February 2017

Update data From Other Table


Update one MySQL table with values from another



My customer table looks like:



And the customer_rate table looks like:



I want to update prize in customer with the id from customer_rate based on prize 
Action : customer.prize = customer.qty * customer_rate.prize
The updated table will hopefully look like:


I have a query that works

UPDATE customerINNER JOIN customer_rate ON (customer.cus_id = customer_rate.id)SET customer.prize = customer.qty * customer_rate.prize



add where condition 

UPDATE customerINNER JOIN customer_rate ON (customer.cus_id = customer_rate.id)SET customer.prize = customer.qty * customer_rate.prize where customer.id = 1




Wednesday 4 January 2017

Android Radio Group and Radio Button Operations


What is Radio Group ?

A RadioGroup class is used for set of radio buttons.

If we check one radio button that belongs to a radio group, it automatically unchecks any previously checked radio button within the same group.

Example of using Radio Group

Create the layout of the Activity


<RadioGroup
    android:id="@+id/rg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:id="@+id/rad1"
        android:checked="false" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:id="@+id/rad2"
        android:checked="false" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3"
       android:id="@+id/rad3"
        android:checked="false" />

</RadioGroup>


* Radio Group orientation as two types are vertical and horizontal

Android:placing the radio buttons horizontally


android:orientation="horizontal"

Android:placing the radio buttons vertically

android:orientation="vertical"

Code the Activity

At this point we will show how we can handle the change of a radio button, that belongs to a RadioGroup.

package com.example.root.calendar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class EntriesActivity extends AppCompatActivity {

    private RadioGroup radioGroup;
    private Button btnSaveall;
    private RadioButton rad1, rad2, rad3;

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


        radioGroup = (RadioGroup) findViewById(R.id.rg);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

// find which radio button is selected

if(checkedId == R.id.rad1) { Toast.makeText(getApplicationContext(), "choice: Radio1 Selected",Toast.LENGTH_SHORT).show(); } else if(checkedId == R.id.rad2) { Toast.makeText(getApplicationContext(), "choice: Radio2 Selected",Toast.LENGTH_SHORT).show(); } else if(checkedId == R.id.rad3) { Toast.makeText(getApplicationContext(), "choice: Radio3 Selected",Toast.LENGTH_SHORT).show(); } } }); rad1 = (RadioButton) findViewById(R.id.rad1); rad2 = (RadioButton) findViewById(R.id.rad2); rad3 = (RadioButton) findViewById(R.id.rad3); btnSaveall = (Button)findViewById(R.id.btnSaveall); btnSaveall.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int selectedId = radioGroup.getCheckedRadioButtonId();

// find which radioButton is checked by id

if(selectedId ==rad1.getId()) { Toast.makeText(getApplicationContext(),"Radio 1 Selected" ,Toast.LENGTH_SHORT).show(); } else if(selectedId == rad2.getId()) { Toast.makeText(getApplicationContext(),"Radio 2 Selected" ,Toast.LENGTH_SHORT).show(); } else if(selectedId == rad3.getId()) { Toast.makeText(getApplicationContext(),"Radio 3 Selected" ,Toast.LENGTH_SHORT).show(); } } }); } }

Now let’s have a look at the code above. When a checked radio button is changed in its group, OnCheckedChangeListener is invoked in order to handle this situation. The onCheckedChanged() method of this interface, includes the unique id of the radio button that was selected and caused the invoke of the callback.
In this example we will show you another way of selecting the choice information (for example when a button is pressed). This can be done through getCheckedRadioButtonId(), which is a public function of RadioGroup class. This method returns the unique id of the radio button that is chosen from the group. You can have a look at the code to see how you can handle both situations.
Of course Android system provides us a more dynamic way of changing and handling the attributes of the application views. As a prerequisite is to map every view with the unique id component of the XML. This can be done viafindViewById() method.



Radio Buttons Set checked state through code


You can use either radioButton.setChecked(true); or radiobutton.setSelected(true);

private RadioButton radA; //declare 
.
.
.
rad1 = (RadioButton) findViewById(R.id.rad1);
rad1.setChecked(true);



Hiding a RadioButton in Android


you can hide the particular radio button this way
RadioButton myRadioButton = (RadioButton) findViewById(R.id.last_radio);
myRadioButton.setVisibility(View.INVISIBLE);
or if you use View Gone menas radio button hide with sapce
RadioButton myRadioButton = (RadioButton) findViewById(R.id.last_radio);
myRadioButton.setVisibility(View.GONE);

Hiding a radio group

RadioGroup myRadioGroup = (RadioGroup) findViewById(R.id.radiogroup_quiz_answers);
myRadioGroup.setVisibility(View.Visible);


Hi use like this.
RadioButton myRadioButton = (RadioButton) findViewById(R.id.my_radio_button_id);
myRadioButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            myRadioButton.setVisibility(View.INVISIBLE);
        }
    });
OR
  <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" 
        android:visibility="invisible"/>

radioGroup1.setVisibility(View.GONE);
radioGroup2.setVisibility(View.GONE);
radioGroup3.setVisibility(View.GONE);
radioGroup2.setVisibility(View.VISIBLE);


How to set the background colour of a RadioButton?


        <RadioGroup
            android:id="@+id/eligibility"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:orientation="horizontal"
            android:layout_weight=".5" >

            <RadioButton
                android:id="@+id/eligibilityYes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:background="@drawable/checkbox_background"
                android:text="yes" />

            <RadioButton
                android:id="@+id/eligibilityNo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/checkbox_background"
                android:text="No" />
        </RadioGroup>

your checkbox_background.xml file put image in res/drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="false" android:drawable="@drawable/radio_rad" />
    <item android:state_checked="true" android:drawable="@drawable/radio_green" />

</selector>

Hi use like this:

Text color  - android:textColor="@color/white"
Backgroud - android:background="@color/red"


try this...
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
            if(radioButton.getText().equals("yes")) {
                radioButton.setBackgroundColor(Color.GREEN);
            } else {
                radioButton.setBackgroundColor(Color.RED);
            }
        }
    });



How to check if a radiobutton is checked in a radiogroup in Android?


If you want to check on just one RadioButton you can use the isChecked function
if(radioButton.isChecked())
{
  // is checked    
}
else
{
  // not checked
}
and if you have a RadioGroup you can use
if (radioGroup.getCheckedRadioButtonId() == -1)
{
  // no radio buttons are checked
}
else
{
  // one of the radio buttons is checked
}




Monday 19 December 2016

Mannually set a where condion for dataprovider in specific controller method



This code queryParams take from user index form.

public function actionIndex()
    {
        
            $searchModel = new StudentsSearch();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);

    }


To add additional where condition for data provider.


public function actionIndex()
    {            
            $ccid = Yii::$app->user->identity->ccid;
            $searchModel = new StudentsSearch();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
            $dataProvider->query->andWhere('center_ccid = '.$ccid);         
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

Usages:



$dataProvider = $searchModel->search([$searchModel->formName()=>['result_type'=>$result->result_type,'date'=>$result->date]]);
$query->andFilterWhere(['<>', 'role', $this->role]);
$dataProvider = $searchModel->search(Yii::$app->request->queryParams, $role = 'regular');
$searchModel = new ModelSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->query->andWhere(['lang'=>'ENG']);
$dataProvider = $searchModel->search(Yii::$app->request->queryParams+['EmployeeSearch' => ['<>', 'role' =>'regular']]);



Yii2 How to pass a range to search model


You can also use BETWEEN construction:
$query->andFilterWhere(['between', 'price', $this->min_price, $this->max_price]);

yii2 data provider default sorting


defaultOrder contain a array where key is a column name and value is a SORT_DESC orSORT_ASC 


$dataProvider->setSort([
'defaultOrder' => ['date'=>SORT_DESC,'result_type'=>SORT_ASC,'chance'=>SORT_ASC],
]);


$dataProvider = new ActiveDataProvider([
            'query'=> $query,
            'sort'=> [
                   'defaultOrder'=> ['your_column'=>SORT_ASC]]
       ]);