⚠ 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

Passing variables to NEW FORM



dwdc
  • profile picture
  • Member

Posted 28 February 2014 - 18:58 PM

I have a page where I display BIDS (gc table) for a particular Property. It received a property_id as a hash (phase) via the URI to display related bids for the property. Here is the controller code for the gc table:

try{
     $phash = $this->uri->segment(3);
     $query = $this->db->query('SELECT * FROM properties WHERE phash = "'.$phash.'"');

     $row = $query->row();
     $address_line = $row->property_address.', '.$row->city.', '.$row->state.' '.$row->zip_code;
          
     $crud = new grocery_CRUD();
     $crud->set_primary_key('prop_bids_id','property_bids');
     $crud->where('property_bids.phash', $phash);
     $crud->order_by('maximum_bid','desc');
     $crud->set_table('property_bids');
     $crud->set_subject('Bid');
     $crud->unset_read();
     $crud->required_fields('user_id','maximum_bid');
     $crud->set_relation('user_id','users','{first_name} {last_name}');
     $crud->set_relation('property_item_id','properties','{property_address}, {city}, {state} {zip_code}');
     $crud->display_as('user_id','Investor');
     $crud->display_as('property_item_id','Property');
     $crud->display_as('maximum_bid','Maximum Bid');
     $crud->display_as('bid_instructions','Bid Instructions');
     $crud->display_as('back_up_amount','Backup Amount');
     $crud->columns('user_id','maximum_bid','bid_instructions', 'back_up_amount');
     $crud->fields('property_item_id','user_id','maximum_bid','bid_instructions', 'back_up_amount');
    
     $crud->field_type('property_item_id', 'readonly');
     $crud->field_type('phash', 'invisible', $phash);
     $crud->unset_texteditor('bid_instructions');
   
     $output = $crud->render();
       
     $output->pAddress = $address_line; // passes address to page


 

     $this->manage_bids_output($output);
}catch(Exception $e){
     show_error($e->getMessage().' --- '.$e->getTraceAsString());
}


On the EDIT form, the first line is the "property_item_id" which them displays the Property address as {property_address}, {city}, {state} {zip_code}. This is perfect and keep the user orientated to what property this existing is related to.

BUT...

On the NEW BID FORM, the first line is labeled Property but the value is blank. I know this is because the EDIT was reading data from the row and the NEW has not be created yet.

QUESTION: How can I get the variable "$address_line" as the value of the first line.

Also, I need to include the "property_item_id" and "phash" fields in the ADD form as hidden fields do they insert into my new row.

I can't seem to find the proper combinations to get it all to work.

Any insights would be appreciated. Love the GC framework so far and each hurdle I conquer make me more efficient and wiser in using it. Thanks in advance!


dwdc
  • profile picture
  • Member

Posted 28 February 2014 - 21:17 PM

Additional comments:

I modified my code a bit hoping to stubble upon a solution:


try{
       $query = $this->db->query('SELECT * FROM properties WHERE phash = "'.$phash.'"');
$row = $query->row();
       $address_line = $row->property_address.', '.$row->city.', '.$row->state.' '.$row->zip_code;
       $property_item_id = $row->property_item_id;
       
       $crud = new grocery_CRUD();
       $crud->set_primary_key('prop_bids_id','property_bids');
$crud->where('property_bids.phash', $phash);
$crud->order_by('maximum_bid','desc');
    $crud->set_table('property_bids');
$crud->set_subject('Bid');
$crud->unset_read();
$crud->required_fields('user_id','maximum_bid');
$crud->set_relation('user_id','users','{first_name} {last_name}');
$crud->set_relation('property_item_id','properties','{property_address}, {city}, {state} {zip_code}');
$crud->display_as('user_id','Investor');
$crud->display_as('property_item_id','Property');
$crud->display_as('maximum_bid','Maximum Bid');
$crud->display_as('bid_instructions','Bid Instructions');
$crud->display_as('back_up_amount','Backup Amount');
$crud->columns('user_id','maximum_bid','bid_instructions', 'back_up_amount');
 
$crud->add_fields('user_id','maximum_bid','bid_instructions', 'back_up_amount');
$crud->edit_fields('user_id','maximum_bid','bid_instructions', 'back_up_amount');
 
$crud->field_type('property_item_id', 'hidden', $property_item_id);
$crud->field_type('phash', 'hidden', $phash);
$crud->unset_texteditor('bid_instructions');
 
$crud->callback_after_insert(array($this, 'mb_callback_after_insert'));
 
    $output = $crud->render();
   
    $output->pAddress = $address_line;
 
    $this->manage_bids_output($output);
   
}catch(Exception $e){
show_error($e->getMessage().' --- '.$e->getTraceAsString());
}

 

 

I thought adding lines these would enter a few HIDDEN fields with values in my ADD FORM. The 3 parameter being a VALUE.



$crud->field_type('property_item_id', 'hidden', $property_item_id);
$crud->field_type('phash', 'hidden', $phash);

It does not insert the values and when I INSERT the row.

Maybe I am missing something here?  Little help...


connard
  • profile picture
  • Member

Posted 01 March 2014 - 00:20 AM

Well I'd say you need the callback_add_field() function when you test the getstate() function being "add"...??


dwdc
  • profile picture
  • Member

Posted 01 March 2014 - 15:56 PM

Hmmm...  I tried adding this to my controller code, but these do not seem to be picking up as hidden fields when I do the INSERT:



$crud->callback_add_field('property_item_id',array($this,'add_field_callback_1'));
$crud->callback_add_field('phash',array($this,'add_field_callback_2'));
function add_field_callback_1() 
{    
    return $address_line.'<input type="hidden" value="'.$property_item_id.'" name="property_item_id">'; 
}

function add_field_callback_2()
{
   return '<input type="hidden" value="'.$phash.'" name="phase">';
}

Please advise...


dwdc
  • profile picture
  • Member

Posted 01 March 2014 - 18:37 PM

Still struggling with this. I stumbled upon a POST that explain the use of INVISIBLE fields. I thought that would work for me:
 

try{
     $query = $this->db->query('SELECT * FROM properties WHERE phash = "'.$phash.'"');
     $row = $query->row();
       
     $sdata = array(
        'address_line' => $row->property_address.', '.$row->city.', '.$row->state.' '.$row->zip_code,
        'property_item_id'   => $row->property_item_id,
        'phash' => $phash
     );
     $this->session->set_userdata($sdata);
       
     $crud = new grocery_CRUD();
     $crud->set_primary_key('prop_bids_id','property_bids');
     $crud->where('property_bids.phash', $phash);
     $crud->order_by('maximum_bid','desc');
     $crud->set_table('property_bids');
     $crud->set_subject('Bid');
     $crud->unset_read();
     $crud->required_fields('user_id','maximum_bid');
     $crud->set_relation('user_id','users','{first_name} {last_name}');
     $crud->display_as('user_id','Investor');
     $crud->display_as('maximum_bid','Maximum Bid');
     $crud->display_as('bid_instructions','Bid Instructions');
     $crud->display_as('back_up_amount','Backup Amount');
     $crud->columns('user_id','maximum_bid','bid_instructions', 'back_up_amount');

     $crud->add_fields('property_item_id','phash','user_id','maximum_bid','bid_instructions', 'back_up_amount');
     $crud->edit_fields('user_id','maximum_bid','bid_instructions', 'back_up_amount');

     $crud->unset_texteditor('bid_instructions');

     $crud->change_field_type('phash','invisible');
     $crud->change_field_type('property_item_id','invisible');
     $crud->callback_before_insert(array($this,'insert_hiddens'));

     $output = $crud->render();
   
     $output->pAddress = $this->session->userdata('address_line');

     $this->manage_bids_output($output);
   
}catch(Exception $e){
     show_error($e->getMessage().' --- '.$e->getTraceAsString());
}

Then the callback_before_insert function is:



function insert_hiddens($post_array){
   $post_array['phash'] = $this->session->userdata('phash');
   $post_array['property_item_id'] = $this->session->userdata('property_item_id');
   return $post_array;
}

Can someone tell me why when INSERT that these fields and their values are not populated to my database. I am hoping it is something simple.

Thanks!

 


Amit Shah
  • profile picture
  • Member

Posted 02 March 2014 - 21:22 PM

Well the very1 thing, if you wanted to set the fields hidden, u can use

$crud->field_type  .... to set the field hidden and provide in value for the same

Grocerycrud automatically picks up the values @the time of insert for the same.

 

You used the following code

$crud->change_field_type('phash','invisible');
$crud->change_field_type('property_item_id','invisible');

To my knowledge - GC dose not consider invisible fields as the ones to be stored in. - why it is given in and what is major purpose u may dig in deep inside the forum and u might know the reason for the same.. But if u used hidden with the set values passed - it will automatically pickup this fields and values set .. and store them into the table / row.

$crud->change_field_type('phash','hidden',$this->session->userdata('phash'));    //u can set the values as you desire
$crud->change_field_type('property_item_id','hidden', ,$this->session->userdata('property_item_id'));

This should definately work for you.

As for what you have provided in the code earlier when u tried to set the values the different way (retriving the rows) .. u can debug in .. if you actually got any rows or not. If not - it would not have had provided u any return values for the same.. and it would have been blank ... else your earlier code should surely work for you...

Do a debuging as is it really retriving the desired value..

You can verify if the forms hidden values are being set or not by checking in the source code for the hidden fields... if it dose not - means it was not able to retrieve the row / desired value from the query and hence it needs to be debugged / fixed in.

 

Hope to see you have good solution..

 

Happy GCing:)