在foreach循环MySQL中的foreach循环

时间:2013-12-14 21:32:08

标签: php mysql

我会尝试用更多细节来解释它。

在数据库中有以下制造商: Apple,Canon,HTC,Hewlett,Palm和Sony。

这是现有的PHP代码:

$query = $db->result_cache("
  SELECT   DISTINCT(SUBSTRING(manufacturers_name,1,1)) AS firstletter
  FROM     " . TABLE_MANUFACTURERS . "
  ORDER BY firstletter
");
$module_index = array();
$module_content = array();
foreach ($query['RESULT'] as $result) {
  $module_index[] = array(
    'MANU_INDEX' => $result['firstletter'],
  );
  $query = $db->result_cache("
    SELECT   manufacturers_name, 
             manufacturers_id 
    FROM     " . TABLE_MANUFACTURERS . "
    WHERE    manufacturers_name LIKE '" . $result['firstletter'] ."%'
    ORDER BY manufacturers_name
  ");
  foreach ($query['RESULT'] as $result) {
    if (SEARCH_ENGINE_FRIENDLY_URLS == 'true') {
      $SEF_parameter = '&name=' . inc_cleanName($result['manufacturers_name']);
    }
    $module_content[] = array(
      'MANU_NAME'  => $result['manufacturers_name'],
      'MANU_LINK'  => inc_url(FILENAME_DEFAULT, 'manufacturers_id=' . $result['manufacturers_id'] . $SEF_parameter),
    );
  } 
}

使用第一个mysql查询我获取带有distinct的第一个字母以避免双重结果(例如,因为HTC和Hewlett而出现“H”)

这是我得到的数组

Array(
[0] => Array([MANU_INDEX] => A)
[1] => Array([MANU_INDEX] => C)
[2] => Array([MANU_INDEX] => H)
[3] => Array([MANU_INDEX] => P)
[4] => Array([MANU_INDEX] => S)
)

这是正确的。

使用secound mysql-query我尝试让所有制造商以第一个字母开头(例如Apple的“A”)。但是当我调试$ module_content数组时,我总是得到所有制造商

Array(
  [0] => Array(
     [MANU_NAME] => Apple
     [MANU_LINK] => manufacturers_id=1)
  [1] => Array(
     [MANU_NAME] => Canon
     [MANU_LINK] => manufacturers_id=2)
  [2] => Array(
      [MANU_NAME] => Hewlett-Packard
      [MANU_LINK] => manufacturers_id=4)
  [3] => Array(
      [MANU_NAME] => HTC
      [MANU_LINK] => manufacturers_id=3)
  [4] => Array(
      [MANU_NAME] => Palm
      [MANU_LINK] => manufacturers_id=5)
  [5] => Array(
      [MANU_NAME] => Sony
      [MANU_LINK] => manufacturers_id=6)
  )

我不明白这个问题,因为第二个mysql查询中的WHERE子句说要用第一个字母的开头来获取所有制造商。

0 个答案:

没有答案