php - 如何从表中获取记录并将其显示在另一个 View 中

标签 php mysql codeigniter

我有一个名为设置的表,其中包含 id、tax1、tax2 等字段,并且我创建了一个名为 service.view 的 View ,我需要从设置表中获取tax1 值并在 service.view 文件中显示标签。我怎样才能做到这一点?有人可以帮我编码吗... 这是我的表结构

CREATE TABLE IF NOT EXISTS `service_settings` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `company_name` varchar(30) NOT NULL,
  `company_logo` varchar(50) NOT NULL,
  `company_tax11` varchar(20) NOT NULL,
  `company_tax12` varchar(20) NOT NULL,
  `company_tax13` double NOT NULL,
  `company_tax14` double NOT NULL,
  `company_tax21` varchar(20) NOT NULL,
  `company_tax22` varchar(20) NOT NULL,
  `company_tax23` double NOT NULL,
  `company_tax24` double NOT NULL,
  `company_tax31` varchar(20) NOT NULL,
  `company_tax32` varchar(20) NOT NULL,
  `company_tax33` double NOT NULL,
  `company_tax34` double NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

我正在使用codeigniter和mysql数据库 我需要从此表中获取一条记录并将其显示在 service.view 中

最佳答案

根据需要更改此代码。这也会显示数据库中的数据

在模型中

<?php
    public function get_service()
    {
        $query= $this->db->query("SELECT * FROM user service_settings");
        $result= $query->result_array();
        return $result;
    }
?>

在 Controller 中

<?php
    public function login()
        {

            $data['table'] =  $this->Model_Name->get_service();

            $this->load->view('service');//load your relevant view

        }
?>

在 View 中

<table>
    <tr>
        <th>company_name</th>
        <th>company_tax11</th>
        <th>company_tax12</th>
        <th>company_tax13</th>
        <th>company_tax14</th>
    </tr>       
    <?php
    foreach ($table as $new_tbale) 
    {
        ?>
        <tr>
            <td>$new_tbale['company_name']</td> 
            <td>$new_tbale['company_tax11']</td>    
            <td>$new_tbale['company_tax12']</td>    
            <td>$new_tbale['company_tax13']</td>    
            <td>$new_tbale['company_tax14']</td>
        </tr>                   
        <?php
    }
        ?>      
</table>

编辑01

<?php
    public function get_service()
    {
        $query= $this->db->query("SELECT company_tax11 FROM user service_settings");
        $result= $query->result_array();
        return $result;
    }
?>

关于php - 如何从表中获取记录并将其显示在另一个 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31018472/

相关文章:

php - 表示分层任务/子任务的最佳方式 (MySQL/PHP)

php - 在 CodeIgniter 中不使用 Eval 来计算 php 中的表达式

php - 加水印后照片颜色丢失

javascript - Node.js mysql - 嵌套查询和异步/等待

mysql - 我想要获取last_update 4天后的记录?

php - 计算使用连接的 MYSQL 查询子组中的记录数

php - 未知列 "in field list form "

php - 在应用程序中选择给我一个空数组但在数据库中给我行(Codeigniter)

php - Codeigniter 将新项目添加到当前 session 用户数据数组

php - 查找字符串中反斜杠的出现