0

I am new to plugin development, so sorry for this most likely very easy question.

In a plugin, where can I store variables and queries that will be used accross multiple functions?

Example of Private:

private function uploadsQuery() {

    $member_id = $this->EE->session->userdata('member_id');

    $sql = "
        SELECT COUNT(t.author_id) AS current_uploads, c.upload_total AS upload_total
        FROM exp_competition_purchase_upload_total AS c
        LEFT JOIN exp_channel_titles AS t ON c.member_id = t.author_id AND t.channel_id = '4'
        WHERE c.member_id = $member_id 
    ";

    $query = $this->EE->db->query($sql);

}

public function details() {

    return $this->uploadsQuery();

    $data = $query->result_array();

    return $this->EE->TMPL->parse_variables( $this->EE->TMPL->tagdata, $data );

}  

1 Answer 1

4

If they are static variables, you could store them in the construct and refer to them like this:

function __construct()
{
  $this->var1 = "data";
  $this->var2 = "data";
  $this->query = "select * from table";
}

function getVar1()
{
  return $this->var1;
}

function getVar2()
{
  return $this->var2;
}

// below has not been tested, but it should give you an idea
function getResults()
{
  //load db lib
  $q = $this->query($this->query);

  if($q->num_rows() == 0)
    return false;

  return $q->result_object();
}

Or you could have private functions that you refer to in all of your functions.

private function nameQuery()
{
  // return data
}

function getName()
{ 
  return $this->nameQuery();
}

I have updated my original post with a revised version of the code you updated your posted with.

The first return you had in your details function will just return that and ignore the rest of the function.

You want to return the data retrieved from the uploadsQuery function, so you need to return that data in that function.

Then you want to store that data into your $data variable as seen in the details function, to be passed into the parse_variables function.

I can't say this works for sure, but it'll give you a push in the right direction

private function uploadsQuery() {

    $member_id = $this->EE->session->userdata('member_id');

    $sql = "
        SELECT COUNT(t.author_id) AS current_uploads, c.upload_total AS upload_total
        FROM exp_competition_purchase_upload_total AS c
        LEFT JOIN exp_channel_titles AS t ON c.member_id = t.author_id AND t.channel_id = '4'
        WHERE c.member_id = $member_id 
    ";

    $query = $this->EE->db->query($sql);

    if($query->num_rows() == 0) // no data retrieved from table, return false
      return false;

    return $query->result_array(); // return this data, to be used in your other function
}

public function details() {
    $data = $this->uploadsQuery(); // storing the data that was returned from the uploadsQuery function

    if(!$data)
        return false; // no data to pass into parse_variables below, so just return false

    return $this->EE->TMPL->parse_variables( $this->EE->TMPL->tagdata, $data );
}
7
  • Hi Brad. Thanks. I have just tried the private function, but it hasn't worked for me. Would this be the right code to return the private functions contents into another function, where the private function is called uploadsQuery : return $this->uploadsQuery();
    – ccdavies
    Commented May 31, 2013 at 14:20
  • Update your question with the code in question.
    – Brad
    Commented May 31, 2013 at 14:49
  • Updated my original post.
    – Brad
    Commented May 31, 2013 at 15:06
  • Thanks Brad. I see now. I was hoping there was a way to almost embed parts like variables work, but on a larger scale. For instance in this example, I didn't include the return query as for one function I need to return a row, another I need return results_array...
    – ccdavies
    Commented May 31, 2013 at 15:17
  • You could have multiple queries, then pass a variable into your private function to say whether you want to use the one row result or result_array, you'd just have to setup the conditional to run the proper code.
    – Brad
    Commented May 31, 2013 at 15:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.