I am not clear how to construct a multidimensional array to create a simple datagrid framework. I have a number of class methods with crud select statements from MySQL tables in the format:

<?php

  function table1()
  {
    $sql = "SELECT * FROM `table1`";
    $this->database_select($sql);
    $result = $this->getMySQLresult($sql);
    $html = "<table>";
    while ($row = mysql_fetch_array( $result ))
    {
      $html .= "<tr><td>".$row['field_a']."</td><td>".$row['field_b']."</td></tr>";
    }
    $html .= "</table>";
    return $html;
  }

  function table2()
  {
    $sql = "SELECT * FROM `table2`";
    $this->database_select($sql);
    $result = $this->getMySQLresult($sql);
    $html = "<table>";
    while ($row = mysql_fetch_array( $result ))
    {
      $html .= "<tr><td>".$row['field_x']."</td><td>".$row['field_y']."</td></tr>";
    }
    $html .= "</table>";
    return $html;
  }

?>

where the MySQL tables look like:

table1

+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | bigint(12)   | NO   | PRI | NULL    | auto_increment |
| field_a | varchar(128) | NO   |     | NULL    |                |
| field_b | varchar(128) | NO   |     | NULL    |                |
+---------+--------------+------+-----+---------+----------------+

table 2

+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | bigint(12)   | NO   | PRI | NULL    | auto_increment |
| field_x | varchar(128) | NO   |     | NULL    |                |
| field_y | varchar(128) | NO   |     | NULL    |                |
+---------+--------------+------+-----+---------+----------------+

I am trying to replace all these methods with a single datagrid method, selecting information about each individual table from two summary grid tables - one with properties of each table (table name and the select sql statement for that table) and the other with field names and headings for each individual table:

grid_properties

+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | bigint(12)   | NO   | PRI | NULL    | auto_increment |
| table      | varchar(128) | NO   |     | NULL    |                |
| select_sql | varchar(128) | NO   |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

grid_fields

+--------------------+--------------+------+-----+---------+----------------+
| Field              | Type         | Null | Key | Default | Extra          |
+--------------------+--------------+------+-----+---------+----------------+
| id                 | bigint(12)   | NO   | PRI | NULL    | auto_increment |
| grid_properties_id | bigint(12)   | NO   |     | NULL    |                |
| heading            | varchar(128) | NO   |     | NULL    |                |
| field              | varchar(128) | NO   |     | NULL    |                |
+--------------------+--------------+------+-----+---------+----------------+

Data in the grid_fields.field column is intended to just be the field name of the table referenced in grid_properties.table, related through grid_properties.id::grid_fields.grid_properties_id.

The method I am using to construct the datagrid has a multidimensional array in the last while loop that has something wrong with it. What is the correct way to construct this?

<?php

  function grid_schema($table)
  {
    // Get information about $table from the grid_properties table
    $sql = "SELECT * FROM `grid_properties` WHERE `table` LIKE '".$table."'";
    $this->database_select ($sql);
    $result1 = mysql_query ($sql);
    $properties = mysql_fetch_array ($result1);
    $properties_sql = $properties['select_sql'];
    $id = $properties['id'];

    // Get information about the grid field names
    $fields_sql = "SELECT * FROM `grid_fields` WHERE `grid_properties_id` ='".$id."'";
    $this->database_select ($fields_sql);
    $fields_result = mysql_query ($fields_sql);

    // Display the Table Headings
    $html = "<table><tr>";
    while ($fields_row = mysql_fetch_array ( $fields_result ))
    { $html .= "<th>".$fields_row['heading']."</th>"; }
    $html .= "</tr>";

    // Perform the SQL query from the grid_properties table
    $this->database_select ($properties_sql);        
    $properties_result = mysql_query ($properties_sql);
    while ($properties_row = mysql_fetch_array ($properties_result))
    {
      // Print out the contents of each row
      $html .= "<tr>";
      while ($fields_row = mysql_fetch_array( $fields_result ))
      {
        // This is where I am having problems
        $html .= "<td>".$properties_row[$fields_row]['field']."</td>";
      }
    }
    $html .= "</table>";
    return $html;
  }
?>
share|improve this question
It's hard to say as I'm not sure what your grid_fields.field column contains, but perhaps that should be $properties_row[$fields_row['field']]? – eggyal yesterday
Thanks for the suggestion @eggyal. I tried moving the [ ], without success. Data in the grid_fields.field column is intended to just be the field name of the table named referenced in grid_properties.table, related through grid_properties.id::grid_fields.grid_properties_id. – Steven J. Garner yesterday
feedback

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.