首頁 >後端開發 >php教程 >这个递归函数究竟是哪里写错了?

这个递归函数究竟是哪里写错了?

WBOY
WBOY原創
2016-06-06 20:29:171088瀏覽

$user_list为0时,竟然会再次重新调用,$user_list再次为0时,才会返回$result

<code>function count_user_order($user_list_str, $limit = 4, $times = 1)
{
  static $result = array('users_count'=>array(), 'order_number'=>array(), 'order_amount'=>array());
  $sql = 'SELECT user_id FROM '.$GLOBALS['ecs']->table('users')." WHERE parent_id IN (%s)"; // 统计顾客
  $user_list = $GLOBALS['db']->getCol(sprintf($sql, $user_list_str));

  $sql_order = 'SELECT SUM(order_amount) order_amount,COUNT(*) order_number FROM '
  .$GLOBALS['ecs']->table('order_info')." WHERE pay_status=2 AND user_id IN (%s)"; // 统计订单数量及金额
  list($result['order_amount'][], $result['order_number'][]) = $GLOBALS['db']->getRow(sprintf($sql_order, $user_list_str));

  $result['users_count'][] = count($user_list);
  if ((!$limit || $times </code>

或许图片更容易看些:

这个递归函数究竟是哪里写错了?

回复内容:

$user_list为0时,竟然会再次重新调用,$user_list再次为0时,才会返回$result

<code>function count_user_order($user_list_str, $limit = 4, $times = 1)
{
  static $result = array('users_count'=>array(), 'order_number'=>array(), 'order_amount'=>array());
  $sql = 'SELECT user_id FROM '.$GLOBALS['ecs']->table('users')." WHERE parent_id IN (%s)"; // 统计顾客
  $user_list = $GLOBALS['db']->getCol(sprintf($sql, $user_list_str));

  $sql_order = 'SELECT SUM(order_amount) order_amount,COUNT(*) order_number FROM '
  .$GLOBALS['ecs']->table('order_info')." WHERE pay_status=2 AND user_id IN (%s)"; // 统计订单数量及金额
  list($result['order_amount'][], $result['order_number'][]) = $GLOBALS['db']->getRow(sprintf($sql_order, $user_list_str));

  $result['users_count'][] = count($user_list);
  if ((!$limit || $times </code>

或许图片更容易看些:

这个递归函数究竟是哪里写错了?

随便扫了下,应该是没有出口,就是没有停止递归的条件
还有你应该把统计顾客和统计订单数量及金额也写成一个函数

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn