简体   繁体   English

在基于同一制造商的PHP中合并MySQL结果

[英]Combining MySQL results in PHP based on same Manufacturer

I'm i've been working on an e-commerce site www.lady-elegance.co.uk that its main product is shoes. 我一直在一个电子商务网站www.lady-elegance.co.uk上工作,其主要产品是鞋子。 The site works well displaying all the products within categories etc. but what I want to do is be able to have each shoe in each size as a separate product in the back end for keeping a record of stock levels. 该站点可以很好地显示类别等中的所有产品,但是我想要做的是能够将每种尺码的每种鞋子作为单独的产品放在后端,以保持库存水平的记录。

For example: "shoe 1" in size 10 has ID 00001 and "shoe 1" in size 11 has ID 00002 and so on. 例如:大小为10的“鞋子1”的ID为00001,大小为11的“鞋子1”的ID为00002,依此类推。

Creating each product is easy enough but what I want is to be able to display the "shoe 1" once with a drop down with each of the sizes that correspond to each of the product ID's, displaying only those size that are in stock. 创建每个产品非常容易,但是我想要的是能够一次显示“鞋子1”,并带有与每个产品ID对应的每个尺寸的下拉菜单,仅显示库存的那些尺寸。

Each shoe type will obviously have its own manufacturers product ID which I can use to identify the group but how to I group the same ones together. 显然,每种鞋子类型都有其自己的制造商产品ID,我可以使用它来标识组,但如何将相同的组在一起。

I cant get my head around the logic for it. 我无法理解它的逻辑。 Could some one please help! 可以请人帮忙!

I have found this answer that appears similar but need some help understanding it ( Click Here ) 我发现这个答案看起来很相似,但需要一些帮助来理解它( 单击此处

I don't know exactly what your database looks like, but you could do something like this: 我不知道您的数据库到底是什么样子,但是您可以执行以下操作:

function getShoes($manufacturer, $name=''){
    $manufacturer = mysqli_real_escape_string($manufacturer);
    $sql = 'SELECT id, name, size FROM shoes WHERE manufacturer = "'.$manufacturer'" AND in_stock > 0 ';
    if($name){
        $name = mysqli_real_escape_string($name);
        $sql .= 'AND name = "'.$name.'" ';
    }
    $sql .= 'ORDER BY name, size';
    $result = mysqli_query($sql);  //Results like this: id:00001, name:shoe1, size:10
    while($row = mysqli_fetch_array($result)){       // id:00002, name:shoe1, size:11
        $rows[$row['name']][] = $row;  //group them by name (assuming you have more
    }                                  //than one shoe type per manufacturer)

    foreach($rows as $name=>$shoes){
        echo('<select name="'.$name.'">');
        foreach($shoes as $shoe){
            echo('<option value="'.$shoe['id'].'">'.$shoe['name'].' - '.$shoe['size'].'</option>');
        }
        echo('</select>');
    }
}

Now in your HTML, if you called this function, you would get all shoes made by the manufacturer you designate. 现在,在HTML中,如果调用此函数,则可以得到指定制造商生产的所有鞋子。 For example, getShoes('nike'); 例如, getShoes('nike'); would get all shoes manufactured by Nike and give you select boxes for them all. 将获得耐克生产的所有鞋子,并为您提供所有选择框。 You might want to make it a bit more specific, because you don't want selection options for every shoe made by Nike on a page setup for Shoe1 made by Nike. 您可能需要使其更具体一些,因为您不希望在Nike制造的Shoe1的页面设置中为Nike制造的每双鞋子选择选项。 So then you could do getShoes('nike', 'shoe1'); 因此,您可以执行getShoes('nike', 'shoe1'); and that would give you only the select box for Shoe1 from Nike. 那只会给您Nike的Shoe1的选择框。 Hope this helps. 希望这可以帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM