phpmysqleditablegrid

EditableGrid, "addcolumn" loop


Trying to make a loop for EditableGrid code. This is how it looks now.

$grid->addColumn('id', 'ID', 'integer');
$grid->addColumn('site', 'Site', 'string');

So if I need to add a new column to the page, I add a new column in MySQL database and also add a new row in this code, like:

$grid->addColumn('newcolumn', 'A brand new column', 'string');

In order to automatically add new columns to the page I want to make a loop, which gets inputs for the first argument (name of the field in the database) taken from the table:

CREATE TABLE price (
  id   INT(11)     NOT NULL AUTO_INCREMENT,
  site VARCHAR(50) NOT NULL,

and the other two arguments (label that will be displayed in the header and data type of the column in MySQL) taken from this table:

CREATE TABLE header (
  header_name VARCHAR(50) NOT NULL,
  header_type VARCHAR(50) NOT NULL,

Solution

  • Ok, think I found the solution. In order to create the loop, we create 2 queries, which are:

    $get=$mysqli->query('SELECT header_name, header_type FROM header');
    $get1=$mysqli->query('SHOW COLUMNS FROM price');
    

    then we make a loop

    while($row = mysqli_fetch_assoc($get) and $row1 = mysqli_fetch_assoc($get1)){
    $grid->addColumn(''.$row1['Field'].'', ''.$row['header_name'].'', ''.$row['header_type'].'');}
    

    I, guess, that's it. Also, if you need to exclude some of the columns, use this piece of code:

    if($row1 == 'id' || $row1 == 'site')
        continue;