0

I am using CodeIgniter and want to remove an item from object in a loop

foreach($query->result() as $row){
     if($row->parent_id == 1){
         // code here to delete current record
    }
}

I want to use foreach to loop all record. In case current item in loop meet condition in if, delete current row in $query object.

Can I delete current record in $query object in CodeIgniter Or Have others way to do that?

MagePal Extensions
  • 17,646
  • 2
  • 47
  • 62
KH-DP
  • 43
  • 2
  • 8

2 Answers2

4

To Removing an item from object in a loop

$query = $this->db->query("YOUR QUERY");
$rows = $query->result();
foreach ($rows as $key => $row)
{
  if($row->parent_id == 1){
      unset($rows[$key]);
     // code here to delete current record
  }
}

print_r($rows) // remove row with parent_id = 1

See more @ Removing an item from object in a loop

You could also change your select statement to get all records where parent_id not eq 1 (or other 'where' logic to filter the rows... see example and link below)

$this->db->get('mytable')
$this->db->where('parent_id !=', 1);

or

//get all except parent id 1,4 or 7
$parent_ids = array(1, 4, 7);
$this->db->where_not_in('parent_id', $parent_ids);

To delete records from the database as your previous question title suggested (Delete current record with foreach in CodeIgniter)

You can do this using sql query without using any php logic by write the condition in sql ... see more at http://codeigniter.com/user_guide/database/active_record.html

example

//delete record where parent_id = 1
$this->db->delete('mytable', array('parent_id' => 1));

or

//delete record where parent_id = 1 or 5 or 8
$names = array(1, 5, 8);
$this->db->where_in('parent_id', $names);
$this->db->delete('mytable');

or

//delete record where parent_id = 1
$this->db->where('parent_id', 1);
$this->db->delete('mytable'); 

or

//delete all record where parent_id not eq 1
$this->db->where('parent_id !=', 1);

//delete all record where parent_id less than 10
$this->db->where('parent_id <', 10); 
Community
  • 1
  • 1
MagePal Extensions
  • 17,646
  • 2
  • 47
  • 62
  • I don't want to delete data in table, but record in $query in php codeigniter code – KH-DP Oct 26 '12 at 06:20
  • @KH-DP I just updated my code, your are trying "to Removing an item from object in a loop"... see http://stackoverflow.com/questions/7843157/removing-an-item-from-object-in-a-loop – MagePal Extensions Oct 26 '12 at 11:22
  • it's still can't delete. If in normal code maybe work, but it in CodeIgniter – KH-DP Oct 29 '12 at 08:47
  • Using CodeIgniter shouldn't have nothing to do with it...what you could do is instead of 'unset($rows[$key]);' create a new object $rows_new[] = $row; – MagePal Extensions Oct 29 '12 at 12:15
1

Do an invert an add it to an new array:

$new_result = array();

foreach($query->result() as $row){
    if($row->parent_id != 1){
       $new_result[] = $row;
    }
}

After that work with the $new_result object.

ps: wouldn't it be easier to filter the rows in the SQL query?

fccotech
  • 449
  • 2
  • 7
  • i have more than 20 category as menu-item and have three level menu. If i filter it by connect to database mean that: one query to select parent menu-item, then loop more than 20 times for sub menu-item, then loop for child sub menu-item – KH-DP Oct 26 '12 at 07:06
  • Ok, i think you want this: http://stackoverflow.com/questions/8840319/build-a-tree-from-a-flat-array-in-php – fccotech Nov 01 '12 at 08:32
  • Thank you fccotech for your recursive function – KH-DP Nov 03 '12 at 01:48