CodeIgniter Forums
passing id to link? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: passing id to link? (/showthread.php?tid=12524)



passing id to link? - El Forum - 10-22-2008

[eluser]slothdog[/eluser]
I want to delete an entry from the database by its id number. Ive got the model to do the delete working when I manually edit the function and put in the number for the id to delete.

for example:

#1 BLHABLH edit | delete

how do i get the id #1 into the link to tell the delete function what to delete?

thanks.


model:
Code:
function delete_by_pkey($idField)
{
      
      // LOAD DATABASE
      $this->load->database();
      
      // SELECT
      $this->db->where('id', $idField);

      // DO THE DELETE
      $this->db->delete('registration');

      return true;

   }

excerpt from controller:
Code:
function deletecollective()
    {
    // LOAD DATABASE MODEL
    $this->load->model('registrationmodel');
    
    // QUERY MODEL
    $data['query'] = $this->registrationmodel->delete_by_pkey(HERE IS WHERE I NEED TO GET THE ID#);
    
    // DISPLAY DATA
    $this->load->view('viewdeletecollective');
    
    }


view:
Code:
<? echo "Deleted"; ?>



passing id to link? - El Forum - 10-22-2008

[eluser]manilodisan[/eluser]
Code:
$data['query'] = $this->registrationmodel->delete_by_pkey($this->uri->segment(3));

I've put uri segment 3. You should put there whatever corresponds to the id of your item.

And the link:

Code:
<a href="&lt;?= site_url ( 'controller/method/' . $id ) ?&gt;">click</a>



passing id to link? - El Forum - 10-22-2008

[eluser]zimco[/eluser]
I can't tell how you're creating the links but i did something similar in a view like:
Code:
echo ('<a href="'. site_url('controller/function/'.$results[$i]['your_id']) .'">' . $results[$i]['your_id']) . '</a>');

Then do the $thedeleteid = $this->uri->segment(3, 0); thing in your delete function to know which one it is you want to delete like mailodisian already mentioned above; his post beat mine.


passing id to link? - El Forum - 10-22-2008

[eluser]slothdog[/eluser]
[quote author="manilodisan" date="1224711998"]
Code:
$data['query'] = $this->registrationmodel->delete_by_pkey($this->uri->segment(3));

I've put uri segment 3. You should put there whatever corresponds to the id of your item.

And the link:

Code:
<a href="&lt;?= site_url ( 'controller/method/' . $id ) ?&gt;">click</a>
[/quote]

i tired that using the anchor() and it sent it to a function in the class with the number that didnt exist so i got a 404 error. is there a difference between using <a > and using the anchor() to make a link?


passing id to link? - El Forum - 10-23-2008

[eluser]manilodisan[/eluser]
No. I tend to write html and not php to echo some html Tongue That's the only difference.


passing id to link? - El Forum - 10-23-2008

[eluser]slothdog[/eluser]
thanks that worked!


passing id to link? - El Forum - 10-23-2008

[eluser]Velo Studios[/eluser]
Should you not be using a form to delete?

Is it not best practice to use a form?

$data['query'] = $this->registrationmodel->delete_by_pkey($_POST['id']);