Welcome Guest, Not a member yet? Register   Sign In
Can't get segment() info
#1

[eluser]drakenlx[/eluser]
Hello Everybody

I'm new to CI and wrote a very simple code in which I can't get segment info; the plan is to list a table with 3 columns id_user / name / and a Picture Anchor pointing to the edit method in the main controller passing the id_user as a parameter, I get redirected to:

http://localhost/cigniter/index.php/user/edit/3

But when I want to get segment info in the "edit" function always get:
echo $this->uri->total_segments() // displays: 0
echo $this->uri->segment(0,"No Data") // displays: No Data
echo $this->uri->segment(1,"No Data") // displays: No Data
echo $this->uri->segment(2,"No Data") // displays: No Data
echo $this->uri->segment(3,"No Data") // displays: No Data

Do you know if need to edit something in CI config or php.ini, etc. ?
Thank You

The Code is:
Controller: User.php
Code:
<?php
class User extends Controller{

    function User(){
        parent:: Controller();
        $this->load->database('default');
        $this->load->helper('html');
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->library('uri');
    }
    
    function index(){        
        $data['rs_users'] = $this->db->query('SELECT id_user, name FROM users');
        $this->load->view("user_v",$data);
    }

    function edit(){
        echo  $this->uri->total_segments();
        echo  $this->uri->segment(0,"No Data");
        echo  $this->uri->segment(1,"No Data");
        echo  $this->uri->segment(2,"No Data");
        echo  $this->uri->segment(3,"No Data");
    }
}

View: user_v.php
Code:
<html>
..
    <table>
        <tr>
            <td>id_user</td>
            <td>name</td>
            <td>edit</td>
        </tr>
        &lt;?php foreach ($rs_users->result() as $user): ?&gt;
        <tr>
            <td>&lt;?php echo $user->id_user;?&gt;</td>
            <td>&lt;?php echo $user->name;?&gt;</td>
            <td>&lt;?php echo anchor('user/edit'.$user->id_user,img('images/edit.jpg'));?&gt;</td>
        </tr>
        &lt;?php endforeach; ?&gt;
     </table>
..
&lt;/html&gt;
#2

[eluser]Twisted1919[/eluser]
1. Don't load the uri class, it is auto loaded by CI .
2. $this->uri->segment(0) doesn't exists, the count starts from 1.
3. print_r($this->uri->segment_array()) gives you ?
4. don't do database calls from your controller, move them in your models, and call the models .
5. don't foreach($rs_users->result() as user) in your view. Instead, in your controller do
$data['users'] = $this->model_name->get_users(); and in your view foreach($users AS $user);
6. don't $this->load->helper(1),$this->load->helper(2) blah . $this->load->helper(array(1,2,3,n)); is the way .
7.You will use your database in all your controllers instances maybe, so autoload it , don't load it in the constructor .
#3

[eluser]drakenlx[/eluser]
Thank You very much "Lab Assistant"

My problem is gone just removing the $this->load->library('uri'); line
Now I'm taking note about the rest too.

Thank you on going further the mere solution, I do appreciate it!
#4

[eluser]Twisted1919[/eluser]
No problem, just please read the user guide ten times again. Believe me, it'll make your life much easier Wink
#5

[eluser]drakenlx[/eluser]
Alright, I will! Wink




Theme © iAndrew 2016 - Forum software by © MyBB