Get db field value to gc list data


i am trying to get value from a db field and add it to set_upload_path
eg. assets/upload/files/."$my_folder"

how can i get the value inside grocery_Crud controller where is crud = new Grocery_crud() object.

i have a problem which is that I can’t build the file URL on the list when the folder where I save the files, changed eg.
we have 2021 now , when it changed to 2022 i can’t see the files with path + 2021

I hope you understand what I mean.

Thank you…

Can you please post what you tried so far (code)…
Your post is not very clear…

1 Like

hello panosv ,

I have made a function

public function doc_inc() {

$crud = new grocery_CRUD();


/* resend document */
$crud->add_action(’’,’’,‘main/resendmail’,‘fa fa-envelope-o fa-2x’,’’);

	$this->db->where('id', 239);
	$q = $this->db->get('docs');
	$row = $q->row();*/

	$path = '/assets/uploads/files/'.**date('Y')**;
	if (!is_dir($path)) {


//$crud->add_action(‘Project_Details’, ‘’, ‘’,‘ui-icon-plus’,array($this,‘get_id’));
//$crud->add_action(‘Project_Details’, ‘’, ‘examples/proj_details’,‘ui-icon-plus’);

	$output = $crud->render();
	if ( $this->session->userdata('is_authenticated') ){
	}else {



so code with bold is where i have a problem. I have a list with data and a field to upload a file.

as you can see i have define the function set_field_upload(‘file_url’,‘assets/uploads/files/’.date(‘Y’));

I use date(‘Y’) to set the name of the folder where I have uploaded the file. the problem is that when the year will be changed (2022) I would lose the link to the files inside the 2021 folder.


I have created a column inside the database named folder_file eg. 2021

so how can I get the folder_file and set it for each record ?

Hello @cbabekos ,

I think the best solution is to store the info about the year that this photo was updated on the database. You can keep the date('Y') only for the insert, for example:

if ($crud->getState() === 'add') {
} else if ($crud->getState() === 'edit') {
        // Take the information that you need from $crud->getStateInfo();
        set_field_upload(‘file_url’,‘assets/uploads/files/’ . $photoYear);
1 Like

how can i explain it better!

we have a list of records ok ?
between the database fields we have a file_url field where we can save file like a pdf file. clear so far ?
now . I define the function ```
set_field_upload(‘file_url’,‘assets/uploads/files/’.date(‘Y’)); // location for upload file and date(‘Y’) is a value for a folder i create before to save the files , depending on year! eg 2021 folder

my problem is when the year changes. I will lose the link with the file in the list because date() get the current year, so the name of folder changes.
so how can I get a value from the database where I have inserted and replace the date(‘Y’) with it !

I think the set_field_upload works serially . it creates the link between file upload and the path of each

ok now?

If I understand you well, you should save the folder name for each upload file, as a field in your database.

Create a new column in the database, call it ‘file_folder’ for example.

When you are saving the record, save the date(‘Y’) i.e. 2021 in that record.
And keep the record hidden in grocerycrud.

Then, use that value in the ‘set_field_upload’ instead of date(‘Y’)

Once I get home, I’ll try an example and send you…

1 Like

This function is used to set the path for the upload file. I build the path ‘assets/uploads/files/’.date(‘Y’)

I could replace date(‘Y’) with text ‘2021’ . I did that thinking when the year changed , I will create the new folder for the new year but that’s the problem . when I create the new folder with the new year 2022 I lose the connection with files that are saved to folder 2021.

The function set_field_upload creates a link assets/uploads/files/2022 and previous files have saved to 2021 folder. So I have lost connection with previous files because the record has this url assets/uploads/files/2022…this func creates all links on the list with this path …

What I am trying to say though is that you don’t need to lose the folder if this is stored in the database as a row for every entry really :slight_smile:

For example you can use: callback_after_insert to insert in the row to a field the upload_year and use it this way:

if ($crud->getState() === 'add') {
} else if ($crud->getState() === 'edit') {
        // Take the information that you need from $crud->getStateInfo();
        // and then have a query for that entry. The result will be something like that:
        // $photoYear = $row->upload_year;
        set_field_upload(‘file_url’,‘assets/uploads/files/’ . $photoYear);
1 Like

I don’t want to get the folder_year on edit

I want to get it on the list and put it on each link
set_field_upload(‘file_url’,‘assets/uploads/files/’ . $photoYear);

Then you can use the callback_column and do not use the set_field_upload on the list state. For example something like this:

if ($crud->getState() === 'list' || $crud->getState() === 'ajax_list') {
	 $crud->callback_column('file_url', function () {
			return "<a href='/assets/uploads/files/{$row->upload_year}/$value'>$value</a>";	 
} else if ($crud->getState() === 'add') {
} else if ($crud->getState() === 'edit') {
        // Take the information that you need from $crud->getStateInfo();
        // and then have a query for that entry. The result will be something like that:
        // $photoYear = $row->upload_year;
        set_field_upload('file_url','assets/uploads/files/' . $photoYear);
1 Like

This is what I was trying to say…

In all cases, if you see it much complicated, keep all your files in a single upload directory, and add the year to the file name…

Example :


This way, all your files will be in a single directory

1 Like
$state = $crud->getState();
$state_info = $crud->getStateInfo();	
if ( $state === 'list'){
				$primary_key = $state_info->primary_key;

i wrote this but i get this error !
Severity: Notice

Message: Undefined property: stdClass::$primary_key

Filename: controllers/Documents.php

Line Number: 494


File: /var/webs/wwweprotocol/www/application/controllers/Documents.php
Line: 494
Function: _error_handler

File: /var/webs/wwweprotocol/www/index.php
Line: 315
Function: require_once

I guess there is no primary key in list state …
It’s in edit state

What are you trying to do?

I try to get the primary key on list mode and get from each row the field folder (eg. 2021)
and define the $folder


Thank you for you help !!

i define this function inside controller

and create this

public function _callback_upload_field($value, $row)
return “<a href=’”.site_url(‘assets/uploads/files/’.$row->folder_file)."/".$row->file_url."’ target=’_blank’>$row->file_url";

ok now !

Thank you !!!

Am glad you were able to fix that…
GroceryCrud is very simple to use once you get used to its structure…

Good luck.

1 Like

Indeed! it is the best framework ! I am going to buy the enterprise edition later which is built with Zend framework !

It was so easy thinking the callback_column ! I have used this function elsewhere!

can I ask another issue with set_field_upload('field', 'path file upload', 'set file types')

I see that inside grocery crud lib there is another argument for this function that defines what file types allowed.
I tried to set a file type(e.g pdf) but is not working.

@cbabekos ,
Please, always include the code that you used… there might be a bug in your code…
That way, someone can help you faster…


I found this on git

and as you can see there is a reference to set_field_upload( 1 argue, 2 argue, there is a 3rd argument)
if you will see inside lib/grocery_crud there is this :

header('Access-Control-Allow-Headers: X-File-Name, X-File-Type, X-File-Size');

			$allowed_files = $this->config->file_upload_allow_file_types;

	                $reg_exp = '';
	                    $reg_exp = '/(\\.|\\/)('.$upload_info->allowed_file_types.')$/i';
	                    $reg_exp = '/(\\.|\\/)('.$allowed_files.')$/i';

			$max_file_size_ui = $this->config->file_upload_max_file_size;
			$max_file_size_bytes = $this->_convert_bytes_ui_to_bytes($max_file_size_ui);

			$options = array(
				'upload_dir' 		=> $upload_info->upload_path.'/',
				'param_name'		=> $this->_unique_field_name($state_info->field_name),
				'upload_url'		=> base_url().$upload_info->upload_path.'/',
				'accept_file_types' => $reg_exp,
				'max_file_size'		=> $max_file_size_bytes

and …

public function set_field_upload($field_name, $upload_dir = ‘’, $allowed_file_types = ‘’)
$upload_dir = !empty($upload_dir) && substr($upload_dir,-1,1) == ‘/’
? substr($upload_dir,0,-1)
: $upload_dir;
$upload_dir = !empty($upload_dir) ? $upload_dir : ‘assets/uploads/files’;

  /** Check if the upload Url folder exists. If not then throw an exception **/
  if (!is_dir(FCPATH.$upload_dir)) {
  	throw new Exception("It seems that the folder \"".FCPATH.$upload_dir."\" for the field name
  			\"".$field_name."\" doesn't exists. Please create the folder and try again.");

  $this->upload_fields[$field_name] = (object) array(
  		'field_name' => $field_name,
  		'upload_path' => $upload_dir,
  		'allowed_file_types' => $allowed_file_types,
  		'encrypted_field_name' => $this->_unique_field_name($field_name));
  return $this;


the code where i found in the GIT has been written from John Scoumbourdis and reports that allowed file types / per field with set_field_upload now!

I tried to set file type but not working.

i think the above code bypassed from general definition of this :

any ideas? I want to set individual file types per upload field.