dongwo5449 2014-08-07 12:23
浏览 41
已采纳

如何只显示具有匹配数组键的数组值?

I am trying to construct a table which shows price on product, but I cannot figure out how to match the data based on the key values.

I query the SQL db and get the following result. Date is actually the month

Product name  Price   date
care bear      11       4
care bear      12.5     5
care bear      9        6
car            123      3
car            344      4

.etc

The sql result is read to an array. Similar to table example.

Now I am trying to make a table that shows the name and price for the whole year, but I am stuck how to do this. The idea is the following.

    Product   jan feb mar apr may jun jul aug oct nov dec
    care bear  0   0   0  11  12.5  9  0   0   0   0   0
    car        0   0   3  4    0    0  0   0   0   0   0


// while there are products
    while (!empty($results[$k]['product'])) 
    {
// fill first cell with name.
echo '<td>'.$results[$k]['product'].'</td>';
// while the product has the same name
    while ($results[$i]['product'] = $results[$i-1]['product'] )
    {
//generate values for each month
        while ($i< 12) {
            if ($results[0]['month'] == $i){echo '<td>'.$results[0]['price'].'</td>';} 
                else if ($results[1]['month'] == $i){echo '<td>'.$results[1]['price'].'</td>';} 
                    else if ($results[2]['month'] == $i){echo '<td>'.$results[2]['price'].'</td>';}
                        else if ($results[3]['month'] == $i){echo '<td>'.$results[3]['price'].'</td>';}
                            else if ($results[4]['month'] == $i){echo '<td>'.$results[4]['price'].'</td>';}
                                else if ($results[5]['month'] == $i){echo '<td>'.$results[5]['price'].'</td>';}
                                    else if ($results[6]['month'] == $i){echo '<td>'.$results[6]['price'].'</td>';}
                                        else if ($results[7]['month'] == $i){echo '<td>'.$results[7]['price'].'</td>';}
                                            else if ($results[8]['month'] == $i){echo '<td>'.$results[8]['price'].'</td>';}
                                                else if ($results[9]['month'] == $i){echo '<td>'.$results[9]['price'].'</td>';}
                                                    else if ($results[10]['month'] == $i){echo '<td>'.$results[10]['price'].'</td>';}
                                                        else if ($results[11]['month'] == $i){echo '<td>'.$results[11]['price'].'</td>';}
                                                            else if ($results[12]['month'] == $i){echo '<td>'.$results[12]['price'].'</td>';}
                                                                //else if ($results[13]['month'] == $i){echo '<td>'.$results[13]['price'].'</td>';}
                                                                else echo '<td>0,00</td>';
            If($i==12)
            {
            echo '</tr><tr>';
            }
            $i++;
        }
    }
    $k++;
    }

There should be better ways of doing this and getting it actually to work, but for the death of me I can not come up with any...

Any help would be much appreciated.

  • 写回答

2条回答 默认 最新

  • dongying6179 2014-08-07 13:00
    关注

    If array is sorted alphabetically by product names then you can use this code to easily achieve what you want

    $number_of_rows = count($results);
    $i = 0;
    while ($i < $number_of_rows) {
        $j = $i;
        $some_array = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
        while ($results[$j]['product'] == $results[$i]['product']) {
            $some_array[$results[$j]['date']] = $results[$j]['price'];
            $j++;
        }
        echo '<tr><td>'.$results[$i]['product'].'</td>';
        for ($k = 1; $k <= 12; $k++)
            echo '<td>'.$some_array[$k].'</td>';
        echo '</tr>';
        $i = $j;
    }
    

    In second while you save prices to additional $some_array while there are the same products (it needs array to be sorted by product names). If there is no row for some month then the price is 0. At the end you echo the prices and assign to $i the value of $j so next loop will count prices of next product

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Matlab在app上输入带有矩阵形式的初始条件发生错误
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器