php - 在函数中添加参数
问题描述
我有 2 个可以协同工作的功能。但是我不能在第二个上添加参数,我不明白它是如何工作的。
第一个功能
if(!function_exists('pixiehuge_select_all')) {
function pixiehuge_select_all($table, $where = null, $order = null, $limit = null, $andwhere = null, $noteq = false, $or = false) {
global $wpdb;
$q = "SELECT * FROM `{$table}`";
$signWhere = ($or) ? 'OR' : 'AND';
// Select where
if(empty($noteq)) {
if(!empty($where)) {
$q .= " WHERE `" . esc_sql($where[0]) . "`='" . esc_sql($where[1]) . "'";
}
if(!empty($where) && !empty($andwhere)) {
$q .= " " . esc_sql($signWhere) . " `" . esc_sql($where[0]) . "`='" . esc_sql($where[1]) . "'";
}
} else {
if(!empty($where)) {
$q .= " WHERE `" . esc_sql($where[0]) . "`!='" . esc_sql($where[1]) . "'";
}
if(!empty($where) && !empty($andwhere)) {
$q .= " " . esc_sql($signWhere) . " `" . esc_sql($where[0]) . "`!='" . esc_sql($where[1]) . "'";
}
}
if(!empty($order)) {
$q .= " ORDER BY `" . esc_sql($order[0]) . "` " . esc_sql($order[1]);
}
if(!empty($limit)) {
$q .= " LIMIT 0, {$limit}";
}
// Check if plugin exists
if(!function_exists('huge_app')) {
return false;
}
$result = $wpdb->get_results($q, ARRAY_A);
return $result;
}
}
第二个功能我正在尝试编辑的功能
if(!function_exists('pixiehuge_streams')) {
function pixiehuge_streams($id = false, $streamCat = false, $slug = false) {
global $tables;
// Get Stream(s)
if($id) {
$streams = pixiehuge_select_all($tables['streams'], ['id', esc_sql($id)]);
} elseif($streamCat) {
$streams = pixiehuge_select_all($tables['streams'], ['category', esc_sql($streamCat)]);
} elseif($slug) {
$streams = pixiehuge_select_all($tables['streams'], ['slug', esc_sql($slug)]);
} else {
$streams = pixiehuge_select_all( $tables['streams'] );
}
return $streams;
}
}
我想要得到什么 $streams = "SELECT * FROM streams ORDER BY id DESC LIMIT 4";
(但如果我这样做,我会得到错误 500)
那么如何通过在第二个函数中$streams
添加参数来重写pixiehuge_select_all()
以通过 id desc 和 limit 4 获取订单?
解决方案
只需看你的函数参数并完成它。
$streams = pixiehuge_select_all($tables['streams'], [], ['id','DESC'] ,4);
推荐阅读
- r - 用第二层符号叠加地图
- shortest-path - GraphDB 无向图路径搜索
- python - 在多个 JSON 文件中查找包含特定键值的 JSON 文件
- fpdf - 使用 php 和 fpdf 将图像上传到 pdf 文件
- javascript - 如何使用自己的属性选择更改javascript中的属性名称?
- python - Visual Studio 代码终端不工作
- haskell - 我们可以在 Haskell 中定义一类函数定义吗?
- checkbox - 如何从复选框 DLGIdentifier 启用条目?
- typescript - 打字稿:在维护类型的同时将“通用对象数组”转换为“通用对象对象”
- python - 如何将 Python 字符串类型注释转换为正确的类型?