Jump to content


How to pass data using crud?


  • Please log in to reply
12 replies to this topic

#1 fantas

fantas

    Member

  • Members
  • PipPip
  • 12 posts

Posted 16 January 2012 - 03:58 AM

Hello, im just wondering how to pass a extra data to the view for example if my crud code is

$crud = new grocery_CRUD();
$crud->set_table('xxx');
$output = $crud->render();
$this->load->view('view', $output);
____________
I want to add
$data['title'] = "Something";

How i can pass $data into view too <img src='http://www.grocerycrud.com/forums/public/style_emoticons/<#EMO_DIR#>/sad.png' class='bbc_emoticon' alt=':(' />??

Thanks thanks thanks!!

#2 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,155 posts
  • LocationLondon

Posted 16 January 2012 - 07:20 AM

Hello, im just wondering how to pass a extra data to the view for example if my crud code is

$crud = new grocery_CRUD();
$crud->set_table('xxx');
$output = $crud->render();
$this->load->view('view', $output);
____________
I want to add
$data['title'] = "Something";

How i can pass $data into view too <img src='http://www.grocerycrud.com/forums/public/style_emoticons/<#EMO_DIR#>/sad.png' class='bbc_emoticon' alt=':(' />??

Thanks thanks thanks!!


That's easy. ActuallyI really didn't understand exactly what you are looking for I will give you 3 solutions.
1. You can simply add your data like this:
$output = $crud->render();
$output->title = "Something";
$output->meta_keywords = "Something 2";
$this->load->view('view', $output);
2. A simple array_merge will do the job, if you already created the array $data:
$output = $crud->render();
$data['title'] = "Something";
$output = array_merge($data,(array)$output);
$this->load->view('view', $output);
3. With this you will call from the view the $data['title'] (actually I think you don't want that)
$output = $crud->render();
$data['title'] = "Something";
$output->data = $data;
$this->load->view('view', $output);

Posted Image

#3 carlosT

carlosT

    Member

  • Members
  • PipPip
  • 17 posts

Posted 16 January 2012 - 07:22 AM

I am saying this without testing it but try this...

$crud = new grocery_CRUD();
$crud->set_table('xxx');
$output = $crud->render();
$output->title = 'this is a title';
$this->load->view('view', $output);

in your view use...

<?php echo $output->title ?>

#4 carlosT

carlosT

    Member

  • Members
  • PipPip
  • 17 posts

Posted 16 January 2012 - 07:23 AM

Haha we answered at the same time :)

#5 winni

winni

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 24 January 2012 - 09:40 AM

Hi,

first a big thanks for this great piece of software!
I have quite the same issue, but I want to pass an array of strings to a view.
Background: I want to use the jquery autocomplete feature of text fields in the add/edit form for one field. Therefore I collect the data from the datebase ("SELECT DISTINCT(x) FROM y") and fill the values like here:
http://docs.jquery.c...complete#source
This works fine, but currently I do this in the view, which imho is quite bad, since I want the database access to happen in the controller.
I'm quite new to grocery/codeignition and PHP at all, so please don't mind if this has an obvious solution ^_^

cheers,
Christian

#6 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,155 posts
  • LocationLondon

Posted 24 January 2012 - 07:28 PM

Hi,

first a big thanks for this great piece of software!
I have quite the same issue, but I want to pass an array of strings to a view.
Background: I want to use the jquery autocomplete feature of text fields in the add/edit form for one field. Therefore I collect the data from the datebase ("SELECT DISTINCT(x) FROM y") and fill the values like here:
http://docs.jquery.c...complete#source
This works fine, but currently I do this in the view, which imho is quite bad, since I want the database access to happen in the controller.
I'm quite new to grocery/codeignition and PHP at all, so please don't mind if this has an obvious solution ^_^

cheers,
Christian


Can you have an example of your controller-functions and the view? I don't understand really what do you want to do. Did you use any callback for this?
Posted Image

#7 winni

winni

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 24 January 2012 - 10:02 PM

Hi,

thanks for your reply. What I want to do is: Using the autocomplete feature of input boxes in the add/edit form.
The values for autocompletion should be read from the database. My current code looks like this:

controller:
	public function example()
	{
		$this->grocery_crud->set_table('mytable');
		$this->grocery_crud->callback_add_field('myfield',array($this,'field_myfield'));
		$output = $this->grocery_crud->render();
		$this->_example_output($output);
	}
	// callback function to set the id
	function field_myfiield()
	{
		return '<input name="myfileld" id="myfield" type="text" value=""/>';
	}


view:
...
  <script>
  $(document).ready(function() {

	$("input#myfield").autocomplete({
	source: [
<?php
	$query = $this->db->query("SELECT DISTINCT(myfield) FROM mytable");
	foreach ($query->result_array() as $row)
	{
	   echo '"' . $row['myfield'] . '",';
	}
?>
		]
});
  });
   </script>

</head>
<body>
...


So my question was: How to move the database call from to view to the controller?

cheers,
Christian

#8 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,155 posts
  • LocationLondon

Posted 25 January 2012 - 06:23 AM

You can easily do this kind of thing:
		public function example()
		{
				$this->grocery_crud->set_table('mytable');
				$this->grocery_crud->callback_add_field('myfield',array($this,'field_myfield'));
				$output = $this->grocery_crud->render();
  
	$results = $this->db->query("SELECT DISTINCT(myfield) FROM mytable")->result_array();
	$output->output .= $this->load->view("autocomplete_script.php",array('results' => $results),true); //If the last segment is true , returns the view as a string
  
				$this->_example_output($output);
		}

This is a quite quick solution of what you said. The normal way is to do it with views:

$this->load->view('header.php');
$this->load->view('content.php');
$this->load->view('crud.php');
....
$this->load->view('footer.php');

or to use a template control.
But those are basic codeigniter features, so after this I think you have to find your answers to codeigniter forum.
You need to read examples and be more familiar with codeigniter to do this. You can read this post http://codeigniter.c...wthread/125687/ of codeigniter that I thing is very good for beginners (except the csrf_protection that grocery CRUD doesn't support yet!) or if you want to don't start from the scratch and create your brand new CMS you can also see: http://codeigniter.c...wthread/209171/ This is a brand new idea to not create a CMS from scratch but by having many required libraries built in codeigniter , such as :
-HMVC, to make fully modular separation
-Phil Sturgeon’s Template, to make customizable themes
-groceryCRUD, to build CRUD application in a minute.
-Authentication & Authorization by using group, privilege, and user management. Not like other CMS, there is no backend-frontend in No-CMS. You have freedom to choose how different groups of users can access pages and modules differently.
Posted Image

#9 winni

winni

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 25 January 2012 - 10:02 PM

Hi again,

$output->output .= $this->load->view("autocomplete_script.php",array('results' => $results),true); //If the last segment is true , returns the view as a string

many thanks! works like a charm.

cheers,
Christian

#10 Mohammed Zayan

Mohammed Zayan

    Advanced Member

  • Members
  • PipPipPip
  • 72 posts
  • LocationEgypt

Posted 23 June 2012 - 08:07 AM

I use the second way
$output = $crud->render();
$data['title'] = "Something";
$output = array_merge($data,(array)$output);
$this->load->view('view', $output);

but the view become like this (the datatables theme doesn't appear)
Posted Image
my code is
$crud = new grocery_CRUD();
$crud->set_theme('datatables');
$crud->where($row_where, $q_where);
$crud->set_table('volunteers');
$crud->unset_operations();
$crud->columns('profileId', 'name', 'country', 'printDate', 'mail');
$crud->callback_column('name',array($this,'_callback_webpage_url'));
$crud->callback_column('country',array($this,'_callback_languages'));
$crud->display_as('profileId', 'تسلسل')->display_as('name', 'الاسم')->display_as('country', 'البلد')->display_as('printDate', 'تاريخ التسجيل')->display_as('mail', 'البريد الإلكترونى');
$output = $crud->render();
$data['page_title'] = "البحث فى المتطوعين";  
$data['my_view'] = "includes/search_volunteer";  
$data['css_files'] = array();  
$data['js_files'] = array();  
$data['output'] = "";  
$data['allSkills'] = $this->volunteer_model->getAllSkills();
$data['allLanguages'] = $this->volunteer_model->getAllLanguages();
$output = array_merge($data,(array)$output);
$this->_admin_output($output);
What is problem?

#11 udayasri

udayasri

    Member

  • Members
  • PipPip
  • 10 posts

Posted 18 July 2012 - 09:14 AM

That's easy. ActuallyI really didn't understand exactly what you are looking for I will give you 3 solutions.
1. You can simply add your data like this:

$output = $crud->render();
$output->title = "Something";
$output->meta_keywords = "Something 2";
$this->load->view('view', $output);
2. A simple array_merge will do the job, if you already created the array $data:
$output = $crud->render();
$data['title'] = "Something";
$output = array_merge($data,(array)$output);
$this->load->view('view', $output);
3. With this you will call from the view the $data['title'] (actually I think you don't want that)
$output = $crud->render();
$data['title'] = "Something";
$output->data = $data;
$this->load->view('view', $output);



Thanks a lot ... I have search this for several pages ... It works for me

#12 ganbca2003

ganbca2003

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationIndia

Posted 10 September 2012 - 06:51 AM

Thanks a lot ... I have search this for several pages ... It works for me


Hi,

I have the same issue, how to display this title to in my view page. I already try the following code


$output = $crud->render();
$output->title = "Something";
$this->load->view('view', $output);

in my view page

<?php echo $output->title;?>

Its shows error message :-(

#13 ganbca2003

ganbca2003

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationIndia

Posted 10 September 2012 - 06:54 AM

Hi,

I have the same issue, how to display this title to in my view page. I already try the following code


$output = $crud->render();
$output->title = "Something";
$this->load->view('view', $output);

in my view page

<?php echo $output->title;?>

Its shows error message :-(


Sorry

I found solution just include the following code

<?php echo $title;?>

Thanx....




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users