0

多次元配列を構築して単純なデータグリッド フレームワークを作成する方法がわかりません。次の形式の MySQL テーブルからの crud select ステートメントを含むクラス メソッドがいくつかあります。

<?php

  function table1()
  {
    $sql = "SELECT * FROM `table1`";
    $result = mysqli_query ($link, $sql);
    $html = "<table>";
    while ($row = mysqli_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`";
    $result_properties = mysqli_query ($link, $sql);
    $html = "<table>";
    while ($row = mysqli_fetch_array( $result ))
    {
      $html .= "<tr><td>".$row['field_x']."</td><td>".$row['field_y']."</td></tr>";
    }
    $html .= "</table>";
    return $html;
  }

?>

MySQL テーブルは次のようになります。

表1

+---------+--------------+------+-----+---------+----------------+
| 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    |                |
+---------+--------------+------+-----+---------+----------------+

表 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    |                |
+---------+--------------+------+-----+---------+----------------+

これらすべてのメソッドを単一のデータグリッド メソッドに置き換えて、2 つのサマリー グリッド テーブルから個々のテーブルに関する情報を選択しようとしています。および個々の表の見出し:

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    |                |
+--------------------+--------------+------+-----+---------+----------------+

のデータgrid_fieldsfieldcolumn は、 で参照されているテーブルのフィールド名にすぎませんgrid_propertiestable、 を通じて関連してgrid_propertiesいます。id:: grid_fields. grid_properties_id.

データグリッドを構築するために使用している方法には、最後の while ループに多次元配列があり、何か問題があります。これを構築する正しい方法は何ですか?

<?php

  function grid_schema($table)
  {
    // Get information about $table from the grid_properties table
    $sql = "SELECT * FROM `grid_properties` WHERE `table` LIKE '".$table."'";
    $result_properties = mysqli_query ($link, $sql);
    $properties = mysqli_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."'";
    $fields_result = mysqli_query ($link, $fields_sql);

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

    // Perform the SQL query from the grid_properties table
    $properties_result = mysqli_query ($link, $properties_sql);
    while ($properties_row = mysqli_fetch_array ($properties_result))
    {
      // Print out the contents of each row
      $html .= "<tr>";
      while ($fields_row = mysqli_fetch_array( $fields_result ))
      {
        // This is where I am having problems
        $html .= "<td>".$properties_row[$fields_row]['field']."</td>";
      }
    }
    $html .= "</table>";
    return $html;
  }
?>
4

1 に答える 1

0
<?php
    function grid_schema($table)
{
$link = mysqli_connect("localhost", "root", "", "test");

// Get information about $table from the grid_properties table
$sql = "SELECT * FROM `grid_properties` WHERE `table` LIKE '".$table."'";
$result_properties = mysqli_query ($link, $sql);
$properties = mysqli_fetch_array ($result_properties, MYSQLI_ASSOC);
$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."'";
$fields_result = mysqli_query ($link, $fields_sql);

// Display the Table Headings
$html = "<table><tr>";
    $rows = array();
while ($fields_row = mysqli_fetch_array ( $fields_result, MYSQLI_ASSOC ))
{
        $rows[] = $fields_row['field'];
        $html .= "<th>".$fields_row['heading']."</th>";
    }
$html .= "</tr>";

// Perform the SQL query from the grid_properties table
$properties_result = mysqli_query ($link, $properties_sql);
while ($properties_row = mysqli_fetch_array ($properties_result, MYSQLI_ASSOC))
{
// Print out the contents of each row
$html .= "<tr>";
foreach($rows as $row)
{
// This is where I am having problems
$html .= "<td>".$properties_row[$row]."</td>";
}
}
$html .= "</table>";
return $html;
}

echo grid_schema('table1');
于 2013-02-06T09:02:17.970 に答える