⚠ 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

Insert Data not working



Rodrigo4ever

Rodrigo4ever
  • profile picture
  • Member

Posted 11 July 2017 - 04:28 AM

What not working?

 

public function index(){
			$crud = new grocery_CRUD();
			echo $state = $crud->getState();
			$state_info = $crud->getStateInfo();

				$crud->set_table('proxsorteios');
				$crud->unset_jquery();
				$crud->set_theme('datatables');
				$crud->set_subject('Proximo Sorteio');
				$crud->order_by('id', 'desc');
				$crud->limit(5);
				$crud->columns('id', 'data', 'proxExtracao');
				$crud->set_field_upload('img_cautela','assets/uploads/files/proxsorteios');


			if($state === 'list'){
				$output = $crud->render();
			}
			if($state === 'insert'){
				print_r($_POST);
				$output = $crud->render();
			}
			if($state === 'add'){
				echo "<script>alert('Entrei')</script>";
				// $crud->set_table('proxsorteios');

				$crud->unset_jquery();
				$crud->set_theme('datatables');
				$crud->set_subject('Adicionar Proximo Sorteio');

				$crud->unset_add_fields('desc_rodada2','foto_premio1','foto_premio2', 'foto_premio3', 'foto_premio4');
				$crud->fields('data','n_susep', 'desc_rodada', 'separador', 'desc_premio1', 'desc_premio2', 'desc_premio3', 'desc_premio4', 'img_cautela', 'proxExtracao');
				$crud->field_type('data', 'date');
				$crud->callback_before_insert(array($this, '_callback_before'));

				$output = $crud->render();
		 }
		 $this->_example_output($output);
  }
  public function _callback_before($post_array)
  {
       echo "<script>alert('callback_before_insert')</script>";
       // return $post_array;
       return true;
  }
  public function _example_output($output = null)
  {

   $this->load->model("config_model");
   $config_site = $this->config_model->buscaConfig();

   $head = array(
            'author' => 'Rodrigo Barbosa',
	    'description' => '',
	    'title' => 'Painel de Controle - Site'
	  );
	 $nav = array(
	         'site' => $config_site['site'],
	       );

    $this->load->view('head', $head);
    $this->load->view('layout-bootstrap/css/template-bootstrap-css');
    $this->load->view('layout-bootstrap/nav.php', $nav);
    // $this->load->view('dashboard');
    $this->load->view('usuarios',(array)$output);
    $this->load->view('layout-bootstrap/js/template-bootstrap-js');
}

Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 13 July 2017 - 05:27 AM

Well i will not recommend on 1 thing.. setting callback before insert to be set in the add state .. Technically it dont mind being set across globally in the function and in any state.

 

And am not able to understand what you want to convey - what is not working - is it your insert is not working .. or your callback before insert is not working..

 

Please clarify


Rodrigo4ever

Rodrigo4ever
  • profile picture
  • Member

Posted 13 July 2017 - 11:09 AM

Obrigado por tentar me ajudar Amit Shah. Já estava desacreditado numa resposta.

Bom, o que eu quero e relativamente simples, e acredito que a Framework deve sim fazer isso, só que não achei nenhum material que demonstrasse isso.
 

O que eu queria mesmo era separar as ações dentro de escopos diferentes. E acredito que é pra isso que sirva os if($state === 'add')

Dentro dessa verificação eu trataria dados e mandaria renderizar, pelo menos isso deveria ser a logica do FrameWork.

Bom, enfim, o que eu preciso fazer é somente tratar dados que vem dos meus inputs antes de submetê-los ao bando de dados.
Que eu acho que o framework fez essa função: $crud->callback_before_insert(array($this, '_callback_before'));

Mas ela não está sendo chamada, não mostra nada, não está tratando nada e inserindo campos não tratados.


=======================================================================================================

 

Thank you for trying to help me, Amit Shah. He was already discredited in an answer.

Well, what I want is relatively simple, and I believe that the Framework should rather do this, but I do not find any material that demonstrates this.

What I really wanted was to separate as actions within different scopes. And believe it's what it is you want, if ($ state === 'add')
Inside the selection of electronic data and would render, at least, is a logic of FrameWork.

Well, anyway, what I need to do is just process data that comes from my inputs before submitting it when sending data.
I think the framework did the function: $ crud-> callback_before_insert (array ($ this, '_callback_before'));
But it is not being called, it shows nothing, it is not treating anything and inserting untreated fields.

/// Not Working
public function _callback_before($post_array)
{
	    echo "<script>alert('callback_before_insert')</script>";
	    // return $post_array;
	    return true;
}

Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 13 July 2017 - 11:37 AM

Eu pensei assim. Experimente e compreenda esse processo simples. Este é um aplicativo sem estado. É ajax conduzido. O gancho callback_before_insert foi projetado para ser chamado apenas no estado de inserir / salvar.
 
O que você está tentando fazer é definir o evento somente dentro do estado "adicionar". O estado "adicionar" é mesmo desencadeado para o método que indica: o usuário deseja adicionar um novo registro - mostrar-lhe um novo formulário. O estado em que você está interessado é "inserir" ou "salvar" (por favor, consulte o código ... eu esqueci o estado exato). Agora, em tal cenário - quando retorna como chamada ajax com estado "inserir" - esse evento não será registrado. Por isso - falhará.
 
É um gancho. Não é necessário executar o mesmo apenas no estado de adição. Então, quando o estado é inserido, o código é executado de novo. Desta vez, não registrou o evento. Assim, o código nunca será executado.
 
Mesmo que você escreva o código fora do estado (que é o caminho ocioso preferido), ele será executado apenas no estado apropriado.
 
Feliz GCing :)

 

ENGLISH TRANSLATION TO MY OTHER FRIENDS HERE

 
 
I thought so. Try and understand this simple process. This is a stateless application. It is ajax driven. The hook callback_before_insert is designed to be called only in the state of insert / save. 
 
What you are trying to do is setting the event only inside the state "add". The state "add" is an even triggered to the method stating - user wants to add a new record - show him a new form. The state where u are interested is "insert" or "save" (pls refer to the code .. i forgot the exact state). Now in such scenario - when it comes back as ajax call with state "insert" - that event wont be registered. Hence - it will fail. 
 
Its a hook. There is no need to execute the same only in the state of add.. So when the state is insert, the code re-runs. This time, it have not registered the event. Hence code will never execute.
 
Even if you write the code outside the state (which is the prefered idle way), it will be executed only in the appropriate state.
 
Happy GCing :)

Rodrigo4ever

Rodrigo4ever
  • profile picture
  • Member

Posted 13 July 2017 - 12:08 PM

So I have put $crud->callback_before_insert(array($this, '_callback_before'))  inside of :

if($state === 'insert'){

                print_r($_POST);
                $output = $crud->render();
}

 

?????

 

Because of translate sometimes I can not understand, do you have any sample code?

Rodrigo4ever

Rodrigo4ever
  • profile picture
  • Member

Posted 13 July 2017 - 12:12 PM

public function index(){
			$crud = new grocery_CRUD();
			echo $state = $crud->getState();
			$state_info = $crud->getStateInfo();

				$crud->set_table('proxsorteios');
				$crud->unset_jquery();
				$crud->set_theme('datatables');
				$crud->set_subject('Proximo Sorteio');
				$crud->order_by('id', 'desc');
				$crud->limit(5);
				$crud->columns('id', 'data', 'proxExtracao');
				$crud->set_field_upload('img_cautela','assets/uploads/files/proxsorteios');

			if($state === 'list'){
				$output = $crud->render();
			}
			if($state === 'insert'){
				$crud->callback_before_insert(array($this, '_callback_before'));
			}
			if($state === 'add'){
				echo "<script>alert('Entrei')</script>";
				// $crud->set_table('proxsorteios');

				$crud->unset_jquery();
				$crud->set_theme('datatables');
				$crud->set_subject('Adicionar Proximo Sorteio');

				$crud->unset_add_fields('desc_rodada2','foto_premio1','foto_premio2', 'foto_premio3', 'foto_premio4');
				$crud->fields('data','n_susep', 'desc_rodada', 'separador', 'desc_premio1', 'desc_premio2', 'desc_premio3', 'desc_premio4', 'img_cautela', 'proxExtracao');
				$crud->field_type('data', 'date');
				$crud->callback_before_insert(array($this, '_callback_before'));

				$output = $crud->render();
		 }
		 $this->_example_output($output);
  }
	public function _callback_before($post_array)
	 {
			 echo "<script>alert('callback_before_insert')</script>";
			 // return $post_array;
			 return true;
	 }
  public function _example_output($output = null)
	{

		$this->load->model("config_model");
		$config_site = $this->config_model->buscaConfig();

		$head = array(
			'author' => 'Rodrigo Barbosa',
			'description' => '',
			'title' => 'Painel de Controle - Site'
		);
		$nav = array(
				'site' => $config_site['site'],
		);

It's still not working. It shows the message "Loading, saving data ..." and does not save to the database.


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 14 July 2017 - 05:12 AM

Ao invés de
Se ($ state === 'insert') {
     $ Crud-> callback_before_insert (array ($ this, '_callback_before'));
}
 
Deveria ser apenas
// if ($ state === 'insert') {
     $ Crud-> callback_before_insert (array ($ this, '_callback_before'));
//}
 
Como eu já disse antes ... Não faz sentido ter o retorno de chamada dentro de qualquer estado. Como é um evento que irá desencadear apenas no tempo quando necessário.
 
Isso deve funcionar agora ..
Feliz GCing :)

 

 

ENGLISH TRANSLATED VERSION

 

Instead of

if($state === 'insert'){
    $crud->callback_before_insert(array($this, '_callback_before'));
}

It should be just 

//if($state === 'insert'){
    $crud->callback_before_insert(array($this, '_callback_before'));
//}

As i told you earlier.. It dose not make any sense in having the callback inside any state. as it is an event which will trigger only on the time when required.

 

This should work now..


Rodrigo4ever

Rodrigo4ever
  • profile picture
  • Member

Posted 14 July 2017 - 13:57 PM

Still does not working, see:

 

	public function index(){
			$crud = new grocery_CRUD();
			echo $state = $crud->getState();
			$state_info = $crud->getStateInfo();

				$crud->set_table('proxsorteios');
				$crud->unset_jquery();
				$crud->set_theme('datatables');
				$crud->set_subject('Proximo Sorteio');
				$crud->order_by('id', 'desc');
				$crud->limit(5);
				$crud->columns('id', 'data', 'proxExtracao');
				$crud->set_field_upload('img_cautela','assets/uploads/files/proxsorteios');

			if($state === 'list'){
				$output = $crud->render();
			}
			// if($state === 'insert'){
			// 	$crud->callback_before_insert(array($this, '_callback_before'));
			// }
			if($state === 'add'){
				echo "<script>alert('Entrei')</script>";
				// $crud->set_table('proxsorteios');

				$crud->unset_jquery();
				$crud->set_theme('datatables');
				$crud->set_subject('Adicionar Proximo Sorteio');

				$crud->unset_add_fields('desc_rodada2','foto_premio1','foto_premio2', 'foto_premio3', 'foto_premio4');
				$crud->fields('data','n_susep', 'desc_rodada', 'separador', 'desc_premio1', 'desc_premio2', 'desc_premio3', 'desc_premio4', 'img_cautela', 'proxExtracao');
				$crud->field_type('data', 'date');
				$crud->callback_before_insert(array($this, '_callback_before'));

				$output = $crud->render();
		 }
		 $this->_example_output($output);
  }
	public function _callback_before($post_array)
	 {
			 echo "<script>alert('callback_before_insert')</script>";
			 // return $post_array;
			 return true;
	 }
  public function _example_output($output = null)
	{

		$this->load->model("config_model");
		$config_site = $this->config_model->buscaConfig();

		$head = array(
			'author' => 'Rodrigo Barbosa',
			'description' => '',
			'title' => 'Painel de Controle - Site'
		);
		$nav = array(
				'site' => $config_site['site'],
		);

    $this->load->view('head', $head);
    $this->load->view('layout-bootstrap/css/template-bootstrap-css');
    $this->load->view('layout-bootstrap/nav.php', $nav);
    // $this->load->view('dashboard');
    $this->load->view('usuarios',(array)$output);
    $this->load->view('layout-bootstrap/js/template-bootstrap-js');
	}

Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 15 July 2017 - 18:46 PM

Well my friend,

 

Please try and understand the reason of the given state. For your code.. there is no need of you checking any state .. It will be in best interest not to use the state if you dont really understand the flow of the application. For me, what i understand is - the state management is for advanced programing .. controlling the flow going out of the box. What in your code you are trying to achieve is nothing out of box.. its all simple and usual. I am sharing you the simple / rewritten code... that should work 100% ..

	public function index(){
		$crud = new grocery_CRUD();
		echo $state = $crud->getState();
		$state_info = $crud->getStateInfo();
		$crud->set_table('proxsorteios');
		$crud->unset_jquery();
		$crud->set_theme('datatables');
		$crud->set_subject('Proximo Sorteio');
		$crud->order_by('id', 'desc');
		$crud->limit(5);
		$crud->columns('id', 'data', 'proxExtracao');
		$crud->set_field_upload('img_cautela','assets/uploads/files/proxsorteios');
			
		$crud->unset_jquery();
		$crud->set_theme('datatables');
		$crud->set_subject('Adicionar Proximo Sorteio');
		$crud->unset_add_fields('desc_rodada2','foto_premio1','foto_premio2', 'foto_premio3', 'foto_premio4');
		$crud->fields('data','n_susep', 'desc_rodada', 'separador', 'desc_premio1', 'desc_premio2', 'desc_premio3', 'desc_premio4', 'img_cautela', 'proxExtracao');
		$crud->field_type('data', 'date');
		$crud->callback_before_insert(array($this, '_callback_before'));
		$output = $crud->render();
	 
		 $this->_example_output($output);
  }
	public function _callback_before($post_array)
	 {
			 echo "<script>alert('callback_before_insert')</script>";
			 // return $post_array;
			 return true;
	 }
  public function _example_output($output = null)
	{

		$this->load->model("config_model");
		$config_site = $this->config_model->buscaConfig();

		$head = array(
			'author' => 'Rodrigo Barbosa',
			'description' => '',
			'title' => 'Painel de Controle - Site'
		);
		$nav = array(
				'site' => $config_site['site'],
		);

    $this->load->view('head', $head);
    $this->load->view('layout-bootstrap/css/template-bootstrap-css');
    $this->load->view('layout-bootstrap/nav.php', $nav);
    // $this->load->view('dashboard');
    $this->load->view('usuarios',(array)$output);
    $this->load->view('layout-bootstrap/js/template-bootstrap-js');
	}

Happy GCing :)


Rodrigo4ever

Rodrigo4ever
  • profile picture
  • Member

Posted 17 July 2017 - 14:38 PM

A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 1

Filename: libraries/Grocery_CRUD.php

Line Number: 282

Backtrace:

File: D:\Google Drive\SAMSUNG_WHITE\htdocs\rondoncap\atual\admin2\application\libraries\Grocery_CRUD.php
Line: 282
Function: _error_handler


Rodrigo4ever

Rodrigo4ever
  • profile picture
  • Member

Posted 17 July 2017 - 15:02 PM

Após alguns testes, o código abaixo funcionou, porém fica uma mensagem de "Loading, saving data..." e não salva na base de dados.

Alguém sabe me ajudar?

 

====================

After some testing, the code below works, a "Loading, saving data ..." message and is not a database.


Does anyone know how to help me?

public function index(){

		$crud = new grocery_CRUD();

		echo $state = $crud->getState();
		$state_info = $crud->getStateInfo();

		$crud->set_table('proxsorteios');
		$crud->set_theme('datatables');
		$crud->set_subject('Proximo Sorteio');
		$crud->order_by('id', 'desc');
		$crud->limit(5);
		$crud->columns('id', 'data', 'proxExtracao');
		$crud->set_field_upload('img_cautela','assets/uploads/files/proxsorteios');
		//$crud->unset_jquery();
		// $crud->unset_jquery();
		// $crud->set_theme('datatables');
		// $crud->set_subject('Adicionar Proximo Sorteio');
		// $crud->unset_add_fields('desc_rodada2','foto_premio1','foto_premio2', 'foto_premio3', 'foto_premio4');
		$crud->unset_add_fields('desc_rodada2','foto_premio1','foto_premio2', 'foto_premio3', 'foto_premio4');
		$crud->fields('data','n_susep', 'desc_rodada', 'separador', 'desc_premio1', 'desc_premio2', 'desc_premio3', 'desc_premio4', 'img_cautela', 'proxExtracao');
		$crud->field_type('data', 'date');
		$crud->callback_before_insert(array($this, '_callback_before'));

		$output = $crud->render();
		$this->_example_output($output);


			// if($state === 'insert'){
			// 	$crud->callback_before_insert(array($this, '_callback_before'));
			// }
		// 	if($state === 'add'){
		// 		echo "<script>alert('Entrei')</script>";
		// 		// $crud->set_table('proxsorteios');
		 //
		// 		$crud->unset_jquery();
		// 		$crud->set_theme('datatables');
		// 		$crud->set_subject('Adicionar Proximo Sorteio');
		 //
		// 		$crud->unset_add_fields('desc_rodada2','foto_premio1','foto_premio2', 'foto_premio3', 'foto_premio4');
		// 		$crud->fields('data','n_susep', 'desc_rodada', 'separador', 'desc_premio1', 'desc_premio2', 'desc_premio3', 'desc_premio4', 'img_cautela', 'proxExtracao');
		// 		$crud->field_type('data', 'date');
		// 		$crud->callback_before_insert(array($this, '_callback_before'));
		 //
		// 		$output = $crud->render();
		//  }
		//  $this->_example_output($output);
  }
	public function _callback_before($post_array)
	 {
			 echo "<script>alert('callback_before_insert')</script>";
			 // return $post_array;
			 return true;
	 }
  public function _example_output($output = null)
	{

		$this->load->model("config_model");
		$config_site = $this->config_model->buscaConfig();

		$head = array(
			'author' => 'Rodrigo Barbosa',
			'description' => '',
			'title' => 'Painel de Controle - Site'
		);
		$nav = array(
				'site' => $config_site['site'],
		);

    $this->load->view('head', $head);
    $this->load->view('layout-bootstrap/css/template-bootstrap-css');
    $this->load->view('layout-bootstrap/nav.php', $nav);
    // $this->load->view('dashboard');
    $this->load->view('usuarios',(array)$output);
    $this->load->view('layout-bootstrap/js/template-bootstrap-js');
	}

Rodrigo4ever

Rodrigo4ever
  • profile picture
  • Member

Posted 17 July 2017 - 15:24 PM

Error in Firebug:
 

http://[::1]/rondoncap/atual/admin2/assets/sb-admin-2/vendor/jquery/jquery.js:9393 POST http://[::1]/rondoncap/atual/admin2/Proxsort/index/insert 500 (Internal Server Error)

http://[::1]/rondoncap/atual/admin2/assets/sb-admin-2/vendor/jquery/jquery.js:9393 POST http://[::1]/rondoncap/atual/admin2/Proxsort/index/insert 500 (Internal Server Error)


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 18 July 2017 - 06:59 AM

Good - now since you got in Internal server error - you need to identify .. what error it is - check the console output / response for the request set -if there is any error there -rectify the same..

else do the following

 

1. Enable threshold in the config.php from 0-1

2. In database, enable debug mode - set it to true.

 

Whatever error - it will print it in application/logs/<some log file>.php

 

there you can check and see what was the error and then go back and rectify the same.