What arguments are required?
I have the following code and when I call <?php compare_update ?>
I get a PHP error that tells me I'm missing argument 1. Looking at the code can someone tell me what the variable for argument 1 is supposed to be?
function compare_update( $post_id ) {
//Assign Posts to Variables
$start = $_POST['_date_start'];
$end = $_POST['_date_end'];
//Parse Start Date
if($start):
$start = explode('-', $start);
$start = mktime($hour, $_POST['_date_minute'], 0, $start[0], $start[1], $start[2]);
$compare = date('ymd', $start);
endif;
//Parse End Date
if($end):
$today = date('ymd');
$end = explode('-', $end);
$end = mktime($hour, $_POST['_date_minute'], 0, $end[0], $end[1], $end[2]);
// If today's date is within the date range of an event that spans multiple days & the event is NOT YET PAST
if ((date('ymd',$start) < date('ymd',$end)) && (date('ymd',$end) >= $today) ){
// and the start date is before today
if (date('ymd',$start) < $today){
$compare = date('ymd'); //Overwrite start date $compare
} else {
$compare = date('ymd', $start);
}
} else {
$compare = date('ymd', $start);
}
endif;
//Check if values exist and then update
if($end):
update_post_meta( $post_id, '_date_end', $end);
el开发者_C百科se:
if($start)update_post_meta( $post_id, '_date_end', $start);
endif;
if($start)update_post_meta( $post_id, '_date_start', $start);
if($compare)update_post_meta( $post_id, '_date_compare', $compare);
if($echo): echo $compare; else: return $compare; endif;
}
Your function says:
function compare_update( $post_id ) {
Yet you don't call it with any argument, like:
compare_update('100'); // Or some number/id/whatnot,
// more than likely a $_POST['id']
// or $_GET['id'] variable
When you create a function in PHP with arguments, and you don't give a default value within the argument list, you have to provide a corresponding variable within the function call for each argument that requires an input.
// $post_id is an argument
function compare_update( $post_id ) {
... your code ...
}
So now you've created a function that requires one parameter be passed in the function call, which then is usable by that label within the function.
$post_id = $_POST['id'];
compare_update($post_id); // This is passing a parameter
For instance, you're using it down towards the end of the function:
//Check if values exist and then update
if($end):
update_post_meta( $post_id, '_date_end', $end);
else:
if($start)update_post_meta( $post_id, '_date_end', $start);
endif;
if($start)update_post_meta( $post_id, '_date_start', $start);
if($compare)update_post_meta( $post_id, '_date_compare', $compare);
if($echo): echo $compare; else: return $compare; endif;
Also, your code formatting is confusing. For instance, the code directly above could be:
//Check if values exist and then update
if ($end) {
update_post_meta( $post_id, '_date_end', $end);
} else {
if ($start) {
update_post_meta( $post_id, '_date_end', $start);
}
}
if ($start) {
update_post_meta( $post_id, '_date_start', $start);
}
if ($compare) {
update_post_meta( $post_id, '_date_compare', $compare);
}
if ($echo) {
echo $compare;
} else {
return $compare;
}
The parameter to compare_update()
?
You need to pass it the $post_id
.
Are you sending compare_update
a parameter? Like this:
compare_update($someNumber);
Looks like it's supposed to be the post id?
In other words,
<?php compare_update($id); ?>
where $id was set to the post_id you wish to compare_update to.
精彩评论