Welcome Guest, Not a member yet? Register   Sign In
URI segment not working correctly
#1

[eluser]Darker[/eluser]
It is My_photos controller which is reached when going to base_url/my-photos. But when i go to base_url/my-photos/delete/1/ the $photo_id returns no value. Why ?
Code:
<?php
class My_photos extends CI_Controller {
public function index() {
  $this->load->model('user_model');
  $data['isLogged'] = $this->autoload_model->isLogged();
  $photo_id = $this->uri->segment(2);
  if ($data['isLogged']){
   $data['temporary_data'] = $data;
   $this->load->view('main_view', $data);
  } else {
   redirect('index.php', 'refresh');
  }
}
public function delete(){
  $this->load->model('user_model');
  if ($this->user_model->checkIfPhotoExistsByUserID($_SESSION['user_id'], $photo_id)){
   $this->user_model->deletePhotoByID($photo_id);
  }
  redirect('index.php', 'refresh');
}
}
?>

Thanks.
#2

[eluser]CroNiX[/eluser]
Because you didn't define $photo_id in your delete() method, which is the method you are accessing. It doesn't know anything about $photo_id in your index() method.
#3

[eluser]Darker[/eluser]
[quote author="CroNiX" date="1351466367"]Because you didn't define $photo_id in your delete() method, which is the method you are accessing. It doesn't know anything about $photo_id in your index() method.[/quote]

What about an example ? Because everything what i'm trying to do is not working.
#4

[eluser]CroNiX[/eluser]
Code:
public function delete(){
  $photo_id = $this->uri->segment(3); //(segment1=controller, segment2=method, segment3=parameter1)
  
  if ($photo_id !== FALSE)
  {
    //.. rest of your code
  }
  else
  {
    //segment 3 missing
  }
}
#5

[eluser]CroNiX[/eluser]
Also, you should avoid using my_ to prefix things unless you are extending a CI Core Class/Library/Helper, which your controller isn't doing. You can get yourself in trouble if you don't understand how it works and you do this elsewhere.
#6

[eluser]Darker[/eluser]
[quote author="CroNiX" date="1351469330"]Also, you should avoid using my_ to prefix things unless you are extending a CI Core Class/Library/Helper, which your controller isn't doing. You can get yourself in trouble if you don't understand how it works and you do this elsewhere.[/quote]
Okay, i renamed it and tried to do anything i can, but... I realized that delete() function isn't even called. Now i tried to disable any view in delete() but nothing.
Help me please Smile

Code:
<?php
class Photos extends CI_Controller {
public function index() {
  $this->load->model('user_model');
  $data['isLogged'] = $this->autoload_model->isLogged();
  if ($data['isLogged']){
   $data['temporary_data'] = $data;
   $this->load->view('main_view', $data);
  } else {
   //redirect('index.php', 'refresh');
  }
}
public function delete(){

  $this->load->model('user_model');
  $photo_id = $this->uri->segment(2);
  $data['photo_id'] = $photo_id;
  $data['isLogged'] = $this->autoload_model->isLogged();
  if ($data['isLogged']){
   if ($this->user_model->checkIfPhotoExistsByUserID($_SESSION['user_id'], $photo_id)){
    $this->user_model->deletePhotoByID($photo_id);
   }
   $data['temporary_data'] = $data;
   //$this->load->view('main_view', $data);
  }
}
}
?>

Thanks.
#7

[eluser]Darker[/eluser]
No answers ? Sad
#8

[eluser]mr lister[/eluser]
As CroNiX says, you need to obtain the $photo_id from the 3rd segment of the URL, you have it being obtained from your 2nd segment, which would give you:

Code:
$photo_id = "delete"

//(segment1=controller, segment2=method, segment3=parameter1)


Also, and correct me if I am wrong, but you could also do it this way, (as an option):

Code:
public function delete( $photo_id = NULL ) {

// something along the lines of:
if ( $photo_id == NULL )
{
  //... cannot do much without photo id ...
}
  else
  {
   //... have an id value, do something here ...
  }

}

This way, if there is no 3rd segment, $photo_id = NULL, else, if there is a 3rd segment, $photo_id will equal that segment value.

domain.com/photos/delete will have $photo_id equaling NULL, whereas;

domain.com/photos/delete/4 will have $photo_id equaling a value of 4

Does this make sense?

http://ellislab.com/codeigniter/user-gui...passinguri explains more.

#9

[eluser]Darker[/eluser]
[quote author="mr lister" date="1351530080"]As CroNiX says, you need to obtain the $photo_id from the 3rd segment of the URL, you have it being obtained from your 2nd segment, which would give you:

Code:
$photo_id = "delete"

//(segment1=controller, segment2=method, segment3=parameter1)


Also, and correct me if I am wrong, but you could also do it this way, (as an option):

Code:
public function delete( $photo_id = NULL ) {

// something along the lines of:
if ( $photo_id == NULL )
{
  //... cannot do much without photo id ...
}
  else
  {
   //... have an id value, do something here ...
  }

}

This way, if there is no 3rd segment, $photo_id = NULL, else, if there is a 3rd segment, $photo_id will equal that segment value.

domain.com/photos/delete will have $photo_id equaling NULL, whereas;

domain.com/photos/delete/4 will have $photo_id equaling a value of 4

Does this make sense?

http://ellislab.com/codeigniter/user-gui...passinguri explains more.

[/quote]

Thanks for your answer, but delete() function doesn't work when you go to /photos/delete. Dunno why, but it still uses index().
#10

[eluser]Darker[/eluser]
ok it works, there was something bad with routes.




Theme © iAndrew 2016 - Forum software by © MyBB