本文介绍了PHP/MySQL多维/关联数组2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用了近一个星期的代码,似乎无法摆脱它.我正在从看起来像这样的数据库中获取行:

I've been on this code for almost a week and can't seem to get out of it.I'm fetching rows from a database that looks like this:

id |    帐户    |    内容    |    类型    |    数量
1      23494               E             382       23494              E                     nbsp; b ;  5
3       23494               F                      b   7
4       23494                          b&nsp;  b ;                           2

id |    account    |    content     |    type     |    quantity
1       23494               E                 38                  10
2       23494               E                 13                  5
3       23494               F                 38                  7
4       23494               E                 6                    2

到目前为止,我的陈述是这样的:

My Statement looks like this thus far:

// Make the query:
$qu = "SELECT * FROM log WHERE account='".$account."' ORDER BY timelog DESC $pages->limit"; 
$re = @mysqli_query ($dbc, $qu); // Run the query.

$roarr = array();
while ($row = mysqli_fetch_array($re, MYSQLI_ASSOC)) {
    $roarr[$row['account']][$row['content']][$row['type']] = $row['quantity'];

} // End of WHILE loop.
//print_r($roarr);

echo'
<tr>&nbsp;</tr>
<tr>
    <td align="left"><b>Account</b></td>
    <td align="left"><b>6Kg(E)</b></td>
    <td align="left"><b>13Kg(E)</b></td>
    <td align="left"><b>38Kg(E)</b></td>

    <td align="left"><b>6Kg(F)</b></td>
    <td align="left"><b>13Kg(F)</b></td>
    <td align="left"><b>38Kg(F)</b></td>
    <td align="left"><b>Date Created</b></td>
</tr>
';

$dataarr = array();
foreach($roarr as $account => $dataarr) {
    $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');

    echo '
      <tr bgcolor="' . $bg . '">
        <td align="left">'. $account .'</td>
     ';

    foreach($dataarr as $content => $data) {
         if($content == "E") {
              echo '<td align="left">', (isset($data['6']) ? $data['6'] : '&nbsp;'), '</td>';
               echo '<td align="left">', (isset($data['13']) ? $data['13'] : '&nbsp;'), '</td>';
               echo '<td align="left">', (isset($data['38']) ? $data['38'] : '&nbsp;'), '</td>';
         } // Then I GET STUCK on the Else Part! HOW Do I display $content == "F" data on the right hand side?
    } //End Foreach
} //End Foreach

这就是我打算显示我的结果的方式:

This is how I intend to display my results :

帐户| 6KG(E)| 13KG(E)| 38KG(E)| 6KG(楼)| 13KG(楼)| 38KG(楼)|日期

Account | 6KG(E) | 13KG(E) | 38KG(E) | 6KG(F) | 13KG(F) | 38KG(F) | Date

23494              nbsp; bsp 5            10   b&b ;              b           7

23494                        5               10             2                               7

,但是我很困.

问题:如何安排以这种格式获取的结果?

Question:How can I arrange the results fetched in such a format?

我已更改数据库,以便将重点更多地放在该问题上:由于查询是按每个帐户提取的,因此只有一个帐户具有上面显示的行,如上面的数据库所示.

I've Changed the database so as to focus more on the problem: Since the query fetches per account, there is only one account with rows shown as in the database above.

这是$ roarr产生的数组:

This is the array produced by $roarr:

Array([23494] => Array([F] => Array([6] => 2 [38] => 7)[E] => Array([38] => 10 [13] => 5)))

Array ( [23494] => Array ( [F] => Array ( [6] => 2 [38] => 7 ) [E] => Array ( [38] => 10 [13] => 5 ) ) )

先谢谢您

推荐答案

您需要类似的东西,而不是内部的foreach循环:

You need something like that instead of your internal foreach loop:

echo '<td align="left">', (isset($dataarr['E']['6']) ? $dataarr['E']['6'] : '&nbsp;'), '</td>';
echo '<td align="left">', (isset($dataarr['E']['13']) ? $dataarr['E']['13'] : '&nbsp;'), '</td>';
echo '<td align="left">', (isset($dataarr['E']['38']) ? $dataarr['E']['38'] : '&nbsp;'), '</td>';
echo '<td align="left">', (isset($dataarr['F']['6']) ? $dataarr['F']['6'] : '&nbsp;'), '</td>';
echo '<td align="left">', (isset($dataarr['F']['13']) ? $dataarr['F']['13'] : '&nbsp;'), '</td>';
echo '<td align="left">', (isset($dataarr['F']['38']) ? $dataarr['F']['38'] : '&nbsp;'), '</td>';

这篇关于PHP/MySQL多维/关联数组2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 06:32