it really depend on what sort of framework you use.
and how long time you wish to spend on re-factor you code.
after re-factor, you new code may not short than old one, but the new
code will more easy to understand.
from my point of view:
performance should be considered at last, (most of time i do not care
of performance).
for example:
if ($entry['product_sku'] && !in_array($entry['product_sku'],
$imp)){
> $imp[] = $entry['product_sku']
> }
above code is hard to understand.
why you want to test ($entry['product_sku']), ?
why $entry['product_sku'] will contains empty value?
also about those flag variables:
$last_pid = 0;
$first = 1;
unless you are the programer who wrote the code, for other, at first,
it very hard to see
why need this kind of flag variables here.
//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.
mate, do not do it, that make you code more harder to understand.
whenever you add a @, condensing If statements, you probably will
need to add other 2 lines of comments to explains why you do it.
do not shorting you code to make it hard to understand. also
performance is not that important.
anru
On Feb 19, 2009, at 10:54 AM, 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();
>
> $sql = 'select product_id, product_sku from
> #__vm_product_selecon_product_codes '.
> 'order by product_id, product_sku ';
> $db->setQuery($sql);
>
> $list = $db->loadAssocList();
>
> $last_pid = 0;
> $first = 1;
> $imp=array();
> if ($list){
> foreach ($list as $entry){
> if ($entry['product_sku'] && !
> in_array($entry['product_sku'], $imp)){
> $imp[] = $entry['product_sku'];
> }
> if (!$first){
> if ($last_pid != $entry['product_id']){
> $upsql = "update #__vm_product set
> custom_attribute='".implode(';',$imp).
> "' where product_id=".$last_pid;
> $db->setQuery($upsql);
> $db->query();
> $imp=array();
> $last_pid = $entry['product_id'];
> }
> }else{
> $first = 0;
> }
> }
> }
> }
>
>
> >
--~--~---------~--~----~------------~-------~--~----~
NZ PHP Users Group: http://groups.google.com/group/nzphpug
To post, send email to [email protected]
To unsubscribe, send email to
[email protected]
-~----------~----~----~----~------~----~------~--~---