PHP Functions Question [closed]
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 questionI 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.
精彩评论