⚠ 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

One is to many relation



DREON
  • profile picture
  • Member

Posted 25 February 2013 - 07:58 AM

i have a question i dont if it is a one to many relation. i ahev 2 tables on my database.

 

faculty

->id

-lastname

->firstname

->middlename

 

subject

->id

->id1 ( relation to faculty table)

->sugbjectcode

->subject

 

this is my question, i want to create a grocery crud table on my faculty and  i want to add action button called view

 

like this example:

 

                      first name |     last name |        middle name|    action

                       dreon              dreaon                d                    view |  edit   | delit

 

and when i click that view button will bring me to the page with the relation on subject table

 

                               dreon, dreaon d.

 

                   subject code |        subject

                     math123                math

                      english123            english

                      scince123              science

 

 

please help me..

 

 

 

 

 

 


victor
  • profile picture
  • Member

Posted 25 February 2013 - 09:40 AM

you can use the 'where' function in the action function.
for example $crud-> where ('d1',$user_id)

DREON
  • profile picture
  • Member

Posted 25 February 2013 - 13:42 PM

sir can u give sample code  ? thank you..


victor
  • profile picture
  • Member

Posted 26 February 2013 - 07:59 AM

create the action function for example "subjects_list".
then:
......
crud->set_table(subject)
$crud-> where ('d1',$user_id)
....
create action in "faculty"

DREON
  • profile picture
  • Member

Posted 27 February 2013 - 00:38 AM

sorry sir im new to codeigniter and GC this is my code

 

public function faculty()
    
    {
    
        $crud = new grocery_CRUD();
 
        $crud->set_theme('datatables');
        $crud->set_table('faculty');
        $crud->set_subject('faculty');
    
        
        
        $output = $crud->render();
 
    
        $this->load->view('faculty.php',$output);    
        
    }

 

on the above code output is my list of my faculty, i want to add action button when i click one of my faculty it show me another page with the list of there subject.. please nedd help sir..


victor
  • profile picture
  • Member

Posted 27 February 2013 - 06:38 AM

you should use 'add_action' function. read more info here: www.grocerycrud.com/documentation/options_functions/add_action.

DREON
  • profile picture
  • Member

Posted 27 February 2013 - 07:24 AM

thank you sir vic.. i put action button on my faculty view.. but when i click that action button it will redirect me to subject view, but the bad things is no subject appear on my subject table view.

 

public function edit_faculty_name()
    
    {
    
        $crud = new grocery_CRUD();
 
        $crud->set_theme('datatables');
        $crud->set_table('faculty_cmt');
        $crud->set_subject('Faculty Name');
        
        $crud->columns('faculty_lname','faculty_fname','schoolcollege');
        
        $crud->display_as('faculty_lname','Last Name');
        $crud->display_as('faculty_fname','First Name');
        $crud->display_as('schoolcollege','School/College');
                
         $crud->add_action('View', '', 'main/subject','ui-icon-plus');
        
        
        
        $output = $crud->render();
 
    
         $this->_example_output($output);
        
    }

 

and this is my subject view

 

public function subject()
    
    {
    
        $crud = new grocery_CRUD();
 
        $crud->set_theme('datatables');
        $crud->set_table('faculty_sched');
        $crud->set_subject('CMT');
    
        $crud-> where('id1','id1');
        

        
        $output = $crud->render();
 
    
       public function subject()
    
    {
    
        $crud = new grocery_CRUD();
 
        $crud->set_theme('datatables');
        $crud->set_table('faculty_sched');
        $crud->set_subject('CMT');
    
        $crud-> where('id1','id1');   please help with this
        

        
        $output = $crud->render();
 
    
       $this->_example_output($output);  
        
    }
 
        
    }


davidoster
  • profile picture
  • Member

Posted 03 March 2013 - 08:34 AM

First of all, I hope now to have your function subject within another subject function!!! So correct this.

Then your add_action is not complete, so you don't get all the parameters in order to use the $row, $value and call your where function.

 

So I would change it like this,

 

 public function subject($primary_key, $row)
 {
    
        $crud = new grocery_CRUD();
 
        $crud->set_theme('datatables');
        $crud->set_table('faculty_sched');
        $crud->set_subject('CMT');
    
        $crud-> where('id1',$primary_key);  // primary_key has the id from the faculty_cmt table
        

        
        $output = $crud->render();
 
    
        $this->_example_output($output);  
        
}

 

If your tables / fields are properly set, the above code will give you what you want.


DREON
  • profile picture
  • Member

Posted 03 March 2013 - 10:51 AM

First of all, I hope now to have your function subject within another subject function!!! So correct this.

Then your add_action is not complete, so you don't get all the parameters in order to use the $row, $value and call your where function.

 

So I would change it like this,

 

 public function subject($primary_key, $row)
 {
    
        $crud = new grocery_CRUD();
 
        $crud->set_theme('datatables');
        $crud->set_table('faculty_sched');
        $crud->set_subject('CMT');
    
        $crud-> where('id1',$primary_key);  // primary_key has the id from the faculty_cmt table
        

        
        $output = $crud->render();
 
    
        $this->_example_output($output);  
        
}

 

If your tables / fields are properly set, the above code will give you what you want.

thank you david big help.. now my subject table show, but i have php error

A PHP Error was encountered
 

Severity: Warning


Message: Missing argument 2 for Examples::subject()

Filename: controllers/examples.php

 

but i try to delete $row on my public function subject($primary_key, $row) now its working.. thnak you so much..


DREON
  • profile picture
  • Member

Posted 03 March 2013 - 10:57 AM

sir david now my problem is how can i add subject without typing there id1(belong to my faculty)...


DREON
  • profile picture
  • Member

Posted 03 March 2013 - 11:45 AM

how to get this view on my subject view..

 

                                                                         the name of the faculty(should be outside the table)

 

___________________________________________________________________________

                                                        

 

                                    there subject(grocery crud table)

 

 

________________________________________________________________________


victor
  • profile picture
  • Member

Posted 03 March 2013 - 11:55 AM

there is a simple solution:
.......
$data = $this->db->get_where('faculty',array('id'=>$id))->row_array();
$output = $crud->render();
$output->output = '<div>'.$data['firstname'].'</div>'.$output->output;


DREON
  • profile picture
  • Member

Posted 03 March 2013 - 22:54 PM

sir victor thank you for replay but i got error.. this is my code..

 

public function subject($primary_key)
 {
    
        $crud = new grocery_CRUD();
 
        $crud->set_theme('datatables');
        $crud->set_table('faculty_sched');
        $crud->set_subject('CMT');
    
        $crud-> where('id1',$primary_key);  // primary_key has the id from the faculty_cmt table
        
      $data = $this->db->get_where('faculty_cmt',array('id'=>$id))->row_array();
    $output = $crud->render();
    $output->output = '<div>'.$data['faculty_lname'].'</div>'.$output->output;
    
   
        $this->_example_output($output);  
        
}

 

and this is my error

 



A PHP Error was encountered

Severity: Notice


Message: Undefined variable: id


Filename: controllers/main.php


Line Number: 252


 


A PHP Error was encountered

Severity: Notice


Message: Undefined index: faculty_lname


Filename: controllers/main.php


Line Number: 254


 

DREON
  • profile picture
  • Member

Posted 03 March 2013 - 23:08 PM

solve already sir victor.. ^^ thnx

 

$data = $this->db->get_where('faculty_cmt',array('id'=>$primary_key))->row_array();


DREON
  • profile picture
  • Member

Posted 03 March 2013 - 23:09 PM

my question now is when im in the subject page of one of my faculty how can i add subject without incoding the faculty id..


victor
  • profile picture
  • Member

Posted 03 March 2013 - 23:28 PM

Sorry, I don't understand what do you want (I'm about : "without incoding the faculty id..")? 


DREON
  • profile picture
  • Member

Posted 04 March 2013 - 13:30 PM

what i mean sir victor is on my subject page on adding subject that belongs to faculty..

 

this is my sample database,

faculty


->id


-lastname


->firstname


->middlename


 


subject


->id


->id1 ( relation to faculty table) How cn i add subject without encoding this on add.


->sugbjectcode


->subject

 

this is my code on my subject page.

public function subject($primary_key, $row)
{

$crud = new grocery_CRUD();

$crud->set_theme('datatables');
$crud->set_table('faculty_sched');
$crud->set_subject('CMT');

$crud-> where('id1',$primary_key); // primary_key has the id from the faculty_cmt table



$output = $crud->render();


$this->_example_output($output);

}


php_lover
  • profile picture
  • Member

Posted 11 March 2013 - 08:33 AM

i only say one thing VICTOR MY HERO