⚠ 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

Want to sql my own table in a callback function - how?



Hoergold2
  • profile picture
  • Member

Posted 19 September 2012 - 15:05 PM

Hi there,

I found this CRUD tool it is awsome, great, superb - what everyone waited for some years and someone finally did it! Thanky!

As I am quite acquainted with PHP but totally new to Codeigniter (that is needed by GroceryCRUD I do not get the idea how I can query the databse (using Codigniter-Helpers in a Callback function. What I want to do is to check a (German) postal code that is typed in in a form and lookup the state where it is located. The needed data sits in an own table.

What I tried:

$crud->callback_before_insert(array($this,'plz_code'));


and then:


function plz_code($post_array)
{

$plz = $post_array['plz']; // $plz = $this->db->escape_str($post_array['plz']);
$ort = $post_array['ort']; // $ort = $this->db->escape_str($post_array[ort]);

$query = $this->db->query("SELECT plz, ort, land from orte WHERE PLZ = $plz and ort = $ort ");
if (count($query->row_array() > 0 )) {
$result = $query->row_array();
$plz_code = $result['Land'];
} else {
$plz_code = 'XXX';
}

$post_array['b_land'] = $plz_code;
return $post_array;
}


I hope - though it is more a Codeigniter problem (that I am totally new with) - you might help with some lines? I would appreciate it very much!

Thanks in advance,
Martin

victor
  • profile picture
  • Member

Posted 21 September 2012 - 10:06 AM

HI!

function plz_code($post_array)
{
$search_criteria = array('PLZ' => $post_array['plz'],'ort'=>$post_array['ort']);
$result = $this->db->get_where('orte', $search_criteria)->row_array();
$post_array['b_land'] = $result['Land']?$result['Land']:'XXX';
return $post_array;
}

José Sánchez Córdoba
  • profile picture
  • Member

Posted 21 September 2012 - 10:21 AM

For good code. The functions for db insert in MODEL best that in CONTROLLER. Sorry my english. Good day.