【问题标题】:Load a multidimensional array from 2 database queries and 2 loops从 2 个数据库查询和 2 个循环加载多维数组
【发布时间】:2017-10-09 16:40:24
【问题描述】:

我尝试获取一行代表名称作为列标题,然后在该标题下每个代表的前十个销售项目

我试图让数据像这样输出:

代表姓名1 代表姓名2 代表姓名3 代表姓名4
(代表名称来自数据库,永远不会是固定数字)

在列中每个销售代表姓名的下方,按降序排列他们最畅销的十件商品

像这样:

RepName1

(将从数据库中提取最畅销的信息)

第一个销售项目

第二个卖点

第三个卖点

第四个卖点

第 5 个卖点

第 6 个卖点

第 7 个卖点

第 8 个卖点

第 9 件商品

第 10 件商品

然后开始一个新的专栏,其中包含下一位代表和他或她的前十名卖家。

    $qry = "SELECT sales_rep_login FROM sales_reps WHERE status = 'A'";
    $return_reps = mysql_query($qry);
        $reps = array();
        while($repX = mysql_fetch_assoc($return_reps)) {
            $reps[] = $repX['sales_rep_login'];
        }

    foreach ($reps as $rep){
    $q="SELECT sales_quantity.style_num, sales_quantity.number_sold, sales_reps.fname, sales_reps.lname FROM catalog JOIN sales_quantity ON catalog.style_num = sales_quantity.style_num
        JOIN sales_reps ON sales_quantity.sales_rep_login = sales_reps.sales_rep_login
        WHERE catalog.status = 'A'
        AND catalog.season_id = '" . $_POST['season'] . "'
        AND sales_quantity.sales_rep_login = '" .$rep. "'
        AND sales_reps.status = 'A'
        ORDER BY sales_quantity.number_sold DESC LIMIT 10";
        $return_sales = mysql_query($q);
        $Total_Sales = array();
        while($Sales=mysql_fetch_assoc($return_sales)) 
        $Total_Sales[] = $Sales;
        } // end foreach

出于某种原因,我只获得了最后一次代表的信息。

提前致谢

【问题讨论】:

  • 只是一个建议:我的项目很容易维护,你真的应该迁移到 PDO 模块,因为 mysql_* 函数从 php5.5 开始被弃用并从 php7.0 中删除。
  • 第 1 步是停止使用已弃用的 API。第 2 步是将这两个查询减少到只有 1 个。然后我们可以讨论显示。第 2 步,请参阅meta.stackoverflow.com/questions/333952/…
  • 哦,在处理第 2 步时假装它是 LIMIT 3

标签: php mysql arrays multidimensional-array


【解决方案1】:

您正在覆盖 $total_Sales 数组。在 foreach 之前定义它。

$Total_Sales = array();  // define array here
foreach ($reps as $rep){
    $q="SELECT sales_quantity.style_num, sales_quantity.number_sold, sales_reps.fname, sales_reps.lname FROM catalog JOIN sales_quantity ON catalog.style_num = sales_quantity.style_num
        JOIN sales_reps ON sales_quantity.sales_rep_login = sales_reps.sales_rep_login
        WHERE catalog.status = 'A'
        AND catalog.season_id = '" . $_POST['season'] . "'
        AND sales_quantity.sales_rep_login = '" .$rep. "'
        AND sales_reps.status = 'A'
        ORDER BY sales_quantity.number_sold DESC LIMIT 10";
        $return_sales = mysql_query($q);

        while($Sales=mysql_fetch_assoc($return_sales)) 
        $Total_Sales[$rep][] = $Sales; // make 2 dimensional array
        }

打印 $Total_Sales 并查看它是否包含所有值。

使用 mysqli 代替已被贬值的 mysql。

将结果数组视为

$Total_Sales = array
(
    'RepName1' => array(
            array('names' => '1st selling item','style' => 'DD39050BG','num' => '660'),
            array('names' => '2nd selling item','style' => 'DD39043','num' => '600'),
            array('names' => '3rd selling item','style' => 'DD79021','num' => '582')
        ),
    'RepName2' => array(
            array('names' => '1st selling item','style' => 'DD39050BG','num' => '660'),
            array('names' => '2nd selling item','style' => 'DD39043','num' => '600'),
            array('names' => '3rd selling item','style' => 'DD79021','num' => '582')
        ),

    'RepName3' => array(
            array('names' => '1st selling item','style' => 'DD39050BG','num' => '660'),
            array('names' => '2nd selling item','style' => 'DD39043','num' => '600'),
            array('names' => '3rd selling item','style' => 'DD79021','num' => '582')
        ),

);

使用以下代码显示

<table cellpadding="5" cellspacing="5" border="1 sold #ccc">
    <tr>
    <?php foreach($Total_Sales as $key=>$value){
        echo "<td>".$key;
        if(is_array($value) && count($value)>0){
            echo "<table>";
            foreach($value as $k=>$v){
                echo "<tr><td>".$v['names']."</td></tr>";
            }
            echo "</table>";
        }
        echo "</td>";
    }?>
    </tr>
</table>

【讨论】:

  • 我按照建议进行了更改。打印它并且仍然有一个空数组。应该有什么东西放在空括号里。 $Total_Sales[$rep][] = $Sales;
  • 已弃用。这一事实可能会也可能不会对其价值产生不利影响。
  • 所以我现在已经改成mysqli我现在在数组中得到了想要的结果,但是我需要知道如何显示它。
  • Array ([0] => Array ([names] => Steven Handmaker [style] => DD39050BG [num] => 660) [1] => Array ([names] => Steven Handmaker [style] => DD39043 [num] => 600 ) [2] => Array ( [names] => Steven Handmaker [style] => DD79021 [num] => 582 ) )是 119 个数组
  • 我猜阵列中的“Steven Handmaker”正在销售商品。我说的对吗?
猜你喜欢
  • 2017-07-01
  • 1970-01-01
  • 2018-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多