0

I am sorry for asking this basic level question. I have fetched some data from DataBase and stored it to a variable inside a function, and wanted to get the value of that variable outside the function?

public function getemailData()
{
    $email_id_investors = $this->db
        ->select('value')
        ->get_where('common_email_settings', ['name' => investors_email])
        ->row()->value;

}

I wish to get the value of the $email_id_investors outside the function. Again I am apologizing for this basic question

Database table name - common_email_settings

fields are Id, title, name, value

1 Greeting Mail, greeting_email ,[email protected]

2 Loan Service Mail, loan_service_email ,[email protected]

3 Processing Mail, processing_email ,[email protected]

5
  • 4
    The obvious thing to do would be to return it. Commented Jun 14, 2021 at 10:57
  • 4
    It's nothing to do with CodeIgniter specifically. return is one of the basic things you learn when you first learn about PHP functions (or functions in almost any programming language, actually). php.net/manual/en/functions.returning-values.php Commented Jun 14, 2021 at 11:00
  • 3
    also: 'name' => investors_email is wrong, either use a variable $investors_email or a string 'investors_email', but not a method name Commented Jun 14, 2021 at 12:48
  • 1
    well why don't you try it and find out? In a standard PHP app that would work, not sure in CodeIgniter what would happen to a raw echo, I assume it would end up somewhere in the output, but not sure where. You could always log it to a file, instead. codeigniter4.github.io/userguide/testing/debugging.html seems to explain ways of formalising your debugging process to fit in with the framework. But to be honest if you don't understand this sort of basic stuff about returning from functions, then you're not ready for frameworks anyway. Commented Jun 14, 2021 at 13:58
  • 2
    Please stop deleting your comments, it makes the rest of the chat nonsensical. That's not helpful to anyone. Commented Jun 14, 2021 at 14:15

3 Answers 3

2

To strictly answer the question, you could store the value in a scoped global $this variable, though I don't know why you wouldn't just query the function and have it return a value.

public function getemailData($investors_email)
{
    $this->email_id_investors = $this->db
        ->select('value')
        ->get_where('common_email_settings', ['name' => $investors_email])
        ->row()->value;
}

// then in another function called later in the chain, you can grab it
public function doSomethingElse() {
  $investors =  $this->email_id_investors;
} 

It's probably better just to create a getter function for that variable.

This doesn't look useful given your scenario. This might be useful if the variable you're storing is something processor intensive (and you're using $this like a cache), you need to access it in multiple functions called during a given state, and you don't want to rewrite your function chain to accept this parameter (so as to pass it along). However, that is a clear sign you need to refactor your logic and make it more flexible (pass object or arrays rather than single variables for example).

Sign up to request clarification or add additional context in comments.

4 Comments

@ncp - Did this help solve your question? If so, please accept as the answer. thanks
thanks for your help and its not yet worked.i don't want to initialise a second function.i just want to store the value of $email_id_investors in a variable outside the function,so that i can give that variable value to where ever i want it
Right, that's what this is $this->email_id_investors is a superglobal - meaning it's available across all controllers, models and views. I only added the second function in my answer to show how you can access it. I think this is the answer you were looking for
Suppose if i echo $investors it should be there right? but its really blank. This is the url i called look like .......................dashboard/loan_closing_dashboard/doSomethingElse
1

You are not returning your variable. Try returning your variable like this,

public function getemailData()
{
    $email_id_investors = $this->db
        ->select('value')
        ->get_where('common_email_settings', ['name' => investors_email])
        ->row()->value;
     return $email_id_investors;


}

1 Comment

if i return also how can get it outside the function? i just want to store the value of $email_id_investors in a variable outside the function. is this the right way $email = getemailData(); //example i have tried it but not getting
1
function getemailData($name)
{
    
    $email_id_investors = $this->db
        ->get_where('common_email_settings', ['name' => $name])
        ->result()[0];
    return $email_id_investors->value;
}

This one worked for me. I have given this function in the common model page and called this on other pages.Thank you for your help

    $email = $this->common->getemailData('account_email'); -> getting data in this variable
    echo $email;
    // exit();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.