⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

add action button to copy a row



maru
  • profile picture
  • Member

Posted 04 February 2013 - 20:33 PM

Hello!

 

I have 2 tables: customers and standby and I added a new button into my grocery crud in standby in that way: 

 

$crud->add_action('Send', '', 'standby/copyrow','ui-icon-plus');

Both tables have the same fields, in standby I receive data from a form and the idea it's when I click "Send" copy that id ino customers and delete that id from standby

 

 

In other words I need to add a row from the first table (where I added a new button) into the second one, and then delete that row from the first table.

 

then I created a function

 

function copyrow($id)
    {
       // I don't know how can I add that row 
    }

 

Hope someone can help me, thanks in advance!

 

j-gun
  • profile picture
  • Member

Posted 05 February 2013 - 00:13 AM

@maru

 

Use the $id inside copyrow to delete from first table and insert into to another table. Because it was a database action, so you should use a model instead.

 

Inside the Controller:

-------------------------------

function copyrow($id)

{

   $this->load->model('customer_model','customer');

   $this->load->model('standby_model','standy');

   $this->customer->insert($id); //insert into another table first

   $this->standby->delete($id); //then you can delete the source

}

 

Inside the Customer Model :

-------------------------------------

public function insert($id) {

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

        $rs = $this->db->get('standby')->result_array();


        $data['id'] = $rs['id'];
        $data['field2'] = $rs['field2'];
        $data['field3'] = $rs['field3'];

        // so on.....
        $this->db->insert('customer', $data);

    }

 

Inside the Standby Model :

-------------------------------------

public function delete($id){
        $this->db->delete('standby', array('id'=>$id));
}


maru
  • profile picture
  • Member

Posted 06 February 2013 - 01:41 AM

hey j-gun! thank u so much for your help!

I'll copy ,u full code because maybe I am doing something wrong.

 

my controller standby

 

 

 

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Standby extends CI_Controller {

    function __construct() 
    {
        
        parent::__construct();
        
        $this->load->database();
        $this->load->library('grocery_crud');
        $this->load->helper('url');
    }


    function index() 
    {
        
    }


   
    function standby_v()
    {
                
        try{
            
            $crud = new grocery_CRUD();

            $crud->set_theme('datatables');

            $crud->set_table('standby');

            $crud->set_subject('Standby');

            $crud->set_language('spanish');

            $crud->required_fields(
                'id_standby',
                'name',
                'email'
            );


            $crud->columns(
                'id_standby',
                'name',
                'email',
                'address',
                'phone',
                'password'
            );
            
            $crud->add_action('Send', '', 'copyrows','ui-icon-plus');
            
            $output = $crud->render();

            $this->load->view('standby/standby_v', $output);
            
        }catch(Exception $e){
            
            show_error($e->getMessage().' --- '.$e->getTraceAsString());
        }
        
       
    }
}




function copyrows($id)
    {
       $this->load->model('customer_model','customers');
       $this->load->model('standby_model','standby');
       $this->customers->insert($id); //insert into another table first
       $this->standby->delete($id); //then you can delete the source
    }
 

 

and inside the folder model

 

customer_model.php

 

 

 

<?php
class Customer_model extends grocery_crud_model // I tried without it too
{
    public function insert($id) {
            $this->db->where('id_standby',$id);
            $rs = $this->db->get('standby')->result_array();
    
            $data['id_standby'] = $rs['id_standby']; 
            $data['name'] = $rs['name'];
            $data['email'] = $rs['email'];
            $data['address'] = $rs['address'];
            $data['phone'] = $rs['phone'];
            $data['password'] = $rs['password'];
            
            $this->db->insert('customers', $data);
        }
}    
?>

 

 

and then in the same folder

standby_model.php

 

 

 

<?php
class Standby_model extends CI_Model //doesn't work neither
{
    public function delete($id){
            $this->db->delete('standby', array('id_standby'=>$id));
    }
}
?>
 

 

What am I doing wrong?


victor
  • profile picture
  • Member

Posted 06 February 2013 - 09:10 AM

Hi! What error do you get?


j-gun
  • profile picture
  • Member

Posted 06 February 2013 - 13:17 PM

@maru

 

Same question as Victor  :)


maru
  • profile picture
  • Member

Posted 06 February 2013 - 22:31 PM

hi guys! 

no insert and no delete when I cleck Send and "404 page not found" http://localhost/crud/index.php/copyrows/13 

 

number 13 in this case is an id

 

by the way the id from standby is id_standby and from customer is id_customer


victor
  • profile picture
  • Member

Posted 06 February 2013 - 23:01 PM

index.php/controller/function/params your: index.php/controller/params

maru
  • profile picture
  • Member

Posted 06 February 2013 - 23:18 PM

I added this

 

 

 

 $crud->add_action('Send', '', 'standby/copyrows','ui-icon-plus');
 

where copyrows is the function and standby is the controller

 

now I have http://localhost/crud/index.php/standby/copyrows/17 

404 page not found

 

:wacko:


maru
  • profile picture
  • Member

Posted 06 February 2013 - 23:22 PM

sorry I added this now $crud->add_action('Send', '', 'standby/standby_v/copyrows','ui-icon-plus');

 

and I get the view, but inser and delete it's not working


victor
  • profile picture
  • Member

Posted 06 February 2013 - 23:22 PM

your function isn't in class!!!


 

from your code:
class Standby extends CI_Controller
{
 // your functions
}


function copyrows($id)
{
}
 


victor
  • profile picture
  • Member

Posted 06 February 2013 - 23:24 PM

 class Standby extends CI_Controller
 {
  // your functions

 


 function copyrows($id)
 {
 }


 }


maru
  • profile picture
  • Member

Posted 06 February 2013 - 23:34 PM

now my code is 

 

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Standby extends CI_Controller {

    function __construct() 
    
{
        
        parent
::__construct();
        
        $this
->load->database();
        $this->load->library('grocery_crud');
        $this->load->helper('url');
    }


    function index() 
    
{
        
    
}


   
    
function standby_v()
    {
                
        
try{
            
            $crud
= new grocery_CRUD();

            $crud->set_theme('datatables');

            $crud->set_table('standby');

            $crud->set_subject('Standby');

            $crud->set_language('spanish');

            $crud->required_fields(
                'id_standby',
                'name',
                'email'
            );


            $crud->columns(
                'id_standby',
                'name',
                'email',
                'address',
                'phone',
                'password'
            );
            
            $crud
->add_action('Send', '', 'copyrows','ui-icon-plus');
            
            $output
= $crud->render();

            $this->load->view('standby/standby_v', $output);
            
        
}catch(Exception $e){
            
            show_error
($e->getMessage().' --- '.$e->getTraceAsString());
        }
        
       
    
}

 

function copyrows($id)
    {
       $this->load->model('customer_model','customers');
       $this->load->model('standby_model','standby');
       $this->customers->insert($id); //insert into another table first
       $this->standby->delete($id); //then you can delete the source
    }


}


but nothing....  :blink: 

 


maru
  • profile picture
  • Member

Posted 06 February 2013 - 23:36 PM

<?php
class Customers_model extends CI_Controller
{
    public function insert($id) {
            $this->db->where('id_standby',$id);
            $rs = $this->db->get('standby')->result_array();
    
            $data['id_standby'] = $rs['id_standby']; 
            $data['name'] = $rs['name'];
           ...
            
            $this->db->insert('customers', $data);
        }
}    
?>

victor
  • profile picture
  • Member

Posted 06 February 2013 - 23:38 PM

do you get 404?

maru
  • profile picture
  • Member

Posted 06 February 2013 - 23:45 PM

nop! it show me the same view with the same content in the table 

 

I mean, insert and delete doesn't work


victor
  • profile picture
  • Member

Posted 06 February 2013 - 23:48 PM

sorry, I go to bed) it's 2.40. I can help you tommorow


maru
  • profile picture
  • Member

Posted 06 February 2013 - 23:49 PM

thank u Victor! take care!

 

I'll try few more things, maybe I have luck


j-gun
  • profile picture
  • Member

Posted 07 February 2013 - 03:07 AM

@maru

 

Is your function load properly? try to debug it with echo inside your function to make sure it works.


maru
  • profile picture
  • Member

Posted 07 February 2013 - 21:04 PM

sorry I can't find the problem... if I call my function I have an error server

I can copy the whole code or send it.

 

I was searching the whole day for a solution but I had no luck.


maru
  • profile picture
  • Member

Posted 07 February 2013 - 21:16 PM

with this 

 

 

$crud->add_action('Send', '', 'standby/standby_v/copyrows','ui-icon-plus');
echo $this->copyrows();
 
 
A PHP Error was encountered

Severity: Warning

Message: Missing argument 1 for Standby::copyrows(), called in C:\xampp\htdocs\crud\application\controllers\standby.php on line 82 and defined

Filename: controllers/standby.php

Line Number: 101

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: id

Filename: controllers/standby.php

Line Number: 105

A PHP Error was encountered

Severity: Notice

Message: Undefined index: id_standby

Filename: models/customer_model.php

Line Number: 8

A PHP Error was encountered

Severity: Notice

Message: Undefined index: name

Filename: models/customer_model.php

Line Number: 9

A PHP Error was encountered

Severity: Notice

Message: Undefined index: email

Filename: models/customers_model.php

Line Number: 10

A PHP Error was encountered

Severity: Notice

Message: Undefined index: job

Filename: models/customers_model.php

Line Number: 11

A PHP Error was encountered

Severity: Notice

Message: Undefined index: cuit

Filename: models/customers_model.php

Line Number: 12

A PHP Error was encountered

Severity: Notice

Message: Undefined index: address

Filename: models/customers_model.php

Line Number: 13

A PHP Error was encountered

Severity: Notice

Message: Undefined index: phone

Filename: models/customers_model.php

Line Number: 14

A PHP Error was encountered

Severity: Notice

Message: Undefined index: password

Filename: models/customers_model.php

Line Number: 15

A Database Error Occurred

Error Number: 1054

Unknown column 'id_standby' in 'field list'

INSERT INTO `customers` (`id_standby`, `name`, `email`, `job`, `cuit`, `address`, `phone`, `password`) VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)

Filename: C:\xampp\htdocs\crud\system\database\DB_driver.php

Line Number: 330