开发者

PHP Functions Question [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.

Closed 9 years ago.

Improve this question

I have two functions that i'm hoping i can put into one and can be called differently.

function my_function() {    
global $wpdb;
foreach( $wpdb->get_results( "MYSQL QUERY" ) as $key => $row)
{
    echo "'". $row->DAY . "'";
}
}

function my_function_two() {    
    global $wpdb;
    foreach( $wpdb->get_results( "MYSQL QUERY" ) as $key => $row)
    {
        echo "'". $row->TIME . "'";
    }
}

Notice how the only diffence between them both is the $row->TIME and $row->DAY, i'm using the exact same mysql query in both and was wondering if these two functions can be merged into one?

Thanks for help开发者_Go百科.


You can do:

function my_function($showDay = false) {    
  global $wpdb;
  foreach( $wpdb->get_results( "MYSQL QUERY" ) as $key => $row)
  {
    echo "'". ($showDay === true) ? $row->DAY : $row->TIME . "'";
  }
}

If you have to echo $row->DAY, call the function with true as parameter or leave it out to echo $row->TIME.

Example:

my_function(true); // for $row->DAY
my_function(); // for $row->TIME


function my_function() {    
    global $wpdb;
    foreach( $wpdb->get_results( "MYSQL QUERY" ) as $key => $row)
    {
        echo "'". $row->DAY . "'";
        echo "'". $row->TIME . "'";
    }
}

Since you're being incredibly vague about what you mean by "merge", this is my best guess. If you could provide some sample output, I can re-do my answer to match.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜