从数组创建嵌套列表 - Codeigniter,PHP,MySQL

时间:2012-04-02 19:29:03

标签: php mysql codeigniter

好的长时间编码器,第一次卡住(我想是因为没有看到穿过树林的树林!)

我是如何在数组中返回数据的。 预告出其中一个数据字段是部件号。

作为一个例子,数据集看起来像这样(我已经删除了一些重复)

[1] => Array
    (
        [id] => 1628
        [partnum] => 02030200104bla
        [cat] => PPE
        [class] => FOOT PROTECTION
        [subcat] => BOOTS
        [name] => BOOT SS400 4 BLACK D RING CHUKKA PAD COLLAR PUDD SOLE STEEL TOEandMID S1P STES
        [is_over] => 0
        [price] => 1
        [image] => 1717-1727
        [location] => 
        [purchase] => 1
        [hire] => 0
        [hide] => 0
        [hasother] => 0
        [name_soundex] => 
        [options] => 
        [ass_prod] => 
    )

[2] => Array
    (
        [id] => 1629
        [partnum] => 02030200105bla
        [cat] => PPE
        [class] => FOOT PROTECTION
        [subcat] => BOOTS
        [name] => BOOT SS400 5 BLACK D RING CHUKKA PAD COLLAR PUDD SOLE STEEL TOEandMID S1P STES
        [is_over] => 0
        [price] => 1
        [image] => 1717-1727
        [location] => 
        [purchase] => 1
        [hire] => 0
        [hide] => 0
        [hasother] => 0
        [name_soundex] => 
        [options] => 
        [ass_prod] => 
    )

[3] => Array
    (
        [id] => 1630
        [partnum] => 02030200106bla
        [cat] => PPE
        [class] => FOOT PROTECTION
        [subcat] => BOOTS
        [name] => BOOT SS400 6 BLACK D RING CHUKKA PAD COLLAR PUDD SOLE STEEL TOEandMID S1P STES
        [is_over] => 0
        [price] => 1
        [image] => 1717-1727
        [location] => 
        [purchase] => 1
        [hire] => 0
        [hide] => 0
        [hasother] => 0
        [name_soundex] => 
        [options] => 
        [ass_prod] => 
    )


[11] => Array
    (
        [id] => 1638
        [partnum] => 02030200205bla
        [cat] => PPE
        [class] => FOOT PROTECTION
        [subcat] => BOOTS
        [name] => BOOT SS609 5 BLACK PAD COLLAR/TONGUE PU DD SOLE STEEL TOECAP and MIDSOLE S3 STES
        [is_over] => 0
        [price] => 1
        [image] => 1728-1735
        [location] => C36
        [purchase] => 1
        [hire] => 0
        [hide] => 0
        [hasother] => 0
        [name_soundex] => 
        [options] => 5 6 7 8 9 10 11 12
        [ass_prod] => 
    )

[12] => Array
    (
        [id] => 1639
        [partnum] => 02030200206bla
        [cat] => PPE
        [class] => FOOT PROTECTION
        [subcat] => BOOTS
        [name] => BOOT SS609 6 BLACK PAD COLLAR/TONGUE PU DD SOLE STEEL TOECAP and MIDSOLE S3 STES
        [is_over] => 0
        [price] => 1
        [image] => 1728-1735
        [location] => C36
        [purchase] => 1
        [hire] => 0
        [hide] => 0
        [hasother] => 0
        [name_soundex] => 
        [options] => 
        [ass_prod] => 
    )

[13] => Array
    (
        [id] => 1640
        [partnum] => 02030200207bla
        [cat] => PPE
        [class] => FOOT PROTECTION
        [subcat] => BOOTS
        [name] => BOOT SS609 7 BLACK PAD COLLAR/TONGUE PU DD SOLE STEEL TOECAP and MIDSOLE S3 STES
        [is_over] => 0
        [price] => 1
        [image] => 1728-1735
        [location] => C36
        [purchase] => 1
        [hire] => 0
        [hide] => 0
        [hasother] => 0
        [name_soundex] => 
        [options] => 
        [ass_prod] => 
    )

[14] => Array
    (
        [id] => 1641
        [partnum] => 02030200208bla
        [cat] => PPE
        [class] => FOOT PROTECTION
        [subcat] => BOOTS
        [name] => BOOT SS609 8 BLACK PAD COLLAR/TONGUE PU DD SOLE STEEL TOECAP and MIDSOLE S3 STES
        [is_over] => 0
        [price] => 1
        [image] => 1728-1735
        [location] => C36
        [purchase] => 1
        [hire] => 0
        [hide] => 0
        [hasother] => 0
        [name_soundex] => 
        [options] => 
        [ass_prod] => 
    )

[13] => Array
    (
        [id] => 1640
        [partnum] => 02030200207bla
        [cat] => PPE
        [class] => FOOT PROTECTION
        [subcat] => BOOTS
        [name] => BOOT SS609 7 BLACK PAD COLLAR/TONGUE PU DD SOLE STEEL TOECAP and MIDSOLE S3 STES
        [is_over] => 0
        [price] => 1
        [image] => 1728-1735
        [location] => C36
        [purchase] => 1
        [hire] => 0
        [hide] => 0
        [hasother] => 0
        [name_soundex] => 
        [options] => 
        [ass_prod] => 
    )

[15] => Array
    (
        [id] => 1642
        [partnum] => 02030200108yel
        [cat] => PPE
        [class] => FOOT PROTECTION
        [subcat] => BOOTS
        [name] => BOOT SS400 8 YELLOW D RING CHUKKA PAD COLLAR PUDD SOLE STEEL TOEandMID S1P STES
        [is_over] => 0
        [price] => 1
        [image] => 1728-1735
        [location] => C36
        [purchase] => 1
        [hire] => 0
        [hide] => 0
        [hasother] => 0
        [name_soundex] => 
        [options] => 
        [ass_prod] => 
    )

前9个字符是产品代码,所以

$thisval = substr($t['partnum'], 0, 9);

,接下来的两个字符是

$thissize = substr($t['partnum'], 9, 2);

,最后3个字符是颜色

$thissize = substr($t['partnum'], 11, 3);

我需要做的就是 如果前9个字符匹配,则为前9个匹配的最后2个字符中的每个字符创建一个选择,因此它们都显示在下拉列表中作为可选大小 它需要最后3个字符并使它们成为可选择的颜色

如果出现新的9个字符,则需要该字符并启动一个新字符。 。 。 ?

所以你最终会像在商店里一样,带有一个项目,一个颜色和一个尺寸选择器

从以上数据 我正在寻找2件物品,一件带有尺寸选择器,另一件带有黑色和黄色 还有一个尺寸选择器和一个黑色的颜色选择器。

任何帮助都会很棒!

我认为它应该像

一样工作
If(is_array($products)){
$check="";
foreach($products as $key => $p){
$substring1 = substr($p['partnum'], 9,2);
$nexttwo = substr($p['partnum'],9,2);
If($check == $substring1){

}else{
If(is_array($products){
Foreach($products as $key => $t){
$substring2 = substr($t['partnum'], 9,2);
$nexttwo2 = substr($t['partnum'],9,2);
If($substring1 != $substring2){
Echo "<li>" . $p['itemname'] . "<ul>";
}else{
If($substring1 == $substring2){
Echo "<li>". $nexttwo2."</li>;
}else{
Echo "</ul>";
}
}
</li>
$check = $substring1;
}
}
}

1 个答案:

答案 0 :(得分:0)

$check = '';
$colouris = "";
$sizeis = "";
$first = true;
if(is_array($products)){ 
foreach($products as $key => $p){
    $substring1 = substr($p['partnum'], 0,9);
    $sizeis = substr($p['partnum'],9,4);
    $colouris = substr($p['partnum'],13,2);
if ($substring1 != $check) {

        if (!$first) {
            echo '</select></label>';
        }
        echo '<label>' . $substring1 . '<select>';

        $check = $substring1;
    }
    if(!$sizeis){
        echo '<option>No Options</option>';
    }
    else{


        echo '<option>' . $sizeof . " " . $colourof  .'</option>';
        $first = false;
        $colouris = "";
        $sizeis = "";
    }


 }
}

变量的大小和颜色是从substr

上的一些简单的if,elses设置的
if($sizeis === "28SH"){
        $sizeof = "28 Short";
    }
    elseif($sizeis === "30SH"){
        $sizeof = "30 Short";
    }
if($colouris == "YE"){
        $colourof = "Yellow";
    }
    elseif($colouris == "OR"){
        $colourof = "Orange";
    }

e.t.c。 e.t.c。

这会让我而不是页面上的24个项目,我将在下拉列表中有4个项目,其中包含6个选项。 选择选项是

Medium Blue
Medium Yellow
Medium Green
2XL Orange
2XL Yellow
2XL Green

我想对选择的构建器进行修改,以构建2个选择,但是现在我已将它们全部整合在一起