Jochen Daum wrote: > Hi, > > I have a common piece of code below and I wonder if this can be done > more elegantly. The code below reads records from a database - in this > example a product_id and lots of skus that represent variations of this > product. It then implodes all skus of a product into a field in another > table. This is done for each product_id. > > I know this can be done with less lines of code by using @, condensing > if statements and accepting that it throws E_NOTICE some times. But > aside from that, can it be done completely diferently and more elegantly. > > I'm seeing myself writing this piece of code over and over, covering > database queries or converting one array structure into another. > > BTW, database layer is from Joomla, but could be any database layer. > > > Cheers, > Jochen > > $db =& JFactory::getDBO();
GROUP_CONCAT in mysql is handy for this kind of thing. Try something like (untested) UPDATE #__vm_product AS p SET custom_attribute= ( SELECT GROUP_CONCAT(product_sku SEPARATOR ';') FROM #__vm_product_selecon_product_codes AS c WHERE c.product_id = p.product_id GROUP BY product_id ) -- Tim Oliver Software Engineer 160 Cashel Street (Level 2) PO Box 22 128 Christchurch New Zealand P +64 3 377 0007 F +64 3 377 6582 E [email protected] www.e2media.co.nz --~--~---------~--~----~------------~-------~--~----~ NZ PHP Users Group: http://groups.google.com/group/nzphpug To post, send email to [email protected] To unsubscribe, send email to [email protected] -~----------~----~----~----~------~----~------~--~---
