Jump to content

updating records based on what they contain in a certain field


dadamssg87

Recommended Posts

I am storing products in my database. The table has id, price, title, description, and add_ons. The add_ons column stores id's of other products that can be purchased with it. These id numbers are separated by commas and there's no limit to how many id numbers can be in the column. So for instance a typical product row will look something like this

 

34|9.99|Box of Candy|Assorted bag of candy|31,32,56,30

 

Now i'm trying to figure out the best way to delete a product. If i'm deleting product item 30, i don't want that "30" to still be in the add_ons column of item 34.

 

I didn't know if there was a more efficient way of doing something like the following

<?php
$item_to_delete = 30;

$query = $this->db->query("SELECT * Products WHERE username = '$username' ORDER BY created DESC");	

foreach($query->result() as $row)
{
$add_ons = explode(",",$row->add_ons);
        
         foreach($add_ons as $key => $id)
         {
               if($id = $item_to_delete)
               {
                    unset($add_ons[$key]);

                    $add_ons = implode(",",$add_ons);

                    $this->db->query("Update Products SET add_ons = '$add_ons' WHERE id = '$row->id'");	
                }
          }
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.