Welcome Guest, Not a member yet? Register   Sign In
URI Segments only grabbing first number
#1

[eluser]redlogic[/eluser]
Hello,

I'm still fairly new to CI but been coding in PHP for a while. I've been using the URI segments to pass variables instead of query strings, but it only seems to grab the first digit.

So for example if the address is:

http://localhost/index.php/site/record/3

This will pull record number 3 from the database.

However if it is:

http://localhost/index.php/site/record/39

It still pulls record 3 from the database.

Seems work fine from 0-9 but when it get to double figures it won't work.

Any ideas what I'm missing?

Not sure what code you need but here's my Site controller:

Code:
<?php

class Site extends Controller {
    
    function index() {
        $rec_value = NULL;
        $this->load->model('client_details_model');
        $data['records'] = $this->client_details_model->getClientDetails($rec_value);
        $this->load->view('client', $data);
    }
    
    function record() {
        if ($this->uri->segment(2) == "record") {
            $get = $this->uri->segment(3);
            $rec_value = $get['record']; // get uri segment value
            $this->load->model('client_details_model');
            $data['records'] = $this->client_details_model->getClientDetails($rec_value);
            $this->load->view('client', $data);
            }
    }
    
    function id() {
        if ($this->uri->segment(2) == "id") {
        $id_value = NULL;
        $get = $this->uri->segment(3);
        $id_value = $get['id']; // get uri segment value
        $this->load->model('contact_details_model');
        $data['id'] = $this->contact_details_model->getContactDetails($id_value);
        $this->load->view('contact', $data);
        }
    }

}

Thanks!
#2

[eluser]WanWizard[/eluser]
You are making it a lot more complicated then needed.

With a URL like http://localhost/index.php/site/record/39, CI will take care of the routing to controller site, method record().

You then define the method:
Code:
function record( $id = FALSE )
{
    if ( $id )
    {
        $this->load->model('client_details_model');
        $data['records'] = $this->client_details_model->getClientDetails($id);
        // you probably want to check the resultset first before loading the view?
        $this->load->view('client', $data);
    }
    else
    {
        // no id passed, redirect to the index
        redirect('site/index');
    }
}
#3

[eluser]redlogic[/eluser]
Story of my life :roll:

Thanks for the post, I thought there must be a simpler way of doing it, I just, well, didn't know what it was.

I could still use some help on the original question about why the
Code:
$this->uri->segment(2)
is only using the first digit in the string.

Any ideas?
#4

[eluser]Phil Sturgeon[/eluser]
[quote author="redlogic" date="1284575098"]Story of my life :roll:

Thanks for the post, I thought there must be a simpler way of doing it, I just, well, didn't know what it was.

I could still use some help on the original question about why the
Code:
$this->uri->segment(2)
is only using the first digit in the string.

Any ideas?[/quote]

You are accessing a string (even if it contains a number, its a string) via array syntax:

Code:
$get = $this->uri->segment(3); // 39
$rec_value = $get['record']; // God knows what PHP will try and do here
#5

[eluser]redlogic[/eluser]
Right, yep I'm an idiot.

Removed that and it works fine now, thanks!

Just changed:
Code:
$rec_value = $get['record']; // get uri segment value

to

Code:
$rec_value = $get; // get uri segment value

That'll teach me to cut and paste code.




Theme © iAndrew 2016 - Forum software by © MyBB