CodeIgniter Forums
how to take the url to became a variable - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: how to take the url to became a variable (/showthread.php?tid=17294)

Pages: 1 2 3


how to take the url to became a variable - El Forum - 03-31-2009

[eluser]yudahebat[/eluser]
Code:
http://localhost/ci2/adminpage/choose_testi/4

I want to take '4' as an ID
how to make the function??


how to take the url to became a variable - El Forum - 03-31-2009

[eluser]sherpa[/eluser]
$id = $this->uri->segment(3);


how to take the url to became a variable - El Forum - 03-31-2009

[eluser]yudahebat[/eluser]
Code:
function choose_testi()
    {
        $id = $this->uri->segment(3);
        $query = $this->db->get('testimonial');
        $this->db->where('id_testimonial',$id);
        $user_array = $query->result_array();
            
        if(count($user_array) > 0)
        {
            foreach($user_array as $ua)
            {
                $data['tanggal'] = $ua['tanggal'];
                $data['email'] = $ua['email'];
                $data['nama'] = $ua['nama'];
                $data['isi'] = $ua['isi'];
            }
        }
        $this->load->view('adm_testimonial',$data);
    }

thx, Ive use it on my function

but there is a problem

how to call the adm_testimonial.php??

because on the url from the function that I make it makes me go to

Code:
http://localhost/ci2/adminpage/choose_testi/4

I want it became like this :
Code:
http://localhost/ci2/adminpage/adm_testimonial

help me please


how to take the url to became a variable - El Forum - 03-31-2009

[eluser]sherpa[/eluser]
Code:
function choose_testi()
    {
        $id = $this->uri->segment(3);
        $this->db->where('id_testimonial',$id); // moved to this location
        $query = $this->db->get('testimonial');
        $user_row = $query->row(); //return single row if you want to return a single row from the query
         /* Uncomment if you want ot return multiple rows from the dtabase */
        // $user_row = $query->result();

        //$query->result_array();  //removed this live
          
        if($user_row->num_rows() > 0)
        {
            $data[] = $user_row;
        }
        $this->load->view('adm_testimonial',$data);
    }

Code for Views (print the column fields in in your view
(it only for your understanding purpose )

Code:
echo $tanggal;
echo $email;
echo $nama;
echo $isi;



how to take the url to became a variable - El Forum - 03-31-2009

[eluser]jedd[/eluser]
You could, and arguably should, instead just do this:
Code:
function choose_testi ( $id = 0 )
    {
     echo $id;      // should show '4'
         ...

With your cited URL of : http://localhost/ci2/adminpage/choose_testi/4 , the variable $id gets a value of 4. If your URL has no number after 'choose_testi' then $id is set to 0.

EDIT: Accidentally left the urisegment assign in there .. whoops!


how to take the url to became a variable - El Forum - 03-31-2009

[eluser]yudahebat[/eluser]
didn't work...
forget it

I have new problem
I want to make this
Code:
$this->load->view('adm_testimonial',$data);

to
redirect function

how to make it??

is this true??
Code:
redirect('/adminpage/adm_testimonial/',$data);

make the function still on adm_testimonial page
and read the $data also...

please your opinion..


how to take the url to became a variable - El Forum - 03-31-2009

[eluser]yudahebat[/eluser]
Fatal error: Call to undefined method stdClass::num_rows() in C:\xampp\htdocs\CI2\system\application\controllers\adminpage.php on line 246


how to take the url to became a variable - El Forum - 03-31-2009

[eluser]jedd[/eluser]
[quote author="yudahebat" date="1238576205"]didn't work...
forget it
[/quote]

I can't forget it. I'm not that kind of guy.

I made a mistake - and have since edited the post above - you should re-read and re-try.


With your follow-up question, I'm unsure what you mean.

This bit is from your controller, I'm assuming:
Code:
$this->load->view('adm_testimonial',$data);


And you say you want to 'redirect function' to:
Quote:
Code:
redirect('/adminpage/adm_testimonial/',$data);

make the function still on adm_testimonial page
and read the $data also...

I don't understand what you are actually attempting to achieve, and what file structure you are attempting to use.

Do you have a controller called adminpage? Or do you have a view-subdirectory by that name?

Please use more words and less question marks. Smile


how to take the url to became a variable - El Forum - 03-31-2009

[eluser]yudahebat[/eluser]
Code:
function choose_testi()
    {
        $id = $this->uri->segment(3);
        $this->db->where('id_testimonial',$id); // moved to this location
        $query = $this->db->get('testimonial');
        $user_row = $query->row(); //return single row if you want to return a single row from the query
         /* Uncomment if you want ot return multiple rows from the dtabase */
        // $user_row = $query->result();

        //$query->result_array();  //removed this live
          
        if($user_row->num_rows() > 0)
        {
            $data[] = $user_row;
        }
        $this->load->view('adm_testimonial',$data);
    }

it produce this error statement :
Code:
Fatal error: Call to undefined method stdClass::num_rows() in C:\xampp\htdocs\CI2\system\application\controllers\adminpage.php on line 246

how to fixed it??


how to take the url to became a variable - El Forum - 03-31-2009

[eluser]yudahebat[/eluser]
Code:
<?php
class Adminpage extends Controller
{
    var $base;
    var $css;
    
    function Adminpage()
    {
        parent::Controller();
    }
function adm_testimonial()
    {
        $this->base = $this->config->item('base_url');
        $this->css = $this->config->item('css1');
        $data['css1'] = $this->css;
        $data['base'] = $this->base;
        $data['myrobots'] = '<meta name="robots" content="noindex,nofollow">';
        $data['mywebtitle'] = 'mrputucar | data admin testimonial';
        
        $mymenu = $this->menu->menu_adm();
        $data['menu_adm'] = $mymenu;
    
        //This assumes you used the sample MySQL table
        $user_table = 'admin';
        
        //Load the URL helper
        $this->load->helper('url');
        
        $data['tanggal']     = 'choose database';
        $data['email']         = 'choose database';
        $data['nama']         = 'choose database';
        $data['isi']         = 'choose database';
        
        $this->load->view('adm_testimonial',$data);    
        
    }    
    
    function login()
    {
        //Load
        $this->load->helper('url');
        $this->load->library('validation');
        
        //Check incoming variables
        $rules['login_username']    = "required|min_length[4]|max_length[32]|alpha_dash";
        $rules['login_password']    = "required|min_length[4]|max_length[32]|alpha_dash";        

        $this->validation->set_rules($rules);

        $fields['login_username'] = 'Username';
        $fields['login_password'] = 'Password';
        
        $this->validation->set_fields($fields);
                
        if ($this->validation->run() == false) {
            /**/
            //If you are using OBSession you can uncomment these lines
            $flashdata = array('error' => true, 'error_text' => $this->validation->error_string);
            $this->session->set_flashdata($flashdata);
            $this->session->set_flashdata($_POST);
            /**/
            redirect('/adminpage/');            
        } else {
            //Login to account
            if($this->simplelogin->login($this->input->post('login_username'), $this->input->post('login_password'))) {
                /**/
                //If you are using OBSession you can uncomment these lines
                $flashdata = array('success' => true, 'success_text' => 'Login Successful!');
                $this->session->set_flashdata($flashdata);
                /**/
                redirect('/adminpage/adm_admin/');    
            } else {
                /**/
                //If you are using OBSession you can uncomment these lines
                $flashdata = array('error' => true, 'error_text' => 'There was a problem logging into the account.');
                $this->session->set_flashdata($flashdata);
                $this->session->set_flashdata($_POST);
                /**/
                redirect('/adminpage/');            
            }            
        }
    }

    function delete($user_id)
    {
        
        $this->load->helper('url');

        if($this->simplelogin->delete($user_id)) {
            
            redirect('/adminpage/');    
        } else {
            
            redirect('/adminpage/');            
        }            
        
    }

    function update_admin()
    {
        $this->load->model('Crud');
          
        $data = $this->Crud->update_admin();

        $this->load->view('crud', $data);    
    }

    function choose_testi($id = 0)
    {
    
        $id = $this->uri->segment(3);
        $this->db->where('id_testimonial',$id);
        $query = $this->db->get('testimonial');
        $user_row = $query->row();
        
        if($user_row->num_rows() > 0)
        {
            $data[] = $user_row;
        }
        $this->load->view('adm_testimonial',$data);
  
    }
    
    function update_testi()
    {
        
        $this->load->model('Crud');
          
        $data = $this->Crud->update_testi();

        $this->load->view('crud', $data);    
    }

    function logout()
    {
        //Load
        $this->load->helper('url');

        //Logout
        $this->simplelogin->logout();
        redirect('/adminpage/');
    }
    
    
}

this is my adminpage.php