Home > Back-end >  Redirecting user to the respective table cell clicked
Redirecting user to the respective table cell clicked

Time:11-04

Currently I have a table in my view class that is filling its data from the backend using a foreach loop. I have a edit cell for each row of the table which I want to use to redirect the user to a page like contacts/edit/1 when the cell with the id 1 is clicked on and that page should show all the data of the user with id 1. I'm using the following code to achieve this:

View Class(index.php):

<?php
            foreach($records as $row ){                            
                ?>
            <tr>            
              <td><?= $row->refno ?></td>
              <td><?= $row->display_name ?></td>                  
              <td><a href="contacts/edit/'.$row->id.'">
              <span class="sr-only">edit</span></a>
              </td>
              <td></td>
            </tr>

Controller Class:

public function lists($type='')
    {
        $main['records']=$this->contacts_model->get_records();
        $main['page'] = 'crm/contacts/index';
        $this->load->view('crm/index',$main);
    }

    public function edit($slug='')
    {
        $main['page'] = 'crm/contacts/edit';
        $this->load->view('crm/index',$main);
    }

Model Class:

function get_records(){
        $this->db->select("*");
        $this->db->from("contacts");
        $this->db->where("status='Y'");
        $query = $this->db->get();
        return $query->result();
    }

// I want another method here that will fetch the details as per the id that was selected

So here I have 2 problems, 1 is that my <a> tag does not take me to the respective id after I have set it to contacts/edit/'.$row->id.', and 2 is that how do I show the details for the person with id 1 in my contacts/edit view class.

CodePudding user response:

To solve your 1st problem is very simple. You made a simple syntax error:

Wrong:

<?php
   ...
   <td><a href="contacts/edit/'.$row->id.'">
   ...

Correct:

<?php
   ...
   <td><a href="<?= 'contacts/edit/'. $row->id ?>">
   ...

Note: The short format echo (<?= "some text or variables" ?>) only works when the short_open_tags flag is enabled in php.ini!

For 2nd problem you need to make an other function in controller which collect and show view with ID based data.

  • Related