Php 如何在查询数组中动态插入orderby语句
我试图为列出菜单项的查询动态分配排序顺序(存储在下面的$current\u sort中) 如果我硬编码排序顺序,它可以正常工作,但是,当我尝试将排序参数动态分配给字符串时,它失败了。我错过了什么Php 如何在查询数组中动态插入orderby语句,php,sql-order-by,Php,Sql Order By,我试图为列出菜单项的查询动态分配排序顺序(存储在下面的$current\u sort中) 如果我硬编码排序顺序,它可以正常工作,但是,当我尝试将排序参数动态分配给字符串时,它失败了。我错过了什么 $current_sort = ", 'orderby' => 'title', 'order' => 'asc'"; $myposts = get_posts( array( 'cat' => "$cat,-$catHidden", 'numb
$current_sort = ", 'orderby' => 'title', 'order' => 'asc'";
$myposts = get_posts(
array(
'cat' => "$cat,-$catHidden",
'numberposts' => $my_current_count . $current_sort
));
//If I hard code the value of $current_sort it works fine
$myposts = get_posts(
array(
'cat' => "$cat,-$catHidden",
'numberposts' => $my_current_count,
'orderby' => 'title',
'order' => 'asc'));
不能将字符串转换为PHP源代码。(至少你不应该) 试试这个:
$current_sort_order = "title";
$current_sort_direction = "asc";
$myposts = get_posts(array(
'cat' => "$cat,-$catHidden",
'numberposts' => $my_current_count,
'orderby' => $current_sort_order,
'order' => $current_sort_direction
) );
不能将字符串转换为PHP源代码。(至少你不应该) 试试这个:
$current_sort_order = "title";
$current_sort_direction = "asc";
$myposts = get_posts(array(
'cat' => "$cat,-$catHidden",
'numberposts' => $my_current_count,
'orderby' => $current_sort_order,
'order' => $current_sort_direction
) );
你做错了,你不必抱怨..试试这个: $current_sort=“title”; $order=“asc”; $myposts=get\u posts( 排列( '猫'=>“$cat,-$catHidden”, 'numberposts'=>my\u current\u count, 'orderby'=>$current\u排序, “订单”=>$order
)); 你做错了,你不必说……试试这个: $current_sort=“title”; $order=“asc”; $myposts=get\u posts( 排列( '猫'=>“$cat,-$catHidden”, 'numberposts'=>my\u current\u count, 'orderby'=>$current\u排序, “订单”=>$order
)); 行上的字符串连接
'numberposts' => $my_current_count . $current_sort
与在中创建多个数组元素不同
'numberposts' => $my_current_count,
'orderby' => 'title',
'order' => 'asc'));
在第一个实例中,numberposts
成为包含排序信息的字符串。
在第二个实例中,numberposts
仅包含当前计数
更好的选择可能是:
$orderoption="<TITLE HERE>";
$order_dir="<SORT OPTION HERE>";
$myposts = get_posts(
array(
'cat' => "$cat,-$catHidden",
'numberposts' => $my_current_count,
'orderby' => $orderoption,
'order' => $order_dir));
$orderoption=”“;
$order_dir=“”;
$myposts=get\u posts(
排列(
'猫'=>“$cat,-$catHidden”,
'numberposts'=>my\u current\u count,
'orderby'=>$orderoption,
“订单”=>$order_dir);
行上的字符串连接
'numberposts' => $my_current_count . $current_sort
与在中创建多个数组元素不同
'numberposts' => $my_current_count,
'orderby' => 'title',
'order' => 'asc'));
在第一个实例中,numberposts
成为包含排序信息的字符串。
在第二个实例中,numberposts
仅包含当前计数
更好的选择可能是:
$orderoption="<TITLE HERE>";
$order_dir="<SORT OPTION HERE>";
$myposts = get_posts(
array(
'cat' => "$cat,-$catHidden",
'numberposts' => $my_current_count,
'orderby' => $orderoption,
'order' => $order_dir));
$orderoption=”“;
$order_dir=“”;
$myposts=get\u posts(
排列(
'猫'=>“$cat,-$catHidden”,
'numberposts'=>my\u current\u count,
'orderby'=>$orderoption,
“订单”=>$order_dir);
一种方法是动态的,而另一种方法不是动态的?没有任何东西阻止您编写'orderby'=>$sortField
。而且,将字段名和排序方向与$currentSort
中的任何内容隔离开来应该不会那么困难。一种方法是动态的,而另一种方法不是动态的?没有任何东西阻止您编写'orderby'=>$sortField
。将字段名和排序方向与$currentSort
中的任何内容隔离开来应该不会有那么困难。