【问题标题】:CODEIGNITER4: how to link value from table_1 to table_2 that I insert using SELECT OPTION tagCODEIGNITER4:如何将值从 table_1 链接到我使用 SELECT OPTION 标签插入的 table_2
【发布时间】:2021-11-14 23:13:46
【问题描述】:

我有 2 个表 tbl_officetbl_result

| id | office_name |   | id | office      | rating    | comment |
|----|-------------|   |----|-------------|-----------|---------|
| 1  | Records     |   | 1  | Records     | Satisfied |         |
| 2  | Logistics   |   | 2  | Logistics   | Satisfied |         |
| 3  | HR          |   | 3  | HR          | Neutral   |         |

我在我的表单上使用了 SELECT OPTION 标签来从 tbl_office

获取 office_name 的行值

控制器

namespace App\Controllers;
use App\Models\SurveyModel;
use App\Models\OfficeMOdel;

class Survey extends BaseController
{
    public function index()
    {
        $data = [];
        helper(['form','url']);

        $office = new OfficeMOdel();
        $data['office'] = $office->findAll();
       

        if($this->request->getMethod() == 'post'){

            $rules = [
                'office' => ['label' => 'Office', 'rules' => 'required'],
                'rating' => ['label' => 'Rating', 'rules' => 'required']
            ];

        if (!$this->validate($rules)){
            $data['validation'] = $this->validator;
        }else{
            $model = new SurveyModel();

            $newData = [
                'office' => $this->request->getVar('office'),
                'rating' => $this->request->getVar('rating'),
                'comment' => $this->request->getVar('comment'),
            ];

            $model->save($newData);
            $session = session();
            $session->setFlashdata('success','Your Feedback has been successfully added to our system!',);

            return redirect()->to('survey/confirmation');
         }
        }

        echo view ('templates/header_form', $data);
        echo view ('surveyform');
        echo view ('templates/footer_form');
    }

PHP/HTML 用于显示 office_name

的值
<select name="office">

   <?php foreach($office as $row) :?>
   <option><?php echo $row['office_name'] ?></option>
   <?php endforeach; ?>

</select>

IT WORKS,但是当我从 tbl_office 更新 from office_name 的值时,tbl_result 上的值不改变。当我将选项插入另一个表时,有没有办法链接这些值而不是仅仅获取选项的值?

非常感谢您的回答。这里是初学者。

【问题讨论】:

    标签: javascript php mysql codeigniter-4


    【解决方案1】:

    您应该点头将 office 名称存储在您的 tbl_result 中。相反,您应该存储 office_id:

    CREATE TABLE `tbl_office` (
        id int primary key auto_increment,
        name varchar(64)
    );
    
    CREATE TABLE `tbl_rating` (
        id int primary key auto_increment,
        name varchar(64)
    );
    
    CREATE TABLE `tbl_result` (
        id int primary key auto_increment,
        office_id int,
        rating_id int,
        comment text
    );
    
    SELECT `res`.`id`, `o`.`name` AS `office`, `r`.`name` AS `rating`, `comment`
    FROM `tbl_result` `res`
    JOIN `tbl_office` `o` ON `o`.`id` = `res`.`office_id`
    JOIN `tbl_rating` `r` ON `r`.`id` = `res`.`rating_id`
    

    Share SQL fiddle

    +====+===========+===========+=========+
    | id | office    | rating    | comment |
    +====+===========+===========+=========+
    | 1  | Records   | Satisfied | (null)  |
    +----+-----------+-----------+---------+
    | 2  | Logistics | Satisfied | (null)  |
    +----+-----------+-----------+---------+
    | 3  | HR        | Neutral   | (null)  |
    +----+-----------+-----------+---------+
    

    【讨论】:

    • 我应该将 id 插入到 tbl_result 中,而不是从 tbl_office 获取“名称”,所以每个当数据从表中更改时,它将从 id 行中获取值 name 对吗?
    • 没错!使用 JOIN 你会得到实际的名字
    • 非常感谢。我学到了一些东西。我会尝试应用它。
    • 欢迎采纳有用的答案
    【解决方案2】:

    在索引中加入两个表

      $result = $this->model->select('request_post.*')
                ->join('users', 'request_reply.user_id = users.id', 'left')
                ->join('request_post', 'request_reply.post_id = request_post.id', 'left')
                 ->where(['request_post.id'=>['1']])
              
                ->paginate(10, 'default',1, 0);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多