tags:

views:

47

answers:

1

I have id names with the second part of table name plus id.

For example 

admins_id in omc_admins table, 

customers_id in omc_customers table, 

products_id in omc_products table, 

categories_id in omc_categories table etc.

Now the following function code is supposed to find orphans. For example, when I delete categories, it will check the products and find orphans of that category.

Now I am not sure how to manipulate parameter according to other parameter. For example 'id' in $this->db->select('id,name');.

id will be changing according to the $db_table.

function checkOrphans($segment, $id, $db_table){
$data = array();
$this->db->select('id,name');
$this->db->where($id,id_clean($segment));
$Q = $this->db->get($db_table);
if ($Q->num_rows() > 0){
   foreach ($Q->result_array() as $row){
     $data[$row['id']] = $row['name'];
   }
}
$Q->free_result();  
return $data;   

}

I think I can use $db_table. For example $db_table is omc_categories, I can use categories part and add to id.

categories_id.

Can anyone tell me how to do it please?

I am using codeigniter, but question is purely php.

+1  A: 

You can get the id name in this way:

$id_name = preg_replace('/.*_(.*)/', '${1}_id', $db_table);

then

$this->db->select($id_name . ',name');
kemp
I want to select id and name as well.
shin
Edited my answer
kemp