gpt4 book ai didi

php - 这个方法应该分解成单独的方法吗?

转载 作者:行者123 更新时间:2023-12-02 07:11:57 25 4
gpt4 key购买 nike

此方法采用搜索关键字和解析的 mysql 查询,并重写 where 表达式以包含 LIKE %keyword%。

它运行良好,但我不知道使用具有这么多循环的方法是好是坏...

private function build_where($query_array, $options)  
{
//add WHERE starting point
$where = '';

if(!empty($query_array['WHERE']))
{
//build where array
$where_array = $query_array['WHERE'];

//start the where
$where .= 'WHERE ';

//get columns array
$columns_array = $this->build_columns_array($query_array);

//if there is a search string
if(!empty($options['sSearch']))
{
//check for enabled columns
$i = 0;
$columns_length = count($columns_array);
for($i; $i < intval($columns_length); $i++)
{
//create the options boolean array
$searchable_columns['bSearchable_'.$i] = $options['bSearchable_'.$i];
}

//loop through searchable_columns for true values
foreach($searchable_columns as $searchable_column_key => $searchable_column_val)
{
if($searchable_column_val == true)
{
//get an integer from the searchable_column key
$column_id = preg_replace("/[^0-9]/", '', $searchable_column_key);

//lookup column name by index
foreach($columns_array as $columns_array_key => $columns_array_val)
{
//if the $columns_array_key matches the $column_id
if($columns_array_key == $column_id)
{
//loop to build where foreach base expression
$i = 0;
$where_length = count($where_array);
for($i; $i < intval($where_length); $i++)
{
//append the existing WHERE Expressions
$where .= $where_array[$i]['base_expr'];
}

//append the LIKE '%$options['sSearch'])%'
$where .= ' AND '.$columns_array_val." LIKE '%".$options['sSearch']."%' OR ";
}
}
}
}
//remove the last OR
$where = substr_replace($where, "", -3);
}
else
{
//loop to build where
$i = 0;
$where_length = count($where_array);
for($i; $i < intval($where_length); $i++)
{
$where .= $where_array[$i]['base_expr'];
}
}
}

//print_r($where_length);
return $where;
}

最佳答案

Kent Beck 或 Martin Fowler 的思想流派实际上会建议您将这些大方法重构为许多小方法。在我看来,它不容易阅读,这将是重构的主要原因。

关于php - 这个方法应该分解成单独的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5130012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com