php 代码点火器4:如何将我使用SELECT OPTION标记插入表1中的值链接到表2

ggazkfy8  于 2023-08-02  发布在  PHP
关注(0)|答案(2)|浏览(110)

我有两个表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>

工作正常,但是当我从tbl_office更新from office_name的值时,tbl_result上的值没有变化。有没有一种方法可以链接这些值,而不是在将选项插入到另一个表时仅获取该选项的值?

tcomlyy6

tcomlyy61#

您不应该在tbl_result中存储office名称。你应该保存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)  |
+----+-----------+-----------+---------+

w46czmvw

w46czmvw2#

在索引中联接两个表

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

字符串

相关问题