使用mysql查询返回的数据在cakephp中创建下拉列表

时间:2017-04-18 05:43:46

标签: php cakephp

应用程序/控制器/ Actionscontroller.php

<?php
App::uses('AppController', 'Controller');

class ActionsController extends AppController {
  public Function index(){
    App::import('Model', 'ConnectionManager');
    $con = new ConnectionManager;
    $cn = $con->getDataSource('default');

    $tablequery="SELECT TABLE_NAME
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='cakephp_db' AND TABLE_NAME != 'report'";
    $this->set('table',$cn->query($tablequery));
  }
}
?>

应用程序/视图/操作/ index.ctp

<fieldset>
  <table>
    <tr><th><td>OR</td></th></tr>
    <tr>
      <th>CHOOSE EXISTING DATA</th>
      <td>
      <?php  echo $this->Form->create('table',array('type' => 'get'));
            echo $this->Form->select('table ',  $table,['empty' => 'choose one']);?>          
      <?php  echo $this->Form->end('submit'); ?>
  </td>
    </tr>
    </table>
  </fieldset>

应用程序/模型/ action.php的

<?php
App::uses('AppModel', 'Model');

class Action extends AppModel {
  public $validate = array(
        'table'=> array(
          'required' => array(
            'rule'=> 'notBlank',
            'message' => 'atleast select one table'
          )
        )
      );
}
?>

我需要这种格式的结果。我写的样本输出

<select name="field">
    <option value="">(choose one)</option>
    <option value="0">1</option>
    <option value="1">2</option>
    <option value="2">3</option>
    <option value="3">4</option>
    <option value="4">5</option>
</select>

但我以这种格式获取输出;通过运行此代码。帮助我解决问题。

<select name="table " id="tableTable">
<option value="">choose one</option>
</optgroup>
<option value="TABLE_NAME">Employee</option>
<optgroup label="TABLES">
<optgroup label="1">
</optgroup>
<option value="TABLE_NAME">House_Price</option>
<optgroup label="TABLES">
</optgroup>
<optgroup label="2">
</optgroup>
<option value="TABLE_NAME">insurence</option>
<optgroup label="TABLES">
</optgroup>
<optgroup label="3">
</optgroup>
<option value="TABLE_NAME">posts</option>
<optgroup label="TABLES">
</optgroup>
<optgroup label="4">
</optgroup>
<option value="TABLE_NAME">topics</option>
<optgroup label="TABLES">
</optgroup>
<optgroup label="5">
</optgroup>
<option value="TABLE_NAME">users</option>
<optgroup label="TABLES">
</optgroup>
<optgroup label="6">
</optgroup>
<option value="TABLE_NAME">visualizations</option>
<optgroup label="TABLES">
</optgroup>
</select>          

1 个答案:

答案 0 :(得分:1)

如果你已经通过了cakephp的文件

https://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::select

如果你给这样的结构

$options = array(
   'Group 1' => array(
      'Value 1' => 'Label 1',
      'Value 2' => 'Label 2'
   ),
   'Group 2' => array(
      'Value 3' => 'Label 3'
   )
);
echo $this->Form->select('field', $options);

Output will be:

<select name="data[User][field]" id="UserField">
    <optgroup label="Group 1">
        <option value="Value 1">Label 1</option>
        <option value="Value 2">Label 2</option>
    </optgroup>
    <optgroup label="Group 2">
        <option value="Value 3">Label 3</option>
    </optgroup>
</select>

这就是你的情况。

你必须像这样改变你得到的回应的结构

echo $this->Form->select('field', array(
    'Value 1' => 'Label 1',
    'Value 2' => 'Label 2',
    'Value 3' => 'Label 3'
));
Output:

<select name="data[User][field]" id="UserField">
    <option value=""></option>
    <option value="Value 1">Label 1</option>
    <option value="Value 2">Label 2</option>
    <option value="Value 3">Label 3</option>
</select>

因此,您必须对代码进行更改

    $tablequery="SELECT TABLE_NAME
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='cakephp_db' AND 
    TABLE_NAME != 'report'";
    $rows = $cn->query($tablequery)->fetchAll('assoc');

    $dataToSend = [];
    foreach ($rows as $row) {
        $dataToSend[$row['TABLE_NAME']] = $row['TABLE_NAME'];
    }

    $this->set('table',$dataToSend);