Skip to content

Instantly share code, notes, and snippets.

@tps2015gh
Last active March 22, 2017 23:10
Show Gist options
  • Save tps2015gh/0772de39d11c4118e57b22b6cb2f33c0 to your computer and use it in GitHub Desktop.
Save tps2015gh/0772de39d11c4118e57b22b6cb2f33c0 to your computer and use it in GitHub Desktop.

Form Helper in Codigniter Framework

https://www.codeigniter.com/userguide3/helpers/form_helper.html

Form Open

echo form_open('email/send', /*atrribut */ ['class' => 'email', 'id' => 'myform']
			, /* hidden */ ['username' => 'Joe', 'member_id' => '234'] );
// form_open_multipart([$action = ''[, $attributes = array()[, $hidden = array()]]])

## Form Input 

Input Field

$data =         ['name'          => 'username',
        'id'            => 'username',
        'value'         => 'johndoe',
        'maxlength'     => '100',
        'size'          => '50',
        'style'         => 'width:50%' ]
echo form_input($data);

form Password

//form_password([$data = ''[, $value = ''[, $extra = '']]])

Form Submit Button

//form_submit([$data = ''[, $value = ''[, $extra = '']]]) 

HTML Redirct / PHP Native Code

 function  get_html_redirect($delay,$url){
         $htm = "<meta http-equiv="refresh" content="$delay; url=$url" />" ; 
	 $htm .= "<b>Please wait $delay second(s) to redirect  </b>";
	 $htm .= "<br>" ;
	 $htm .= "<br>if not redirect please click This link <a href='$url'>$url</a>";  
	 return $htm ; 
 }	
 // usage 
 echo get_html_redirect( "1" , site_url("/mycontroller1/myfunction1/param1") );

REF : https://www.codeigniter.com/userguide2/database/active_record.html

Select Data

$q = $this->db->query("select * from test_user where data_id = ?" , array($data_id));
echo "<br>Last Query = " .  $this->db->last_query();  // View Last query

//Option 2

$this->db->select('title, content, date');
$this->db->from('mytable');
$query = $this->db->get();

//Option 3

$this->db->select('title, content, date');
$this->db->where('title', $title);
$this->db->where('status', $status); 
$this->db->where('name !=', $name);
$this->db->where('id <', $id); 				// AND SQL 
$query = $this->db->get('mytable');

//Option 4

$this->db->select('title, content, date');
$array = array('name' => $name, 'title' => $title, 'status' => $status);
$this->db->where($array);				// AND SQL 
$query = $this->db->get('mytable');

Update Data

$data = array(
              'title' => $title,
              'name' => $name,
              'date' => $date
           );
$this->db->where('id', $id);
$this->db->update('mytable', $data); 

// Option2 
$this->db->update('mytable', $data, "id = 4");

// Option 3 
$this->db->update('mytable', $data, array('id' => $id));

// Option 4 - With Set 
$this->db->set('data2', '1000');            //  normal ESCAPE STRING  !!! 
$this->db->set('data', 'data + 1', false);  // NOT ESCAPE STRING  !!! 
$this->db->where('pagenum', $pagenum);
$this->db->update('table1');

// Display Last SQL for Verify 
echo "<br>LastSQL= ";
echo $this->db->last_query();

Insert Data

$data = array(
   'title' => 'My title' ,
   'name' => 'My Name' ,
   'date' => 'My date'
);
$this->db->insert('mytable', $data);

// Otion - Use Set for Insert 
$this->db->set('name', $name);
$this->db->set('title', $title);
$this->db->set('status', $status);
$this->db->insert('mytable');

Delete Row by id

$this->db->where('id', $id);
$this->db->delete('mytable'); 

Display View ( My Style in Codeigniter )

		$args = [] ; 
		$args['rows']     = $rows  ; 
		$args['a_valxy'] = $a_valxy ; 
		$args['b_found'] = $b_found  ; 
		$args['num_rows'] = $num_rows  ; 
		$view_args =  [ 'Menu1' => ["home", 'banana/home']
						,'Menu2' => ["Link",  'Link/list_link_home']
						];  
		$args['webobj'] = ['viewpage'=> "orange_preview"  
							,'args' => $view_args  
						];   
  
		/* DISPLAY VIEW include header + body + footer  */
		$this->load->view('my_master_frame1.php', ['args'=>$args] );

Check Session

     function __construct() {
       parent::__construct();
       safeguard_check_controller_construct();   // in Helper Function 
    }

Check Field Exist

    /* both paramter is string */
    function field_exist($field,$table){
        assert(is_string($field));
        assert(is_string($table));
        $require1 = $field;
        $uf= $this->db->list_fields($table);     
        //$msg = "table admin must have field $require1  " ;
        if( in_array($require1, $uf)){
            return true ;
        }else{
            return false; 
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment