⚠ 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

CRUD inserts 2 or 4 rows instead of 1



janesjoplin

janesjoplin
  • profile picture
  • Member

Posted 01 January 2014 - 09:36 AM

Hello, I have some troubles with adding records to my table using Grocery CRUD. 
 
When I'm pushing the "Save" button I can see from 2 to 4 POST insert calls. Can this problem be related to the use of custom loader class? 
 
  public function projects(){

    $grocery_crud = new grocery_CRUD();
    $this->grocery_crud->set_table('crm_projects');
        
    $output = $this->grocery_crud->render();
    
    $this->_example_output($output);
   
  }
  
  public function _example_output($output = null){
    $this->data["data"] = $output;
    $this->load->view('crm_inner', $this->data);
  }
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * MY_Loader class extends the core CI_Loader class.
 *
 * @author	Eric 'Aken' Roberts <eric@cryode.com> 
 * @link	https://github.com/cryode/CodeIgniter_Smarty
 * @version	1.0.0
 */
 
class MY_Loader extends CI_Loader {
	
	/**
	 * Replace the default $this->load->view() method
	 * with our own, so we can use Smarty!
	 *
	 * This method works identically to CI's default method,
	 * in that you should pass parameters to it in the same way.
	 *
	 * @access	public
	 * @param	string	The template path name.
	 * @param	array	An array of data to convert to variables.
	 * @param	bool	Set to TRUE to return the loaded template as a string.
	 * @return	mixed	If $return is TRUE, returns string. If not, returns void.
	 */
	public function view($template, $data = array(), $return = false)
	{
		// Get the CI super object, load related library.
		$CI =& get_instance();
		$CI->load->library('smartytpl');
		
		// Add extension to the filename if it's not there.
		$ext = '.' . $CI->config->item('smarty_template_ext');
		
		if (substr($template, -strlen($ext)) !== $ext)
		{
			$template .= $ext;
		}
		
		// Make sure the file exists first.
		if ( ! $CI->smartytpl->templateExists($template))
		{
			show_error('Unable to load the template file: ' . $template);
		}
		
		// Assign any variables from the $data array.
		$CI->smartytpl->assign_variables($data);
		
		// Assign CI instance to be available in templates as $ci
		$CI->smartytpl->assignByRef('ci', $CI);
		
		/*
			Smarty has two built-in functions to rendering templates: display() 
			and fetch(). We're going to	use only fetch(), since we want to take
			the template contents and either return them or add them to
			CodeIgniter's output class. This lets us optionally take advantage
			of some of CI's built-in output features.
		*/
			
		$output = $CI->smartytpl->fetch($template);
		
		// Return the output if the return value is TRUE.
		if ($return === true) return $output;
		
		// Otherwise append to output just like a view.
		$CI->output->append_output($output);
	}

}

janesjoplin

janesjoplin
  • profile picture
  • Member

Posted 01 January 2014 - 09:38 AM

Just in case, the contents of the template file crm_inner.tpl:

{foreach from=$data->js_lib_files item=foo}
    <script src="{$foo}"></script>
{/foreach}
{foreach from=$data->js_files item=foo}
    <script src="{$foo}"></script>
{/foreach}
{foreach from=$data->js_config_files item=foo}
    <script src="{$foo}"></script>
{/foreach}
{foreach from=$data->css_files item=foo}
    <link rel="stylesheet" type="text/css" href="{$foo}" />
{/foreach}
{$data->output}


web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 01 January 2014 - 16:09 PM

Hello [member='janesjoplin'],

 

I think your issue is simple. You simply need to remove the code:

{foreach from=$data->js_lib_files item=foo}
    <script src="{$foo}"></script>
{/foreach}

and:

{foreach from=$data->js_config_files item=foo}
    <script src="{$foo}"></script>
{/foreach}

So in your case you can simply have this code in your template file ( crm_inner.tpl).

//crm_inner.tpl:

{foreach from=$data->js_files item=foo}
    <script src="{$foo}"></script>
{/foreach}
{foreach from=$data->css_files item=foo}
    <link rel="stylesheet" type="text/css" href="{$foo}" />
{/foreach}
{$data->output}

and it will simply work :-)

 

Please let me know if you have any issues with that.

 

Thanks

Johnny


janesjoplin

janesjoplin
  • profile picture
  • Member

Posted 01 January 2014 - 17:15 PM

Thank you very much, it works perfectly.