⚠ 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

about row and input data



codefreak999

codefreak999
  • profile picture
  • Member

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:
[list=1]
[*]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)?
[/list]

Thank you for anyone want to help me. :)

web-johnny

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

Posted 06 April 2012 - 10:19 AM

Hello [member='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.

codefreak999

codefreak999
  • profile picture
  • Member

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>";
}

web-johnny

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

Posted 06 April 2012 - 12:17 PM

2. I mean just a simple auto-increment at you phpmyadmin. For example: [attachment=93: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 ;

codefreak999

codefreak999
  • profile picture
  • Member

Posted 06 April 2012 - 13: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. :)

web-johnny

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

Posted 06 April 2012 - 13: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.

codefreak999

codefreak999
  • profile picture
  • Member

Posted 06 April 2012 - 14: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.
[quote]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.[/quote]

Thank you Johnny.

web-johnny

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

Posted 06 April 2012 - 14: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.

codefreak999

codefreak999
  • profile picture
  • Member

Posted 06 April 2012 - 14: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 [color=#444444][font=Arial, Helvetica, sans-serif][size=4]"mark_country field is required".[/size][/font][/color]

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

web-johnny

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

Posted 07 April 2012 - 11:47 AM

Change:

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

to

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

codefreak999

codefreak999
  • profile picture
  • Member

Posted 07 April 2012 - 13: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:
[quote]
[b]Fatal error[/b][color=#000000]: Uncaught exception 'Exception' with message 'On the state "edit" the Primary key cannot be null' in /xampp/[/color][color=#000000]2251 Stack trace: #0 [/color]
[color=#000000]grocery_CRUD_States->getStateInfo() #1[/color]
[color=#000000]function]: Admin->dictionary('edit', '0') #3/xampp/[/color][color=#000000]system\core\CodeIgniter.php(359): call_user_func_array(Array, Array) #4[/color]
[color=#000000]xampp/[/color][b]application\libraries\grocery_crud.php[/b][color=#000000] on line [/color][b]2251[/b]
[/quote]

web-johnny

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

Posted 07 April 2012 - 15: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.

codefreak999

codefreak999
  • profile picture
  • Member

Posted 07 April 2012 - 19: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

web-johnny

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

Posted 07 April 2012 - 19: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 <_<

codefreak999

codefreak999
  • profile picture
  • Member

Posted 08 April 2012 - 09:12 AM

ok... catch it.

Thank you John.
See u again.

AaronTraas

AaronTraas
  • profile picture
  • Member

Posted 12 June 2012 - 22:04 PM

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

[quote]
[color=#4F5155]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}[/color]
[/quote]

This is the dump from MySQL:

[quote][font=courier new,courier,monospace]--[/font]
[font=courier new,courier,monospace]-- Table structure for table `definitions`[/font]
[font=courier new,courier,monospace]--[/font]

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

[font=courier new,courier,monospace]--[/font]
[font=courier new,courier,monospace]-- Dumping data for table `definitions`[/font]
[font=courier new,courier,monospace]--[/font]

[font=courier new,courier,monospace]INSERT INTO `definitions` (`definition_id`, `term`, `definition`) VALUES[/font]
[font=courier new,courier,monospace](1, 'start', 'stop');[/font][/quote]

web-johnny

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

Posted 12 June 2012 - 23: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

shum

shum
  • profile picture
  • Member

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;
}

shum

shum
  • profile picture
  • Member

Posted 30 August 2012 - 11:57 AM

web-johnny, its good?