Jump to content


about row and input data


  • Please log in to reply
18 replies to this topic

#1 codefreak999

codefreak999

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 06 April 2012 - 09:51 AM

Hello,

I'm newbie in Grocery Crud. And I need the fast information for my project.
This my some question:
  • How to modify name row fieldname database. For example in database field name : 'nme' and in the view i want to use 'your name' ?
  • How to make a dinamic value for id in input data Grocery. We can not input data id_ and system can make itself. For example in db record has 2000 and if i create data the id_ value was be automatic become 2001?
  • To make a some value for input data. maybe : input country => english, arabia, australia, .... (not from database and just input in code)?

Thank you for anyone want to help me. :)

#2 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 999 posts
  • LocationLondon

Posted 06 April 2012 - 10:19 AM

Hello codefreak999 and welcome to the forum,

1. use the display_as . You can see an example at: http://www.grocerycr...es/full_example
2. If you mean the auto-increment you don't need to. It's automatically added. Grocery CRUD auto-recognize the primary key and if it's auto-increment. So just make sure that you have your id as primary key and auto-increment and let the rest for grocery CRUD
3. You have to use callback_add_field ( http://www.grocerycr...d_field_example ) and callback_edit_field ( http://www.grocerycr...t_field_example ) and you can return a select. For example:

return "<select name='plan_id' id='' class='chosen-select' data-placeholder='Select Plan' style='width:300px'>
<option value=''></option><option value='1'  >Plan 1</option><option value='2'  >Plan 2</option>
</select>";

Please see all the examples that I have at the website and see what each example does.

Also if you need an API for each function you can go at: http://www.grocerycr...tions_functions

P.S I edited you title as this issue is not an urgent issue. It is just questions of how to use grocery CRUD.
Posted Image

#3 codefreak999

codefreak999

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 06 April 2012 - 11:44 AM

Thank you for your fast respond. :)
Oh sorry for this. I don't know.
:o

2. How do make CRUD auto increment?
3. Fail. this my code
$this->grocery_crud->callback_add_field('country',array($this,'add_field_callback_1'));
function add_field_callback_1()
{
return "<select name='country' id='' class='chosen-select' data-placeholder='Select Plan' style='width:300px'>
<option value=''></option><option value='eng'  >England</option><option value='USA'  >America</option>
<option value='Aus'  >Australia</option>
</select>";
}


#4 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 999 posts
  • LocationLondon

Posted 06 April 2012 - 12:17 PM

2. I mean just a simple auto-increment at you phpmyadmin. For example: auto-increment.png

3. Can you post your function and the table?, probably you have something wrong at somewhere else.

When I say the table I mean only the structure for example:

CREATE TABLE IF NOT EXISTS `actor` (
  `actor_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `fullname` varchar(250) NOT NULL,
  `last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`actor_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=201 ;

Posted Image

#5 codefreak999

codefreak999

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 06 April 2012 - 01:23 PM

2. but. I have accident before, if i delete some data [ex: 1550-1800] and the data will NULL value. I want to 2001 if there is no NULL data and if any NULL data (for example in 1551) and then system Grocery will auto create the input data in ID 1551.

3. This is my code and tabel
function country()
{
   $crud = new grocery_CRUD();
   $crud->set_theme('flexigrid');
   $crud->set_table('country');
   $crud->fields('id_country','country','mark_country');
  
   $this->grocery_crud->callback_add_field('mark_country',array($this,'add_field_callback_1'));
  
   $crud->required_fields('id_country','country','mark_country'); //mark_country=[eng||usa||aus]
  
   $output = $crud->render();
   $this->_example_output($output);
}
function add_field_callback_1()
{
return "<select name='country' id='' class='chosen-select' data-placeholder='Select Plan' style='width:300px'>
<option value=''></option><option value='ENG'  >England</option><option value='USA'  >America</option>
<option value='AUS'  >Australia</option>
</select>";
}
id_country   country	mark_country
1		 England	 ENG
2		 America	 USA
3		 Australia    AUS

Thank you very much in advance forJohnny. :)

#6 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 999 posts
  • LocationLondon

Posted 06 April 2012 - 01:35 PM

2. I understand what you are saying but this is a bad SQL technic . I don't understand why to do this? Is there any important reason to do it? Grocery CRUD doesn't support so complicated and unique customizations so you will have to change the core library of grocery CRUD to do this.
3. Is because you have:

//This is wrong
$this->grocery_crud->callback_add_field('mark_country',array($this,'add_field_callback_1'));

and not:

//This is correct
$crud->callback_add_field('mark_country',array($this,'add_field_callback_1'));

You have to choose everytime what object you will use. You can have $this->grocery_crud OR $crud not both in one function.
Posted Image

#7 codefreak999

codefreak999

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 06 April 2012 - 02:05 PM

3. catch it... Thank you very much. Sory for this because I'm very newbie for Grocery. :rolleyes:

2. Hmm, i've a dateline project. Would you help me to do this.

Grocery CRUD doesn't support so complicated and unique customizations so you will have to change the core library of grocery CRUD to do this.


Thank you Johnny.

#8 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 999 posts
  • LocationLondon

Posted 06 April 2012 - 02:27 PM

Just explain to me why you want to do this ?
The auto-increment is just a simple technic that you just add NULL to the id!! Nothing more!

Ok if you desperately want to do this for your reasons, you can do this without changing the core by simply using the callback_insert method.

I have an example of how to use the callback_before_insert at: http://www.grocerycr..._insert_example . It works with exactly same way but it just don't have any automated insert so use it rarely. The only difference is that you have to add your own logic there and return true or false. For example:


function example_callback_insert(){

	$crud = new grocery_CRUD();

	$crud->set_table('offices');
	$crud->set_subject('Office');
	$crud->required_fields('city');
	$crud->columns('city','country','phone','addressLine1','postalCode');
	$crud->callback_insert(array($this,'checking_post_code'));

	$output = $crud->render();

	$this->_example_output($output);
}

function checking_post_code($post_array)
{
    /* Your own logic here */
    if.... for($i ....)
    $this->db->...
    /* ------------------------- */
    $post_array['country_id'] = $my_new_id;
    $this->db->insert('my_table', $post_array);
   
    return true;
}

Please try to do the logic by your own, as I prefer to answer more questions from other users to the forum than to try to solve every problem separately. I am sorry I just don't have enough time for this.
Posted Image

#9 codefreak999

codefreak999

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 06 April 2012 - 02:45 PM

Ok.
i'll get it ..

back of my question again:
when i use optional value when add or edit data. [i've write and use edit data for mark country] the field say "mark_country field is required".
function edit_field_callback_1($value, $primary_key)
{
return "<select name='country' id='' class='chosen-select' data-placeholder='Select Plan' style='width:300px'>
<option value='".$value."'>".$value."</option><option value='ENG'  >England</option><option value='USA'  >America</option>
<option value='AUS'  >Australia</option>
</select>";
}

function add_field_callback_1()
{
return "<select name='country' id='' class='chosen-select' data-placeholder='Select Plan' style='width:300px'>
<option value=''>"Mark of Country"</option><option value='ENG'  >England</option><option value='USA'  >America</option>
<option value='AUS'  >Australia</option>
</select>";
}

what's wrong

#10 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 999 posts
  • LocationLondon

Posted 07 April 2012 - 11:47 AM

Change:
$crud->required_fields('id_country','country','mark_country');
to
$crud->required_fields('id_country','country');

Posted Image

#11 codefreak999

codefreak999

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 07 April 2012 - 01:11 PM

ok thank you....you're great...

but in addition ; if i insert data that have required field. the field in database were be NULL value.. How solve this?

But i think i found a bug...
if i add data and after that has two choice: edit record and back to menu.
and if i choice 'edit record'

i found this:

Fatal error: Uncaught exception 'Exception' with message 'On the state "edit" the Primary key cannot be null' in /xampp/2251 Stack trace: #0
grocery_CRUD_States->getStateInfo() #1
function]: Admin->dictionary('edit', '0') #3/xampp/system\core\CodeIgniter.php(359): call_user_func_array(Array, Array) #4
xampp/application\libraries\grocery_crud.php on line 2251



#12 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 999 posts
  • LocationLondon

Posted 07 April 2012 - 03:31 PM

Yes I know this. It is just for the reason that you don't have the auto-increment to your ids and it cannot get any insert_id from the $this->db->insert_id. But is still a bug and it will be fixed for the new version. For any news for this bug I will inform you.
Thanks to remind it to me.
Posted Image

#13 codefreak999

codefreak999

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 07 April 2012 - 07:22 PM

Ok. Go for Grocery. :)

How for this:
if i insert data that have not required field. the field in database were be NULL value.. How solve this?

$crud->required_fields('id_country','country');

function edit_field_callback_1($value, $primary_key)
{
return "<select name='country' id='' class='chosen-select' data-placeholder='Select Plan' style='width:300px'>
<option value='".$value."'>".$value."</option><option value='ENG'  >England</option><option value='USA'  >America</option>
<option value='AUS'  >Australia</option>
</select>";
}

function add_field_callback_1()
{
return "<select name='country' id='' class='chosen-select' data-placeholder='Select Plan' style='width:300px'>
<option value=''>"Mark of Country"</option><option value='ENG'  >England</option><option value='USA'  >America</option>
<option value='AUS'  >Australia</option>
</select>";
}

I have insert mark_country but the result in database was NULL. Why?

Thank you

#14 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 999 posts
  • LocationLondon

Posted 07 April 2012 - 07:29 PM

grocery CRUD automatically detects if the field is NULL-ABLE. So if it is and you just give the string '' (empty string) it transform it to NULL. This is an SQL technic to have less storage to the database. I hope you don't want to change this too <_<
Posted Image

#15 codefreak999

codefreak999

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 08 April 2012 - 09:12 AM

ok... catch it.

Thank you John.
See u again.

#16 AaronTraas

AaronTraas

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 12 June 2012 - 10:04 PM

I'm having the same problem, but my field IS auto_increment. Here's the error I'm getting:

On the state "edit" the Primary key cannot be null --- #0 C:\wamp\www\definitions\src\application\libraries\grocery_crud.php(3324): grocery_CRUD_States->getStateInfo() #1 C:\wamp\www\definitions\src\application\controllers\examples.php(46): grocery_CRUD->render() #2 [internal function]: Examples->edit() #3 C:\wamp\www\definitions\src\system\core\CodeIgniter.php(359): call_user_func_array(Array, Array) #4 C:\wamp\www\definitions\src\index.php(202): require_once('C:\wamp\www\def...') #5 {main}


This is the dump from MySQL:

--
-- Table structure for table `definitions`
--

CREATE TABLE IF NOT EXISTS `definitions` (
`definition_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`term` varchar(64) NOT NULL,
`definition` text NOT NULL,
PRIMARY KEY (`definition_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

--
-- Dumping data for table `definitions`
--

INSERT INTO `definitions` (`definition_id`, `term`, `definition`) VALUES
(1, 'start', 'stop');



#17 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 999 posts
  • LocationLondon

Posted 12 June 2012 - 11:24 PM

This problem occurs if you have an id with value 0. So go to your phpmyadmin and just edit the first definition_id from 0 to 3 (or whatever else but not 0) and it will work
Posted Image

#18 shum

shum

    Member

  • Members
  • PipPip
  • 10 posts
  • LocationRussia, Moscow

Posted 28 August 2012 - 11:25 AM

Hello!

3 This works:
    function places_of_study()
	{
		$crud = new grocery_CRUD();

		/**/
		$crud->callback_column('status',array($this,'status_column'));
		$crud->callback_field('status',array($this,'status_choice'));
	  
	   /**/
	}
  
	public $status_choice = array("arh" => "arhive", "tst" => "test", "loc" => "local");
  
	function status_choice($value, $pk)
	{
		$_status_field = $this->status_choice;
		$str = "<select name='status' id='' class='chosen-select' data-placeholder='Select status' style='width:300px'>";
	  
		if(isset($_status_field[$value])) {
			$str = $str."<option value='$value' select>$_status_field[$value]</option>";
		} else {
			$str = $str."<option value='$value' select>$value</option>";
		}
	  
		foreach( $_status_field as $key => $val){
			if ($key != $value) {
				$str = $str."<option value='$key'>$val</option>";
			} else {
				continue;
			}
		}
		$str = $str."</select>";
		return $str;
	}

	function status_column($value, $arr)
	{
		$_status_arr = $this->status_choice;
		if (isset($_status_arr["$value"])) {
			return $_status_arr["$value"];
		}
		return $value;
	}

I do not speak English, sorry :(


#19 shum

shum

    Member

  • Members
  • PipPip
  • 10 posts
  • LocationRussia, Moscow

Posted 30 August 2012 - 11:57 AM

web-johnny, its good?

I do not speak English, sorry :(





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users