⚠ 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

How to pass data using crud?



fantas

fantas
  • profile picture
  • Member

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 :(??


Thanks thanks thanks!!

web-johnny

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

Posted 16 January 2012 - 07:20 AM

[quote name='fantas' timestamp='1326686305' post='292']
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 :(??


Thanks thanks thanks!!
[/quote]

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

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

[b]2.[/b] 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);

[b]3. [/b]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);

carlosT

carlosT
  • profile picture
  • Member

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 ?>

carlosT

carlosT
  • profile picture
  • Member

Posted 16 January 2012 - 07:23 AM

Haha we answered at the same time :)

winni

winni
  • profile picture
  • Member

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.com/UI/Autocomplete#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

web-johnny

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

Posted 24 January 2012 - 19:28 PM

[quote name='winni' timestamp='1327398006' post='336']
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
[/quote]

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?

winni

winni
  • profile picture
  • Member

Posted 24 January 2012 - 22: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

web-johnny

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

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.

winni

winni
  • profile picture
  • Member

Posted 25 January 2012 - 22: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

Mohammed Zayan

Mohammed Zayan
  • profile picture
  • Member

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)
[img]http://im27.gulfup.com/2012-06-23/134043862061.png[/img]
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?

udayasri

udayasri
  • profile picture
  • Member

Posted 18 July 2012 - 09:14 AM

[quote name='web-johnny' timestamp='1326698423' post='294']
That's easy. ActuallyI really didn't understand exactly what you are looking for I will give you 3 solutions.
[b]1.[/b] You can simply add your data like this:

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

[b]2.[/b] 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);

[b]3. [/b]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);

[/quote]


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

ganbca2003

ganbca2003
  • profile picture
  • Member

Posted 10 September 2012 - 06:51 AM

[quote name='udayasri' timestamp='1342602862' post='2756']
Thanks a lot ... I have search this for several pages ... It works for me
[/quote]

Hi,

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


[color=#000000]$output [/color][color=#666600]=[/color][color=#000000] $crud[/color][color=#666600]->[/color][color=#000000]render[/color][color=#666600]();[/color]
[color=#000000]$output[/color][color=#666600]->[/color][color=#000000]title [/color][color=#666600]=[/color][color=#000000] [/color][color=#008800]"Something"[/color][color=#666600];[/color]
[color=#000000]$this[/color][color=#666600]->[/color][color=#000000]load[/color][color=#666600]->[/color][color=#000000]view[/color][color=#666600]([/color][color=#008800]'view'[/color][color=#666600],[/color][color=#000000] $output[/color][color=#666600]);[/color]

in my view page

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

Its shows error message :-(

ganbca2003

ganbca2003
  • profile picture
  • Member

Posted 10 September 2012 - 06:54 AM

[quote name='ganbca2003' timestamp='1347259896' post='3334']
Hi,

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


[color=#000000]$output [/color][color=#666600]=[/color][color=#000000] $crud[/color][color=#666600]->[/color][color=#000000]render[/color][color=#666600]();[/color]
[color=#000000]$output[/color][color=#666600]->[/color][color=#000000]title [/color][color=#666600]=[/color][color=#000000] [/color][color=#008800]"Something"[/color][color=#666600];[/color]
[color=#000000]$this[/color][color=#666600]->[/color][color=#000000]load[/color][color=#666600]->[/color][color=#000000]view[/color][color=#666600]([/color][color=#008800]'view'[/color][color=#666600],[/color][color=#000000] $output[/color][color=#666600]);[/color]

in my view page

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

Its shows error message :-(
[/quote]

Sorry

I found solution just include the following code

<?php echo $title;?>

Thanx....