使用while

时间:2019-03-13 13:31:51

标签: php

我正在尝试找出解决此问题的最佳方法。 while循环将所有数据放入一个数组中,太棒了!但是,我想将一组结果分组到一个数组中,然后将其放回到$ row数组中。而且我不确定该怎么做。

原因是我想运行一个功能,如果分组的项目具有值,该功能将显示图标,并且我认为对它们进行分组会更容易使用。

这就是我现在所拥有的,它抛出错误Too few arguments to function icon(),并且我理解为什么会抛出该错误。

    function icon($pump, $product) {
        if ($pump == 'Yes') {
            return "<img src='.." . IMAGES . "icons/icon-pump.png' alt='Trailer has a Pump' class='trailer-icon'>";
        }
        if ($product == 'Yes') {
            return "<img src='.." . IMAGES . "icons/icon-gas.png' alt='Trailer has Product' class='trailer-icon'>";
        }
    }

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

        $features = array(
            'pump' => $row['trailer_pump'],
            'product' => $row['trailer_product']
        );

        $data = "<tr>\r\n<td><a href='view-trailer.php?id=" . $row['trailer_id'] . "'>" . $row['trailer_number'] . "</a></td>\r\n"
                . "<td>" . $row['first_name'] . " " . $row['last_name'] . "</td>\r\n"
                . "<td>" . icon($features) . "</td>"
                . "<td>" . $row['status_name'] . "</td>\r\n"
                . "<td><a href='https://www.google.com/maps/search/?api=1&query=" . $row['trailer_lat'] . "," . $row['trailer_long'] . "' target='_blank'>View Map</a></td>\r\n"
                . "</tr>\r\n";
        print $data;
    }
} catch (PDOException $e) {
    print $e->getMessage();
}

更新 我正在寻找的结果是:

    Array
(
    [id] => 1
    [trailer_number] => 609
    [trailer_annual] => 0000-00-00
    [trailer_fiveyear] => 0000-00-00
    [trailer_compartments] => 4
    [features] => array (
           [pump] => Yes
           [product] =>
    )
    [trailer_compart1] => 3000
    [trailer_compart2] => 2000
    [trailer_compart3] => 3000
    [trailer_compart4] => 2000
    [trailer_compart5] => 
)

2 个答案:

答案 0 :(得分:1)

如果将功能更改为接受功能数组,则该功能将正常工作

function icon(array $feature) {
    if ($feature['pump'] == 'Yes') {
        return "<img src='.." . IMAGES . "icons/icon-pump.png' alt='Trailer has a Pump' class='trailer-icon'>";
    }

    if (!$feature['product'] == 'Yes') {
        return "<img src='.." . IMAGES . "icons/icon-gas.png' alt='Trailer has Product' class='trailer-icon'>";
    }
}

或者,您可以将2项传递给现有功能

. "<td>" . icon($features['pump'], $features['product']) . "</td>"

但这反而抵消了首先创建数组的意义。

另一种选择是直接使用$row数据和现有功能。

. "<td>" . icon($row['trailer_pump'], $row['trailer_product']) . "</td>"

完全忘记了临时数组

更新

同时运行两种功能的功能

function icon(array $feature) {
    $ret = '';

    if ($feature['pump'] == 'Yes') {
        $ret .= "<img src='.." . IMAGES . "icons/icon-pump.png' alt='Trailer has a Pump' class='trailer-icon'>";
    }

    if (!$feature['product'] == 'Yes') {
        $ret .= "<img src='.." . IMAGES . "icons/icon-gas.png' alt='Trailer has Product' class='trailer-icon'>";
    }
    return $ret;
}

答案 1 :(得分:1)

我认为您正在寻找的只是

$row['features'] = array(
    'pump' => $row['trailer_pump'], 
    'product' => $row['trailer_product']
);

或者如果您想保持$feautres的状态

$row['features'] = $features;