gpt4 book ai didi

php - 排除选择的算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:08:08 25 4
gpt4 key购买 nike

首先抱歉我的英语不好

我对排除选择的算法有疑问

我有这样的设置

enter image description here

基于这些设置,用户可以选择 3 个组合框

比如我的组合框

First combo------Second combo------Third combo

2-------------|-------100-------|------35

2-------------|-------100-------|------40

2-------------|-------100-------|------55

2-------------|-------100-------|------72

2-------------|-------25--------|------35

2-------------|-------25--------|------40

2-------------|-------25--------|------55

当我在第一个组合 2 中选择时,在第二个组合中我需要两个选择 100 和 25。

当我在第二个组合中选择100时,在第三个组合中我需要一个选择35。

当我在第二个组合中选择 25 时,在第三个组合中我需要三个选择 35、40 和 55。

在我的数据库中,我仅使用排除的变体 ID 序列化了数组

a:3:{i:2;s:1:"3";i:3;s:1:"8";i:4;s:2:"10";}
a:3:{i:2;s:1:"3";i:3;s:1:"8";i:4;s:2:"11";}
a:3:{i:2;s:1:"3";i:3;s:1:"8";i:4;s:2:"12";}

如果反序列化这个数组我会得到

array (size=3)
2 => string '3' (length=1)
3 => string '8' (length=1)
4 => string '10' (length=2)
array (size=3)
2 => string '3' (length=1)
3 => string '8' (length=1)
4 => string '11' (length=2)
array (size=3)
2 => string '3' (length=1)
3 => string '8' (length=1)
4 => string '12' (length=2)

请帮我写代码

我试过自己写,没用:(

显然我是一名编码员而不是程序员 :(

这是我的尝试

<?php
function _uc_dependent_attributes_get_dependency_for_node()
{
$nid = 61;
if(!$nid)
return;

$result = db_query("SELECT combination FROM {uc_dependent_attributes} WHERE nid = :nid", array(':nid' => $nid));
$combos = array();
foreach($result as $record)
{
$combos[] = unserialize($record->combination);
}

$attributes = node_load($nid)->attributes;
$attrs = array();
foreach($attributes as $attr)
{
$attrs['a' . $attr->aid] = array();
$attrs['a' . $attr->aid]['name'] = $attr->name;
$attrs['a' . $attr->aid]['aid'] = $attr->aid;
if(!empty($attr->options))
{
foreach($attr->options as $option)
{
$attrs['a' . $attr->aid]['options']['o' . $option->oid] = $option->name;
}
}
}
ttt($attrs, $combos, array(
'attr_2' => 2,
'opt_4' => 4,
));
}

function ttt($attrs, $combos, $selections = array())
{
var_dump($attrs);
var_dump($combos);

foreach($combos as $combo)
{
foreach($combo as $c_aid => $c_oid)
{
foreach($attrs as $a_aid => $attr)
{
if('a' . $c_aid == $a_aid)
{
// var_dump($attrs[$a_aid]['options']['o' . $c_oid]);
// if(isset($attrs[$a_aid]['options']['o' . $c_oid]) && 'o' . $c_oid != 'o' . $opt_id)
// unset($attrs[$a_aid]['options']['o' . $c_oid]);
}
}
}
}

echo '<table border="1"><tr>';
foreach($attrs as $attr)
{
$sa = false;
foreach($selections as $key_s => $selected)
{
if(strstr($key_s, 'attr_') !== false && $selected == $attr['aid'])
{
$sa = true;
break;
}
}
echo '<td>' . ($sa ? '<strong>' : '') . '(' . $attr['aid'] . ')' . $attr['name'] . ($sa ? '</strong>' : '') . '</td>';
}
echo '</tr><tr>';
foreach($attrs as $attr)
{
echo '<td>';
foreach($attr['options'] as $key => $opt)
{
$so = false;
foreach($selections as $key_s => $selected)
{
if(strstr($key_s, 'opt_') !== false && 'o' . $selected == $key)
{
$so = true;
break;
}
}
echo ($so ? '<strong>' : '') . "($key) " . $opt . ($so ? '</strong>' : '') . '<br>';
}
echo '</td>';
}
echo '</tr></table>';
}
?>

最佳答案

如果您想在客户端实现这一点:请参阅 http://code.google.com/p/jquery-option-tree/如果你想在服务器端实现这一点,你将需要一段 JavaScript,每次用户在组合框中选择一个项目时都会提交一个表单,然后在服务器端,你相应地过滤每个组合框。我认为第一个解决方案是最简单和最优雅的。不过,您将需要使用 JQuery 库。参见 http://kotowicz.net/jquery-option-tree/demo/demo.html用于演示。

关于php - 排除选择的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18872800/

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