douwen5951 2012-04-02 19:29
浏览 32

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

Ok long time coder, first time stuck ( I think due to not seeing the woods through the trees!)

any how I have data being returned in an array. When foreached out one of the data fields is the part number.

As an example the data set looks like this (I've removed some as it repeats)

[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] => 
    )

)

The first 9 characters are the product code so i do

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

and the next 2 characters are the size

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

and the last 3 characters are the colour

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

All i need to do is if the first 9 characters match, create a select for each of the last 2 characters where the first nine match, so they all appear in the dropdown as a selectable size and it takes the last 3 characters and makes them selectable colours

If a new 9 characters comes along, it takes that and starts a new one . . . ?

So you would end up like you would on a store with an item and a colour and a size selector

From the above data I'd be looking for 2 items, one with a size selector and a colour selector with black and yellow and one with a size selector and a colour selector of just black.

Any help would be great!

I'm thinking it should work like

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条回答 默认 最新

  • doumo1807831 2012-04-03 19:06
    关注
    $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 = "";
        }
    
    
     }
    }
    

    The size of and colour of variables are set from some simple if,elses on the substr

    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.

    This gets me instead of 24 items on a page, I'll have 4 items with 6 options in a drop down. The select options would be

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

    I'd like to flesh the select builder up, to build 2 selects, but I have it all in one for now

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行