我如何以正确的格式获取这些数据?

发布于 2024-11-04 23:44:40 字数 2829 浏览 0 评论 0原文

好的,我有这个查询

    select ss.system_step_id, ss.step_number, cd.name, ssp.system_step_product_id, p.cost, ssp.class_id from system_step ss 
    join system as s on s.system_id=ss.system_id 
    join category_description as cd on cd.category_id=ss.sub_category_id 
    join system_step_product as ssp on ss.system_step_id=ssp.system_step_id 
    join product as p on p.product_id=ssp.product_id where s.system_id = 41 
    order by ss.step_number, ssp.class_id;

which yields this result

7   1   Screens         808 115.0000 1
7   1   Screens         809 381.9000 2
7   1   Screens         810 441.9000 3
8   2   Printers        811 112.3200 1
8   2   Printers        812 201.0400 2
8   2   Printers        813 202.8700 3
9   3   Cash Drawers    814 135.7000 1
9   3   Cash Drawers    815 86.5400  2
9   3   Cash Drawers    816 135.7000 3

有没有办法将其转换为包含三个元素的 php 数组,如下所示

Array
(
    [0] => Array
        (
            [name] => "Screens"
            [standard_product] => Array ([id] => 808, [price] => '115.0000')
            [business_product] => Array ([id] => 809, [price] => '381.9000')
            [premium_product] => Array ([id] => 810, [price] => '441.9000') 
        )                      

    [1] => Array               
        (                      
          [name] => "Printers"
          [standard_product] => Array ([id] => 811, [price] => '112.3200')
          [business_product] => Array ([id] => 812, [price] => '201.0400')
          [premium_product] => Array ([id] => 813, [price] => '202.8700')
        )
    [2] => Array               
        (                      
          [name] => "Cash Drawers"
          [standard_product] => Array ([id] => 814, [price] => '135.7000')
          [business_product] => Array ([id] => 815, [price] => '86.5400')
          [premium_product] => Array ([id] => 816, [price] => '135.7000')
        )
)


$sql = "select ss.system_step_id, ss.step_number, cd.name, ssp.system_step_product_id, p.cost, ssp.class_id, pd.name as product_name, pd.description from system_step ss join system as s on s.system_id=ss.system_id join category_description as cd on cd.category_id=ss.sub_category_id join system_step_product as ssp on ss.system_step_id=ssp.system_step_id join product as p on p.product_id=ssp.product_id join product_description as pd on pd.product_id=p.product_id where s.system_id = {$system['system_id']} order by ss.step_number, ssp.class_id;";
$result = mysql_query($sql);
$steps = array();
while($row_r = mysql_fetch_assoc($result)){
  $steps[] = $row_r;
}

,步骤是包含 9 个元素的完整数组

正如您所看到的,唯一值得注意的是 class_id 1 是 standard_product class_id 2 是Business_product 和 class_id 3 是 premium_product

ok so i have this query

    select ss.system_step_id, ss.step_number, cd.name, ssp.system_step_product_id, p.cost, ssp.class_id from system_step ss 
    join system as s on s.system_id=ss.system_id 
    join category_description as cd on cd.category_id=ss.sub_category_id 
    join system_step_product as ssp on ss.system_step_id=ssp.system_step_id 
    join product as p on p.product_id=ssp.product_id where s.system_id = 41 
    order by ss.step_number, ssp.class_id;

which yields this result

7   1   Screens         808 115.0000 1
7   1   Screens         809 381.9000 2
7   1   Screens         810 441.9000 3
8   2   Printers        811 112.3200 1
8   2   Printers        812 201.0400 2
8   2   Printers        813 202.8700 3
9   3   Cash Drawers    814 135.7000 1
9   3   Cash Drawers    815 86.5400  2
9   3   Cash Drawers    816 135.7000 3

Is there a way to turn this into a php array of three elements like this

Array
(
    [0] => Array
        (
            [name] => "Screens"
            [standard_product] => Array ([id] => 808, [price] => '115.0000')
            [business_product] => Array ([id] => 809, [price] => '381.9000')
            [premium_product] => Array ([id] => 810, [price] => '441.9000') 
        )                      

    [1] => Array               
        (                      
          [name] => "Printers"
          [standard_product] => Array ([id] => 811, [price] => '112.3200')
          [business_product] => Array ([id] => 812, [price] => '201.0400')
          [premium_product] => Array ([id] => 813, [price] => '202.8700')
        )
    [2] => Array               
        (                      
          [name] => "Cash Drawers"
          [standard_product] => Array ([id] => 814, [price] => '135.7000')
          [business_product] => Array ([id] => 815, [price] => '86.5400')
          [premium_product] => Array ([id] => 816, [price] => '135.7000')
        )
)


$sql = "select ss.system_step_id, ss.step_number, cd.name, ssp.system_step_product_id, p.cost, ssp.class_id, pd.name as product_name, pd.description from system_step ss join system as s on s.system_id=ss.system_id join category_description as cd on cd.category_id=ss.sub_category_id join system_step_product as ssp on ss.system_step_id=ssp.system_step_id join product as p on p.product_id=ssp.product_id join product_description as pd on pd.product_id=p.product_id where s.system_id = {$system['system_id']} order by ss.step_number, ssp.class_id;";
$result = mysql_query($sql);
$steps = array();
while($row_r = mysql_fetch_assoc($result)){
  $steps[] = $row_r;
}

so steps is the the full array with 9 elements

As you can see the only thing of note is the class_id 1 is standard_product class_id 2 is business_product and class_id 3 is premium_product

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱你不解释 2024-11-11 23:44:40

您可以在获取 SQL 结果时获取填充多维数组,或者拆分为多个 SQL 命令,从而构建数组(首先获取名称,然后获取具有每个名称值的产品)。

我不知道你更喜欢哪一个,但由于你有一个大的连接分割,就代码的可读性而言,SQL 可能不是最差的。性能影响可能会有所不同。

You can fetch fill a multi-dimensional array while fetching the SQL result or split into several SQL commands and therefore build up your array (1st fetching the names, than the products with values of every name).

I do not know which you prefer but as you have a large join splitting the SQL might not be the worst in terms of readability of your code. Performance impact may vary.

岛歌少女 2024-11-11 23:44:40

我想这样就可以了,

<?php

    $finalArray = array();
    $nameArray = array('Screens', 'Printers', 'Cash Drawers');

    foreach ($nameArray as $name) {

        while ($row = mysql_fetch_assoc($result)) {

            if ($row['name'] == $name) {

                if ($row['class_id'] == 1) {

                    $standard = array('id' => $row['system_step_product_id'], 'price' => $row['cost']);
                }
                else if ($row['class_id'] == 2) {

                    $business = array('id' => $row['system_step_product_id'], 'price' => $row['cost']);
                }
                else if ($row['class_id'] == 3) {

                    $premium = array('id' => $row['system_step_product_id'], 'price' => $row['cost']);
                }
            }
        }

        array_push($finalArray, array('name' => $name, 'standard_product' => $standard, 'business_product' => $business, 'premium_product' => $premium,));
    }

?>

希望我把所有的列名都写对了。

这就是我测试的结果,我得到了正确的输出 http://codepad.org/yjU3gxbB

您可能需要更改的是动态创建名称数组。

    while ($row = mysql_fetch_assoc($result)) {

        array_push($nameArray, $row['name']);
    }

    $nameArray = array_unique($nameArray);

但是,当您对同一个查询执行两次 mysql_fetch_assoc 时,PHP 不喜欢它。您可能想要执行另一个查询,其中仅选择 cd.name

Methinks this will do it

<?php

    $finalArray = array();
    $nameArray = array('Screens', 'Printers', 'Cash Drawers');

    foreach ($nameArray as $name) {

        while ($row = mysql_fetch_assoc($result)) {

            if ($row['name'] == $name) {

                if ($row['class_id'] == 1) {

                    $standard = array('id' => $row['system_step_product_id'], 'price' => $row['cost']);
                }
                else if ($row['class_id'] == 2) {

                    $business = array('id' => $row['system_step_product_id'], 'price' => $row['cost']);
                }
                else if ($row['class_id'] == 3) {

                    $premium = array('id' => $row['system_step_product_id'], 'price' => $row['cost']);
                }
            }
        }

        array_push($finalArray, array('name' => $name, 'standard_product' => $standard, 'business_product' => $business, 'premium_product' => $premium,));
    }

?>

Hopefully I got all the column names right.

This is what I tested it off of and I get the correct output http://codepad.org/yjU3gxbB

The only thing you may what to change is creating the name array dynamically

    while ($row = mysql_fetch_assoc($result)) {

        array_push($nameArray, $row['name']);
    }

    $nameArray = array_unique($nameArray);

However, PHP doesn't like it when you do a mysql_fetch_assoc twice on the same query. You might want to do another query where you only select the cd.name

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文