我已经在一个项目上工作了好几个月,我陷入困境。患有隧道视觉。
问题是;我正在使用该查询:
SELECT DISTINCT `kgsim_towns`.`name` as town, `kgsim_districts`.`name` as district, `status`, `category`, `type`, `attribute`
FROM (`ad_general`)
LEFT JOIN `kgsim_towns` ON `kgsim_towns`.`id` = `ad_general`.`town`
LEFT JOIN `kgsim_districts` ON `kgsim_districts`.`id` = `ad_general`.`district`
得到结果(不仅如此,但这足以让你了解):
Array
(
[0] => Array
(
[town] => Yenişehir
[district] => Menteş
[status] => 1
[category] => 1
[type] => 9
[attribute] => 4
)
[1] => Array
(
[town] => Mezitli
[district] => Mezitli
[status] => 1
[category] => 1
[type] => 9
[attribute] => 4
)
[2] => Array
(
[town] => Yenişehir
[district] => Limonlu
[status] => 1
[category] => 1
[type] => 9
[attribute] => 6
)
[3] => Array
(
[town] => Yenişehir
[district] => Ellinciyıl
[status] => 1
[category] => 1
[type] => 9
[attribute] => 4
)
[4] => Array
(
[town] => Mezitli
[district] => Menderes
[status] => 1
[category] => 1
[type] => 9
[attribute] => 6
)
[5] => Array
(
[town] => Yenişehir
[district] => Ellinciyıl
[status] => 1
[category] => 1
[type] => 9
[attribute] => 9
)
[6] => Array
(
[town] => Mezitli
[district] => Akdeniz
[status] => 1
[category] => 1
[type] => 9
[attribute] => 4
)
[7] => Array
(
[town] => Yenişehir
[district] => Pirireis
[status] => 1
[category] => 1
[type] => 9
[attribute] => 17
)
[8] => Array
(
[town] => Mezitli
[district] => Viranşehir
[status] => 1
[category] => 1
[type] => 9
[attribute] => 4
)
[9] => Array
(
[town] => Yenişehir
[district] => Batıkent
[status] => 1
[category] => 1
[type] => 9
[attribute] => 4
)
[10] => Array
(
[town] => Yenişehir
[district] => Akkent
[status] => 1
[category] => 1
[type] => 9
[attribute] => 4
)
[11] => Array
(
[town] => Yenişehir
[district] => Güvenevler
[status] => 1
[category] => 1
[type] => 9
[attribute] => 6
)
[12] => Array
(
[town] => Mezitli
[district] => Fatih
[status] => 1
[category] => 1
[type] => 9
[attribute] => 4
)
[13] => Array
(
[town] => Mezitli
[district] => Menderes
[status] => 2
[category] => 1
[type] => 9
[attribute] => 2
)
[14] => Array
(
[town] => Mezitli
[district] => Davultepe
[status] => 1
[category] => 1
[type] => 9
[attribute] => 4
)
[15] => Array
(
[town] => Mezitli
[district] => Yenidistrict
[status] => 1
[category] => 1
[type] => 9
[attribute] => 4
)
[16] => Array
(
[town] => Mezitli
[district] => Tece
[status] => 1
[category] => 1
[type] => 9
[attribute] => 2
)
[17] => Array
(
[town] => Yenişehir
[district] => Fuatmorel
[status] => 1
[category] => 1
[type] => 9
[attribute] => 4
)
[18] => Array
(
[town] => Mezitli
[district] => Fatih
[status] => 2
[category] => 1
[type] => 9
[attribute] => 4
)
[19] => Array
(
[town] => Mezitli
[district] => Viranşehir
[status] => 1
[category] => 1
[type] => 9
[attribute] => 6
)
[20] => Array
(
[town] => Mezitli
[district] => Menderes
[status] => 2
[category] => 1
[type] => 9
[attribute] => 6
)
)
我想像这样构建一个superfish menu:
<ul>
<li>
Choose Category
<ul>
<li>
Town 1
<ul>
<li>
District 1
<ul>
<li>
Status 1
<ul>
<li>
Category 1
<ul>
<li>
Type 9
<ul>
<li>
attiribute 6
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>Status 2</li>
</ul>
</li>
<li>District 2</li>
<li>District 3</li>
</ul>
</li>
<li>Town 2</li>
<li>Town 3</li>
<li>Etc</li>
</ul>
</li>
<ul>
问题是如何实现这一目标?
答案 0 :(得分:2)
第一种方法
修改选择以包含顺序:
SELECT DISTINCT
`kgsim_towns`.`name` as town,
`kgsim_districts`.`name` as district,
`status`,
`category`,
`type`,
`attribute`
FROM (`ad_general`)
LEFT JOIN `kgsim_towns` ON `kgsim_towns`.`id` = `ad_general`.`town`
LEFT JOIN `kgsim_districts` ON `kgsim_districts`.`id` = `ad_general`.`district`
ORDER BY 1,2,3,4,5,6
写数组数组(我用伪代码写,对不起):
previous_town = ''
previous_district = ''
for each row in results:
if previus_town != row.town
array[row.town] = array()
previous_town = row.town
previous_district = ''
if preious_district != row.district
array[row.town][row.district] = array()
previous_district = row.district
...
此时你有一组数组数组......
现在创建一个递归函数,将其转换为<ul><li>
。有点像这样。
(我用伪代码写的,对不起):
function toli( array ) {
str = "<ul>"
for each key, value in array
str += "<li>" + key +
iif( value is array, toli(value), value )
+ "</li>"
str += "</ul>
}
编辑,简单方法
为了减少问题的复杂性,您可以在嵌套循环中创建菜单: 如果想要进行单个大查询,则应将此查询拆分为几个小问题:
$str = '<ul><li>Choose Category<ul>'
SELECT DISTINCT
`kgsim_towns`.`name` as town
FROM (`ad_general`)
LEFT JOIN `kgsim_towns` ON `kgsim_towns`.`id` = `ad_general`.`town`
LEFT JOIN `kgsim_districts` ON `kgsim_districts`.`id` = `ad_general`.`district`
ORDER BY 1,2,3,4,5,6
For each $town selected:
$str = $str . '<li>' . $town . '<ul>'
SELECT DISTINCT
`kgsim_districts`.`name` as district
FROM (`ad_general`)
LEFT JOIN `kgsim_towns` ON `kgsim_towns`.`id` = `ad_general`.`town`
LEFT JOIN `kgsim_districts` ON `kgsim_districts`.`id` = `ad_general`.`district`
WHERE
`kgsim_towns`.`name` = $town
ORDER BY 1,2,3,4,5,6
For each $district selected:
$str = $str . '<li>' . $district . '<ul>'
SELECT DISTINCT
`status`
FROM (`ad_general`)
LEFT JOIN `kgsim_towns` ON `kgsim_towns`.`id` = `ad_general`.`town`
LEFT JOIN `kgsim_districts` ON `kgsim_districts`.`id` = `ad_general`.`district`
WHERE
`kgsim_towns`.`name` = $town and
`kgsim_districts`.`name` = $district
ORDER BY 1,2,3,4,5,6
For each $status selected:
$str = $str . '<li>' . $status . '<ul>'
//etc for category, type attribute ...
$str = $str . '</ul>' //close status ul
$str = $str . '</ul>' //close district ul
$str = $str . '</ul>' //close town ul
答案 1 :(得分:1)
最后我完成了这个功能。可能它不是最好的方法,例如,可能不需要为每一行运行array_unique函数。但仍然是迄今为止唯一可行的解决方案。
function superfish($array = array(), $array_language) {
foreach ($array as $key => $option) {
// town start
$town = $option['town'];
$array_new['town'][$key] = $town;
$array_new['town_unique'] = array_unique($array_new['town']);
// town finish
// district start
$district = $option['district'];
$town_district = $array_new['town'][$key];
$town_district .= "_district";
$town_district_unique = $town_district."_unique";
$array_new[$town_district][$key] = $district;
$array_new[$town_district_unique] = array_unique($array_new[$town_district]);
// district finish
// status start
$status = $option['status'];
$town_district_status = $town."_".$district."_status";
$town_district_status_unique = $town_district_status."_unique";
$array_new[$town_district_status][$key] = $status;
$array_new[$town_district_status_unique] = array_unique($array_new[$town_district_status]);
// status finish
// category start
$category = $option['category'];
$town_district_status_category = $town."_".$district."_".$status."_category";
$town_district_status_category_unique = $town_district_status_category."_unique";
$array_new[$town_district_status_category][$key] = $category;
$array_new[$town_district_status_category_unique] = array_unique($array_new[$town_district_status_category]);
// category finish
// type start
$type = $option['type'];
$town_district_status_category_type = $town."_".$district."_".$status."_".$category."_type";
$town_district_status_category_type_unique = $town_district_status_category_type."_unique";
$array_new[$town_district_status_category_type][$key] = $type;
$array_new[$town_district_status_category_type_unique] = array_unique($array_new[$town_district_status_category_type]);
// type finish
// özellik start
$attribute = $option['attribute'];
$town_district_status_category_type_attribute = $town."_".$district."_".$status."_".$category."_".$type."_attribute";
$town_district_status_category_type_attribute_unique = $town_district_status_category_type_attribute."_unique";
$array_new[$town_district_status_category_type_attribute][$key] = $attribute;
$array_new[$town_district_status_category_type_attribute_unique] = array_unique($array_new[$town_district_status_category_type_attribute]);
// özellik finish
/* $array_new['town']['district'][$key] = $option['district'];
$array_new['town']['district']['status'][$key] = $option['status'];
$array_new['town']['district']['status']['category'][$key] = $option['category'];
$array_new['town']['district']['status']['category']['type'] = $option['type'];
$array_new['town']['district']['status']['category']['type']['attribute'] = $option['attribute']; */
}
if(is_array($array_new['town_unique'])) {
// $array_superfish = "<div class=\"navbar\">";
$array_superfish = "<ul>";
foreach ($array_new['town_unique'] as $town) {
$array_superfish .= "<li><a><span class=\"menu-title\">";
$array_superfish .= $town;
$array_superfish .= "</span></a>";
$town_district_unique = $town."_district_unique";
if(is_array($array_new[$town_district_unique])) {
$array_superfish .= "<ul>";
foreach ($array_new[$town_district_unique] as $district) {
$array_superfish .= "<li><a><span class=\"menu-title\">";
$array_superfish .= $district;
$array_superfish .= "</span></a>";
$town_district_status_unique = $town."_".$district."_status_unique";
if(is_array($array_new[$town_district_status_unique])) {
$array_superfish .= "<ul>";
foreach ($array_new[$town_district_status_unique] as $status) {
$array_superfish .= "<li><a><span class=\"menu-title\">";
$array_superfish .= $array_language[11][$status];
$array_superfish .= "</span></a>";
$town_district_status_category_unique = $town."_".$district."_".$status."_category_unique";
if(is_array($array_new[$town_district_status_category_unique])) {
$array_superfish .= "<ul>";
foreach ($array_new[$town_district_status_category_unique] as $category) {
$array_superfish .= "<li><a><span class=\"menu-title\">";
$array_superfish .= $array_language[21][$category];
$array_superfish .= "</span></a>";
$town_district_status_category_type_unique = $town."_".$district."_".$status."_".$category."_type_unique";
if(is_array($array_new[$town_district_status_category_type_unique])) {
$array_superfish .= "<ul>";
foreach ($array_new[$town_district_status_category_type_unique] as $type) {
if($category==3 or $category==NULL)
$category = 2;
$type_code = ''.$category.'31';
$array_superfish .= "<li><a><span class=\"menu-title\">";
$array_superfish .= $array_language[$type_code][$type];
$array_superfish .= "</span></a>";
$town_district_status_category_type_attribute_unique = $town."_".$district."_".$status."_".$category."_".$type."_attribute_unique";
if(is_array($array_new[$town_district_status_category_type_attribute_unique])) {
$array_superfish .= "<ul>";
foreach ($array_new[$town_district_status_category_type_attribute_unique] as $attribute) {
$array_superfish .= "<li><a><span class=\"menu-title\">";
$genel_attribute_code = $array_language[101][$category]*10+2;
$genel_attribute_metin = $array_language[$genel_attribute_code][$attribute];
$array_superfish .= $array_language[$genel_attribute_code][$attribute];
$array_superfish .= "</span></a>";
$array_superfish .= "</li>";
}
$array_superfish .= "</ul>";
}
$array_superfish .= "</li>";
}
$array_superfish .= "</ul>";
}
$array_superfish .= "</li>";
}
$array_superfish .= "</ul>";
}
$array_superfish .= "</li>";
}
$array_superfish .= "</ul>";
}
$array_superfish .= "</li>";
}
$array_superfish .= "</ul>";
}
$array_superfish .= "</li>";
}
$array_superfish .= "</ul>";
}
// $array_superfish .= "</div>";
return $array_superfish;
}