CodeIgniter Forums
How to pass parameter via URL? - 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: How to pass parameter via URL? (/showthread.php?tid=23520)



How to pass parameter via URL? - El Forum - 10-13-2009

[eluser]Sinclair[/eluser]
Hi,

I need to pass a parameter in the second segment of an URL in the index function of the controller.

My model function is this:
Code:
function getAnunZone($pzone) {
        $query = $this->db->query("select id, name from atw_anu where id_zone = '".$pzone."' ", $this->uri->segment(2));
        return $query->result();
    }

This is not working. What I'am doing wrong?

Best Regards.


How to pass parameter via URL? - El Forum - 10-13-2009

[eluser]CroNiX[/eluser]
I dont understand the comma in your sql or what you are trying to do with the 2nd url segment.

I would have written it like:
Code:
<?php
return $this->db->select('id, name')
            ->where('id_zone', $pzone)
            ->get('atw_anu')
            ->result();



How to pass parameter via URL? - El Forum - 10-13-2009

[eluser]Sinclair[/eluser]
Hi,

I'am trying to pass the value for the parameter "$pzone" in the URL.

Like this: http://www.example.com/ads/HERE_I_PASS_THE_PARAMETER

HERE_I_PASS_THE_PARAMETER = $pzone

This is possible?


Best Regards,


How to pass parameter via URL? - El Forum - 10-13-2009

[eluser]CroNiX[/eluser]
Code:
<?php
function getAnunZone($pzone = $this->uri->segment(2))
{
    return $this->db->select('id, name')
                    ->where('id_zone', $pzone)
                    ->get('atw_anu')
                    ->result();
}

I just gave pzone a default value of $this->uri->segment(2).

$pzone_data = $this->getAnunZone(); //will get pzone data where id_zone = 2nd parameter of uri
$pzone_data = $this->getAnunZone(5); //will get pzone data where id_zone = 5


How to pass parameter via URL? - El Forum - 10-13-2009

[eluser]CroNiX[/eluser]
Another thought...you are using segment 2. Segment 2 is usually the function/method of your controller and segment 3+ is used for variables (unless you've done something in your routes).

www.mysite.com/ads/view/4

Controller = ads
Function = view
parameter = 4 (uri->segment(3))


How to pass parameter via URL? - El Forum - 10-13-2009

[eluser]Sinclair[/eluser]
Thanks for your reply, but I can't get working.

It works if I have this:

- Model:
Code:
function getAnunciosZona($pzona) {
   $query = $this->db->query("select id_anuncio, n_anuncio from atw_anuncios where id_zona = '".$pzona."' ");
   return $query->result();
    }

- Controller:
Code:
function index() {
   $this->load->model('acomp_model');    
   $data['result'] = $this->acomp_model->getAnunciosZona('SP');
        
   $this->load->view('anuncios', $data);
    }


But if I have this, when I call the second segment, gives me an error.

I call the parameter like this: http://www.example.com/anuncios/SP

- Model:
Code:
function getAnunciosZona($pzona) {
   $query = $this->db->query("select id_anuncio, n_anuncio from atw_anuncios where id_zona = '".$pzona."' ");
   return $query->result();
    }

- Controller:
Code:
function index() {
   $this->load->model('acomp_model');    
   $data['result'] = $this->acomp_model->getAnunciosZona($pzone = $this->uri->segment(2));
        
   $this->load->view('anuncios', $data);
    }

What I'am doing wrong?

Best Regards,


How to pass parameter via URL? - El Forum - 10-13-2009

[eluser]Sinclair[/eluser]
[quote author="CroNiX" date="1255487132"]Another thought...you are using segment 2. Segment 2 is usually the function/method of your controller and segment 3+ is used for variables (unless you've done something in your routes).

www.mysite.com/ads/view/4

Controller = ads
Function = view
parameter = 4 (uri->segment(3))[/quote]

I don't have see this post. I think that is the problem. I'am trying to passo to the second segment...


How to pass parameter via URL? - El Forum - 10-13-2009

[eluser]Sinclair[/eluser]
Problem Solved. I can't pass parameters in the second segment.