How can I convert the array to a string variable in php codeigniter?

I am new to Codeigniter and want to display my state_name which I believe in an array, but I want the first element in that array to be in a variable and be displayed

I am getting the output as: Array ( [0] => stdClass Object ( [state_name] => XYZ state ) ) the output I want is just XYZ state

My controller goes like this:

  public function downloadd()
{   
        $this->load->model('New_model');
        $state_id=$this->input->post('state');   //gets me state_id from my view
        $state_name = $this->New_model->getStatename($state_id); //gets me state_name in form of above output
        print_r($state_name[0]);
        exit;
    }

And here is my model code:

public function getStatename($state_id) {
    
    $this->db->select('state_name');
    $this->db->where('state_id',$state_id);
    $state_name=$this->db->get('states')->result();
    
    return $state_name; //geting users data from db in result array
}

Please tell me where am I going wrong, Thanks for any contribution in advance 🙂

Answer

Just edit your last two lines of code in your model as follows:

$ans= $this->db->get('states')->row_array();
return $ans['state_name'];

This shall do the needful.

Leave a Reply

Your email address will not be published. Required fields are marked *