13

I am trying to push new array item into existing array variable that has items from database. What I want to do is add a new item named 'Others' at the end of this array and display it as select drop down in view which consists of all the items from database and at the end of this select the 'Others' item that I manually added in my controller.

Here is what I tried to do:

    $competition_all = Competition::all();
    $newCompete = array('name'=>'Others');
    array_push($competition_all, $newCompete);

    $this->competition_games = array('Competition');

    foreach ($competition_all as $competition_games) {
        $this->competition_games[$competition_games->name] = $competition_games->name;
    }

what it said is like this

Unhandled Exception

Message:

Trying to get property of non-object Location:

C:\xampp\htdocs\khelkheladi\khelkheladi\application\controllers\register.php on line 104

In my database the Competition has this type of column structure

->id
->year
->place
->name
->created_at
->updated_at

in given order.

What I'm trying to do is without actually inserting an item in database, just statically show the others select item in select tag in view. How do I insert such new item without actually inserting it to database but to display in view only?

The output what i'm getting before by just retrieving database item is like this

<select>
  <option value="1">Value 1</option>
  <option value="2">Value 2</option>
  <option value="3">Value 3</option>
  <option value="4">Value 4</option>
</select> 

what I like to do is like this

<select>
  <option value="1">Value 1</option>
  <option value="2">Value 2</option>
  <option value="3">Value 3</option>
  <option value="4">Value 4</option>
  <option value="5">Others</option>
</select> 
2
  • 3
    You've been a member here for over a year. It's time to learn how to use the editor and stop posting malformed content for others to fix. Indent your code blocks. Commented Oct 2, 2012 at 6:25
  • okay, i corrected my error, but i believe the php codes were already indented. thank you for this comment, won't happen again. Commented Oct 2, 2012 at 7:14

2 Answers 2

15

That's because you're adding a non-object to the last element of the array.

Here i suppose you get an array of objects with the name property

$competition_all = Competition::all();

Here you add a key=>value pair to the last element of the objects array

$newCompete = array('name'=>'Others');
array_push($competition_all, $newCompete);

Here you walk through the array of objects and when it comes to the last element the "$competition_games->name" has no name property

foreach ($competition_all as $competition_games) {
            $this->competition_games[$competition_games->name] = $competition_games->name;
        }

Try something like including a stdclass for it like :

$newCompete = new StdClass();
$newCompete->name = 'Others';
array_push($competition_all, $newCompete);
Sign up to request clarification or add additional context in comments.

Comments

3

The "clean" way to do it would be to create an instance of Competition without committing it to the database, and repeat once more your cycle with the extra instance.

However, here you appear to be just producing a list, so it ought to be enough to do a much quicker addition to the final list:

// Empty array
$this->competition_games = [ 0 => 'Others' ];

foreach (Competition::all() as $game) {
    $this->competition_games[$game->id] = $game->name;
}

Using 0 (or -1) as the nonexistent Id.

Comments

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.