0

I read this questions

How do I insert a variable into a PHP array?

But I think it is no more valid in php 7.

Fatal error: Constant expression contains invalid operations in D:\Business_Work\Apache24\htdocs\garbagevalue\books_solution\models.php on line 82

I'm creating an array using which I will generate HTML forms.
In the SELECT input of the form, I have to give options dynamically fetched from some table. But the problem is I can't put anything dynamic on the array. getIds method returns the array of options for select field.

class Chapters extends Models{
        protected $tableName = APP.'_chapters';
        protected $formDef = [
            ['Chapter Number', 'text', ['required']],
            ['Chapter Name', 'text', ['required']],
            ['Keywords', 'text', ['required']],
            ['Description', 'text', ['required']],
            ['Select Book', 'select', ((new Chapters())->getIds((new Books())->$tableName))]
        ];
    }
}

Argument of the method getIds() is a string and it returns an array (I've tested that function). This function is the member of the Base class Models.

public function getIds($table, $field=NULL, $value=NULL){
            require BASE_PATH.'/ap-admin/connection.php';
            if($field == NULL){
                $sql = "SELECT id FROM ".$table;
            }
            else{
                $sql = "SELECT id FROM ".$table." WHERE ".$field." = ".$value;
            }
            $result = $con->query($sql);
            return $result->fetch_row();
        }

So How I can put a variable or dynamic data in a php array in php7 ?

12
  • 2
    Use your class's constructor to add the dynamic entry Commented Jun 13, 2018 at 4:18
  • @Phil Explain it little further, please? Commented Jun 13, 2018 at 4:19
  • Next time, try searching for the error message Commented Jun 13, 2018 at 4:20
  • 3
    📎: "It looks like you're writing your own ORM. Have you considered using one that's already written, tested, and widely supported like Doctrine, Propel or Eloquent?" Commented Jun 13, 2018 at 4:21
  • 1
    @Phil He's back! I'm just dying for combining-character-eyeballs to be pushed into the Unicode spec. Commented Jun 13, 2018 at 4:27

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.