php - 从 2 个数据库查询和 2 个循环加载多维数组

标签 php mysql arrays multidimensional-array

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

我尝试像这样输出数据:

RepName1 RepName2 RepName3 RepName4
(代表姓名来自数据库,永远不会是固定数字)

在该列中的每个代表姓名下方,按降序排列前十种畅销商品

像这样:

代表姓名1

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

第一个畅销商品

第二件畅销商品

第三件畅销商品

第四款畅销商品

第 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

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

提前致谢

最佳答案

您正在覆盖 $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>

关于php - 从 2 个数据库查询和 2 个循环加载多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43902513/

相关文章:

php - Twitter Bootstrap/Wordpress 集成的布局问题

mysql - 对 MySQL 中特定字段中具有相同值的行进行分组

ios - 无法将类型 '__NSArrayI' (0x10df73c08) 的值转换为 'NSDictionary' (0x10df74108)

mysql - 请求 "DELETE"未由应用程序注册

arrays - 在参数请求中传递字符串数组 - swift 5

javascript - 计算循环执行了多少次

php - br 标签的 str_replace 不起作用

javascript - 可以在 init.js 中编写 mysql 查询

php - 在 PHP 中调整 PNG 图像的大小

c# - mysql外键错误