vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Is there a more efficient way to write this code? (https://vborg.vbsupport.ru/showthread.php?t=209462)

Antivirus 03-26-2009 12:20 AM

Is there a more efficient way to write this code?
 
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.

PHP Code:

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:
PHP Code:

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:

PHP Code:

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


All times are GMT. The time now is 02:40 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01150 seconds
  • Memory Usage 1,737KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (4)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete