Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

122
Vistas
CODEIGNITER4: how to link value from table_1 to table_2 that I insert using SELECT OPTION tag

See I have 2 tables tbl_office and tbl_result

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

i used a SELECT OPTION tag on my form to get the row value of office_name from tbl_office

CONTROLLER

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 for displaying the values of office_name

<select name="office">

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

</select>

IT WORKS, but when I update the value from office_name from tbl_office, the value on the tbl_result does not change. Is there a way to link those value instead of just getting the value of the option when i insert it to the other table?

Thank you so much for answering. Beginner here.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You should not store office name in your tbl_result . Instead this you should to store 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)  |
+----+-----------+-----------+---------+
about 4 years ago · Juan Pablo Isaza Denunciar

0

join two table in index

  $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);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda