dtjo51649 2017-04-18 05:43
浏览 7
已采纳

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

app/Controller/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));
  }
}
?>

app/View/Actions/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>

app/model/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'
          )
        )
      );
}
?>

I need the result in this format. a sample output i have written

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

But i am getting the output in this format;By running this code.Help me to solve the issue.

<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条回答 默认 最新

  • dongtiao2105 2017-04-18 06:13
    关注

    If you have gone througn the doc of cakephp

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

    If you give structure like this

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

    That's what in your case is happening .

    You have to change the structure of the response you are getting like this

    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>
    

    So you have to make changes to your code as bellow

        $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。
  • ¥15 MATLAB卫星二体模型仿真