PDA

View Full Version : Is there a more efficient way to write this code?


Antivirus
03-26-2009, 12:20 AM
I have a plugin I am using in two different locations, however it essentially does the exact same thing for each location with the exception that it pulls data from a different array to set the DM.


global $sc_typeset;
if ($sc_typeset == 'sc_typetask' OR $sc_typeset == 'sc_typeevent')
{
if (THIS_SCRIPT == 'newthread')
{
$dataman->set('sc_typeset', $post['sc_typeset']);
$dataman->setr('dateline_from', $post['dateline_from']);
$dataman->setr('dateline_to', $post['dateline_to']);
$dataman->setr('sc_description', $post['sc_description']);
$dataman->setr('sc_location', $post['sc_location']);
}

if (THIS_SCRIPT == 'editpost')
{
$dataman->set('sc_typeset', $edit['sc_typeset']);
$dataman->setr('dateline_from', $edit['dateline_from']);
$dataman->setr('dateline_to', $edit['dateline_to']);
$dataman->setr('sc_description', $edit['sc_description']);
$dataman->setr('sc_location', $edit['sc_location']);
}

}


As you can see, it's somewhat redundant. I want to include this same plugin code in both hook locations ('newpost_process' & 'editpost_update_process') and simply use a conditional to determine which array to pass to the DM. I was thinking on something like this:

if (THIS_SCRIPT == 'newthread')
{
// use the $newpost array
}
else if (THIS_SCRIPT == 'editpost')
{
// use the $edit array
}


So basically, what I am asking is if anyone can think of a shorter way to write this than I am currently using?

Thanks!

AHealthForum
03-26-2009, 07:46 AM
THIS_SCRIPT == 'editpost' OR THIS_SCRIPT == 'newthread'

maybe?

Marco van Herwaarden
03-26-2009, 10:37 AM
Put the code in a function and pass the array to be used as argument.

Antivirus
03-26-2009, 03:42 PM
A function is another way of handling it, but actually the way I solved it was by simply using a new var like this:


global $sc_typeset;
if ($sc_typeset == 'sc_typetask' OR $sc_typeset == 'sc_typeevent')
{
if (THIS_SCRIPT == 'newthread')
{
$scdata = $post;
}
else if (THIS_SCRIPT == 'editpost')
{
$scdata = $edit;
}

$dataman->set('sc_typeset', $scdata['sc_typeset']);
$dataman->setr('dateline_from', $scdata['dateline_from']);
$dataman->setr('dateline_to', $scdata['dateline_to']);
$dataman->setr('sc_description', $scdata['sc_description']);
$dataman->setr('sc_location', $scdata['sc_location']);
}


It's silly now, as the solution was so simple i couldn't see it last night - just goes to show me that my brain doesn't really work all that well after 14+ hours of coding, lol. :o