0

I am trying to figure out the best way to handle this. The while loop puts all the data into an array, great! But, i want to group a set of results into an array and put it back into the $row array. And i am not sure how to do that.

The reason being that i want to run a function that will display icons if the grouped items have a value, and i figured grouping them would be easier to work with.

Here is what i have now, it throws an error Too few arguments to function icon(), and i understand why its throwing that.

    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();
}

Update The results i am looking for is:

    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] => 
)
12
  • You want which part to do what exactly? Commented Mar 13, 2019 at 13:36
  • The while runs, creates an array $row, then i want to put another array in $row that has the features array. Commented Mar 13, 2019 at 13:38
  • So what you want is $features = array('pump' => array(/*all pumps*/), 'product' => array(/*all products*/))? Commented Mar 13, 2019 at 13:40
  • 1
    Not sure I understand the question, but this . "<td>" . icon($features) . "</td>" is obviously missing a parameter! Commented Mar 13, 2019 at 13:43
  • Yeah, i'm confused as hell too haha. I updated my question with the array result i am looking for. Commented Mar 13, 2019 at 13:51

2 Answers 2

1

If you changed the function to accept a features array then the function would work

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'>";
    }
}

Alternatively you could pass the 2 items to the existing function

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

but this rather negates the point of creating the array in the first place.

Another alternative would be to just use the $row data directly and the existing function.

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

and forget about the temporary array completely

UPDATE

Function running both possibilities

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

I thought about doing this too, but didn't know about putting array into a function.
So do you also want to add your new features array into the $row array? Remember the $row array will only exist for the duration of a Loop Iteration, so you would need to do something else with it if you want ed to keep it for use aoutside the WHILE loop
The location of the function is subjective. If it can be run inside the loop, that is fine, outside that is fine as well. What you have given me combined with @Cobra_Fast has solved the problem with the exception of including the duplicate data in the array. So trailer_pump appears twice, it's not a big deal if it's not a performance issue.
Combined with @Cobra_Fast's answer, this worked. Thanks for the help, i appreciate your time!
1

I think what you're looking for is simply

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

or if you want to keep $feautres the way it already is

$row['features'] = $features;

1 Comment

Thanks for the help. This combined with RiggsFolly, solved my problem. I wish i could award both of you with the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.