-3

ItemDb class

    public function getRandomItem() {

        $query = "SELECT * FROM `items` ORDER BY RAND() LIMIT 2";
        return $this->query($query);

    }

Index.php

    $item = new Item();

    $result = $item->getRandomItem();

    while ($row = $result->fetch_assoc()) {
       foreach ($row as $key => $value) {
          //I want to put them in a array but the two items need to be separated 
       }
    }

I get two different items out of the database how can I split them and put them in one array separated like:

$array[$key][$value]

Sorry for my English its my second language and I hope you guys understand me.

Proliner
  • 41
  • 1
  • 6
  • 1
    You don't define `itemArray` before you use it. `$itemArray[$key]` doesn't exist before you try to append to it. – Jim Mar 25 '14 at 16:05
  • You are trying to write to `$itemArray`, but you have not declared it yet. Then you are trying to add to its keys, but those values are not yet set. PHP will let you do this, but as you can see it yells at you about it. – gen_Eric Mar 25 '14 at 16:06

1 Answers1

4

You need to declare $itemArray[$key] before you use it. So your code needs to look like

$itemArray = array();
while ($row = $result->fetch_assoc()) {
    foreach ($row as $key => $value) {
        if(!isset($itemArray[$key])) {
            $itemArray[$key] = array(); //Declare it
        }
        $itemArray[$key][] = $value;
    }
}
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Pattle
  • 5,983
  • 8
  • 33
  • 56