MySQL query with if else condition

MySql query with if-else condition:

Here’s the statement:

SELECT CASE(
WHEN condition THEN statement
WHEN condition THEN statement
ELSE statement
END 
) 
FROM table
WHERE 
conditions 

Example:

SELECT 
    eps_year,
    (CASE
        WHEN q4_eps != '0' THEN q4_eps
        WHEN q3_eps != '0' THEN (q3_eps * (4 / 3))
        WHEN q2_eps != '0' THEN (q2_eps * 2)
        WHEN q1_eps != '0' THEN (q1_eps * 4)
        ELSE 0
    END) AS eps
FROM
    gdslreport.basic_eps_con
WHERE
    trading_code = 'EXIMBANK'
ORDER BY eps_year DESC
LIMIT 1

Codeigniter CSRF with AJAX

Hi,
Thats the very important issue when you enable csrf protection for security reason in codeigniter framework based project then you must face some problem ajax related. So here’s the code how to resolve it :

<script type="text/javascript">
$(function(){
   $('.answerlist').each(function(e){

  $(this).click(function(){

    var valrad = $("input[@name=answer]:checked").val();

    var post_data = {
        'ansid': valrad,
        '<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>'
    };

        $.ajax({
                type: "POST",
                url: "<?php echo base_url(); ?>online/checkanswer",
                data: post_data,
                success: function(msg){
                  /// do something 
                }
            });

  });

   });

});

</script>

How to Create Select field using Zend Framework2 Form

First I  Create Form Class name CategoryForm.php in Form directory. Source Code

<?php
// CategoryForm.php

namespace Admin\Form;

use Zend\Form\Form;

class CategoryForm extends Form {

    public function __construct($name = null) {

        parent::__construct('category');

        $this->setAttribute('method', 'post');
        $this->setAttribute('class', 'form-horizontal');

        $this->add(array(     
            'type' => 'Zend\Form\Element\Select',       
            'name' => 'parentid',
            'attributes' =>  array(
                'id' => 'parentid',
            ),

        ));    

    }

}

In Controller i add this lines:

$form = new CategoryForm();

$form->get('submit')->setValue('Add');

$form->get('parentid')->setValueOptions(array(
                                                '0' => 'Dhaka',
                                                '1' => 'Chittagong',
                                                '2' => 'Barisal',
                                                '3' => 'Khulna',
                                              ));

In view file i add this line :

echo $form->formSelect($form->get('parentid'));

 

Codeigniter pagination style with twitter bootstrap pagination

I want to style my pagination view with twitter bootstrap style and i used codeigniter php framework and twitter bootstrap. First i create controller name is front  and action is news.

public function news() {
$this->load->library('pagination');  // load pagination library
$config['base_url'] = base_url() . 'front/news';
$config['total_rows'] = $this->Fronts->totalNews(); // get no of news in my database
$config['per_page'] = 20; // no of news per page view
$config['full_tag_open'] = '';
$config['prev_link'] = '< Prev';
$config['prev_tag_open'] = '
  • '; $config['prev_tag_close'] = '
  • '; $config['next_link'] = 'Next >'; $config['next_tag_open'] = '
  • '; $config['next_tag_close'] = '
  • '; $config['cur_tag_open'] = '
  • '; $config['cur_tag_close'] = '
  • '; $config['num_tag_open'] = '
  • '; $config['num_tag_close'] = '
  • '; $config['first_link'] = FALSE; $config['last_link'] = FALSE; $this->pagination->initialize($config); $data['content'] = $this->Fronts->getAllNewsNotice(1, $config['per_page'], $this->uri->segment(3)); $this->load->view('inner/all_news', $data); }

    Then in view page write this code:

     pagination->create_links(); ?>
    

    After that my pagination look like twitter bootstrap pagination components.

    how to install LAMP – Apache, MySQL & PHP on Ubuntu 11.10

    LAMP : Linux Apache MySQL, PHP/Perl but in that case P : = PHP.  This is totaly open source stack. I follow this step –

    step 1. Installing Apache

    Open terminal (press Ctrl+Alt+t) and execute the command -

    sudo apt-get install apache2

    Then Open browser and type http://loacalhost. See It Works page ..

     

    step 2. Installing MySQL

    Execute this command in terminal (Ctrl+Alt+t) :


    sudo apt-get install mysql-server

    In installation process need set password for mysql administrative user.

    step 3. Installing PHP


    sudo apt-get install php5 libapache2-mod-php5 php5-mysql

    Then restart the apache server to load php modules. Write following command in Terminal -


    sudo /etc/init.d/apache2 restart

    After all process complete that’s ok.

    To manage MySQL database from a GUI, You may like to install phpMyAdmin – The MySQL web administration tool, a great application for managing – creating and maintaining MySQL database.

    Install phpMyAdmin on Ubuntu 11.10

    just execute the command  in your terminal -

    sudo apt-get install phpmyadmin

    on install process you need select web server such as >> apache2 and >> lighthttp.
    Enter mysql root user password and phpmyadmin password.

    Then finish….

    Make windows default boot OS alongside ubuntu OS

    When installed ubuntu 11.10 in my laptop then grub boot loader default start ubuntu os. But i need change this way thats means i want boot default start my windows 7. So i need configure my grub boot loader ….. i follow this step ………

     

    start terminal (ctrl+alt+t) then type

    sudo nano /etc/default/grub

    This command show this ::

    # If you change this file, run 'update-grub' afterwards to update
    # /boot/grub/grub.cfg.
    # For full documentation of the options in this file, see:

    # info -f grub -n ‘Simple configuration’

    GRUB_DEFAULT=0
    #GRUB_HIDDEN_TIMEOUT=0
    GRUB_HIDDEN_TIMEOUT_QUIET=true
    GRUB_TIMEOUT=10
    GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
    GRUB_CMDLINE_LINUX_DEFAULT=”quiet splash”
    GRUB_CMDLINE_LINUX=”"

    # Uncomment to enable BadRAM filtering, modify to suit your needs
    # This works with Linux (no patch required) and with any kernel that obtains
    # the memory map information from GRUB (GNU Mach, kernel of FreeBSD …)
    #GRUB_BADRAM=”0×01234567,0xfefefefe,0x89abcdef,0xefefefef”

    # Uncomment to disable graphical terminal (grub-pc only)
    #GRUB_TERMINAL=console

    # The resolution used on graphical terminal
    # note that you can use only modes which your graphic card supports via VBE
    # you can see them in real GRUB with the command `vbeinfo’
    #GRUB_GFXMODE=640×480

    # Uncomment if you don’t want GRUB to pass “root=UUID=xxx” parameter to Linux
    #GRUB_DISABLE_LINUX_UUID=true

    # Uncomment to disable generation of recovery mode menu entries
    #GRUB_DISABLE_RECOVERY=”true”

    # Uncomment to get a beep at grub start
    #GRUB_INIT_TUNE=”480 440 1″

    Then i change this line ::

    GRUB_DEFAULT=0

    Into


    GRUB_DEFAULT=5

    After change this save /etc/default/grub file
    Again goto terminal and write this command


    sudo update-grub

    Then restart my laptop and it’s working … … now perfect —