我在调用函数
时遇到问题$ categ-> categ_body()
第二次参数不会改变
$categ = new Categories("Autos & Vehicles");
$categ->categ_body(60*60*24);
$adm_json_arr["a_v_num_last_24_h_videos"] = $categ->body_vid_num;
// last 7 days
$categ->categ_body(60*60*24*7);
$adm_json_arr["a_v_num_last_7_d_videos"] = $categ->body_vid_num;
我需要做什么,所以当我调用函数时要更改参数或者我必须再次调用该类
function categ_body($time){
require $_SERVER["DOCUMENT_ROOT"] . '/include/database/connect_cnt.php';
$time2 = time() - ($time *2);
$this->time = $time;
$time = time() - $time;
$stmt = $conn->prepare("SELECT COUNT(Id) AS Id_count FROM video WHERE Category = '" .$this->categ.
"' AND Published = '1' AND Date > '" .$time. "'");
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($ctg_last_videos);
$stmt->fetch();
$this->body_vid_num = number_format($ctg_last_videos);
}
所有问题都在于$ time的参数
60 * 60 * 24
并且不会更改为
60 * 60 * 24 * 7
答案 0 :(得分:1)
有类似吸气剂和二传手的东西:
class Categories{
public $body_vid_num;
public function setBodyVidNum($num){
// some process or logic
...
$this->body_vid_num = $some_value;
}
public function getBodyVidNum(){
return $this->body_vid_num;
}
}
然后使用它:
$categ->setBodyVidNum(60*60*24);
$adm_json_arr["a_v_num_last_24_h_videos"] = $categ->getBodyVidNum();
// last 7 days
$categ->setBodyVidNum(60*60*24*7);
$adm_json_arr["a_v_num_last_7_d_videos"] = $categ->getBodyVidNum();
答案 1 :(得分:1)
我首先更改categ_body()函数以接收mysql DATE_SUB间隔文本而不是秒数。然后,我会修改查询以利用它。然后,为了使它只进行一次调用,我将返回我赋给类变量的值。
function categ_body($time = '24 HOUR'){
require $_SERVER["DOCUMENT_ROOT"] . '/include/database/connect_cnt.php';
$stmt = $conn->prepare("SELECT COUNT(Id) AS Id_count FROM video WHERE Category = '" .$this->categ.
"' AND Published = '1' AND Date > UNIX_TIMESTAMP(DATE_SUB(NOW(),INTERVAL {$time}))");
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($ctg_last_videos);
$stmt->fetch();
$this->body_vid_num = number_format($ctg_last_videos);
return number_format($ctg_last_videos);
}
现在,您只需调用该函数一次并传入正确的参数:
// No need to pass in a param here, since we set the default value of time
// to '24 HOUR'
$adm_json_arr["a_v_num_last_24_h_videos"] = $categ->setBodyVidNum();
$adm_json_arr["a_v_num_last_7_d_videos"] = $categ->setBodyVidNum('7 DAY');
从设定者那里返回一个值是不正常的,但这并不是一件坏事。如果您觉得自己更喜欢传统风格,那么您可以随时做到:
// No need to pass in a param here, since we set the default value of time
// to '24 HOUR'
$categ->setBodyVidNum();
$adm_json_arr["a_v_num_last_24_h_videos"] = $categ->getBodyVidNum();
$categ->setBodyVidNum('7 DAY');
$adm_json_arr["a_v_num_last_7_d_videos"] = $categ->getBodyVidNum();
答案 2 :(得分:0)
您在Categ_body()函数中做了什么?
您需要在该函数中设置body_vid_num。
答案 3 :(得分:0)
你可以尝试
$adm_json_arr["a_v_num_last_24_h_videos"] = $categ->categ_body(60*60*24);
$adm_json_arr["a_v_num_last_7_d_videos"] = $categ->categ_body(60*60*24*7);