0

I have the following code:

<?php
//The company_array:

$company_array = array(
    "AAA" => "AAA",
    "BBB" => "BBB",
    "CCC" => "CCC",
    "DDD" => "DDD"
);

$platform_data = 'PC'; //Just to keep it short :)


    foreach ($company_array as $company) {
    if ($stmt = $mysqli->prepare("SELECT price, time FROM $company WHERE platform = ? ORDER BY time ASC")) {
        $stmt->bind_param("s", $platform_data);
        $stmt->execute();
        $stmt->bind_result($price[$company], $time[$company]);
        $i=0;
        while ($stmt->fetch()) {
            $company_info[$company][$i] = array('Price' => $price[$company], 'Time' => $time[$company]);
            $i++;
        }
        $stmt->close();
    }
?>

Now I had some issues with this, the last iteration of the loop seems to break, if I print $company_info all but the last company displays fine, the last one seem to repeat the last value for all the rows:

Array
(
[AAA] => Array
    (
        [0] => Array
            (
                [Price] => 626.8600
                [Time] => 2013-09-27 14:30:06
            )

        [1] => Array
            (
                [Price] => 615.5900
                [Time] => 2013-09-27 15:45:05
            )

        [2] => Array
            (
                [Price] => 604.7400
                [Time] => 2013-09-27 17:45:05
            )
    )

[BBB] => Array
    (
        [0] => Array
            (
                [Price] => 246.7200
                [Time] => 2013-09-27 14:30:06
            )

        [1] => Array
            (
                [Price] => 245.4700
                [Time] => 2013-09-27 15:45:05
            )

        [2] => Array
            (
                [Price] => 244.8300
                [Time] => 2013-09-27 17:45:05
            )

    )

[CCC] => Array
    (
        [0] => Array
            (
                [Price] => 189.0900
                [Time] => 2013-09-27 14:30:06
            )

        [1] => Array
            (
                [Price] => 188.9800
                [Time] => 2013-09-27 15:45:05
            )

        [2] => Array
            (
                [Price] => 188.8900
                [Time] => 2013-09-27 17:45:05
            )

    )

[DDD] => Array
    (
        [0] => Array
            (
                [Price] => 134.3100
                [Time] => 2013-10-06 13:30:06
            )

        [1] => Array
            (
                [Price] => 134.3100
                [Time] => 2013-10-06 13:30:06
            )

        [2] => Array
            (
                [Price] => 134.3100
                [Time] => 2013-10-06 13:30:06
            )
    )
)

As you can see company AAA, BBB and CCC all have different Prices and Times in each of their arrays, but company DDD has the same value repeated 3 times (the last value in the database) when the values should be different in the same way as the other companies.

Now from what I have read I am doing this wrong, instead of using foreach I should use implode() on the array and use that, but its confusing me since I can not find a good example where it is used on the FROM field (seems to only be used on WHERE)

My question would be, how can I get away from using a foreach loop since it seems to be causing issues, and use the proper implode() method on the FROM field?

I am guessing I need to modify my while loop also so that the output remains in the same format.

2
  • it would be helpful to see the mysql table content, that produces this side effect.
    – worenga
    Oct 6, 2013 at 15:23
  • @mightyuhu Not sure how to quite display it since each table has several hundreds of rows and there are about 30 tables just like this (it also contains some sensitive data), I trimmed it down because it was always the last iteration, lets say I add a fake company into the company array, then all companies show fine, or if I took out DDD then CCC would be the one messed up.
    – Tinman
    Oct 6, 2013 at 15:36

0

Your Answer

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

Browse other questions tagged or ask your own question.