Search code examples
phparrayscodeignitercodeigniter-2codeigniter-3

retrieve array values , print and substitute in URL


I am trying to extract the value of instance from $sess_data and replace it in the URL using codeigniter.

My code is as below:

$uresult = $this->user_model->get_user($email, $password);

if (count($uresult) > 0) {

    // set session

    $sess_data = array(
    'login' => TRUE, 
    'uname' => $uresult[0]->fname, 
    'uid' => $uresult[0]->id, 
    'instance' => $uresult[0]->instance
    );

    $this->session->set_userdata($sess_data);

    foreach ($sess_data as $object) {
        echo $object->sess_data;
    }

    die;

    redirect('http://www.example.com/"instance"', 'refresh');

} else {

    $this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Wrong Email-ID or Password!</div>');
    redirect('login/index');
}

I need to extract this value instance' => $uresult[0]->instance and replace the URl instance part with this value, so that it goes to that particular URL. I am getting 5 errors withe 4 of them being Severity: Notice

Message: Trying to get property of non-object

and 1 error saying

A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 3

Filename: controllers/login.php

Line Number: 38

My Model code is :

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class user_model extends CI_Model
{
	function __construct()
    {
        parent::__construct();
    }
	
	function get_user($email, $pwd)
	{
		$this->db->where('email', $email);
		$this->db->where('password', md5($pwd));
        $query = $this->db->get('user');
		return $query->result();
	}
	
	// get user
	function get_user_by_id($id)
	{
		$this->db->where('id', $id);
        $query = $this->db->get('user');
		return $query->result();
	}
	
	// insert
	function insert_user($data)
    {
		return $this->db->insert('user', $data);
	}
}?>


Solution

  • You can try it by updating foreach loop like below..

    foreach ($sess_data as $key => $object) {
        echo $key."=>".$object."<br />";
    }