1

I have these two functions:

public function desplubicarItem(){
    $item_id = JRequest::getCMD('id');
    $app = JFactory::getApplication();

    $db = JFactory::getDbo();
    $query = 'UPDATE #__k2_items SET published=0 WHERE id='.$item_id;
    $db->setQuery($query);
    $db->query();
}

public function getCuponesUtilizados(){
    $db = JFactory::getDbo();
    $query = 'SELECT count(item_id) as contador 
                FROM #__cuphoneo_subscripcion as cs 
                LEFT JOIN #__k2_items as k2i ON k2i.id = cs.item_id 
                WHERE cs.estado=0 AND k2i.id='..' GROUP BY cs.item_id';
    $db->setQuery($query);
    $resultado = $db->loadObject();

    return $resultado;
}

I want to use the variable $item_id in the first function inside the second function. How can I do this?

0

5 Answers 5

5

Declare a class property item_id and access it in every method you want :

private $item_id;

And instead of $item_id use $this->item_id.

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

1 Comment

I wouldn't do it as a private property, and also you still need to set the value somewhere. Also if you are making it private Joomla codestyle convnetions would mandate $_item_id.
2

Example, using a class property:

public function desplubicarItem(){
    $this->item_id = JRequest::getCMD('id');
    $app = JFactory::getApplication();

    $db = JFactory::getDbo();
    $query = 'UPDATE #__k2_items SET published=0 WHERE id='.$this->item_id;
    $db->setQuery($query);
    $db->query();
}

public function getCuponesUtilizados(){
    //you can now access $this->item_id inside getCuponesUtilizados
    echo $this->item_id; 
    $db = JFactory::getDbo();
    $query = 'SELECT count(item_id) as contador 
                FROM #__cuphoneo_subscripcion as cs 
                LEFT JOIN #__k2_items as k2i ON k2i.id = cs.item_id 
                WHERE cs.estado=0 AND k2i.id='..' GROUP BY cs.item_id';
    $db->setQuery($query);
    $resultado = $db->loadObject();

    return $resultado;
}

1 Comment

Note that this will only work if you always call desplubicarItem() before calling getCuponesUtilizados(), which is really seems expensive (running an update just to get the item_id from the request).
0

there are multiple ways to achieve this. depends on from where the functions are called and what you exactly wanna do.

you could make your $item_id a return value of your first function for example

public function desplubicarItem(){
    $item_id = JRequest::getCMD('id');
    ...
    return $item_id
}

and then add a parameter for it in the second function:

public function getCuponesUtilizados($item_id){
}

then you can call them like this:

$this->getCuponesUtilizados(desplubicarItem());

and get your $item id right into your second function by using your first function's return value as a parameter.

but that's just one possibility out of a lot and it really depends on your code/class-design which is the best way for you.

Comments

0

It seems that you are writing inside a class.
You can either create a property in your class and put the data inside it so you can use it later in your other method like this:

...
public $item_id;
public function desplubicarItem(){
    $this->item_id = JRequest::getCMD('id');
...

note that now you have to use $this->item_id in your methods

or you can call the second function inside the first function and pass the variable (if you can call one method and get one result from both functions).

public function desplubicarItem(){
    ...
    $this->getCuponesUtilizados($item_id);
}

public function getCuponesUtilizados(item_id){
    // now you can use $item_id here
}

1 Comment

Maybe better to create a setter rather than using the current method which would be making an unnecessary trip to the database and mandate that you always unpublish whatever item you are using in the other method.
0

You can make it a property or you can make it an argument or do a combination.

Since it is a public function you would need to make sure that the property or value for the argument has been set. For example you can add it as a required argument to the function, so that it has to be supplied when called, or you can add it as an $item_id = null argument and then, if it is not set, get the value from the request (as you did in the first function), but conditionally. For example:

public function desplubicarItem($item_id = null)
{
   $this->item_id = $item_id != null ? : JRequest::getCMD('id');
...
}

You can add setting it to the constructor for your class but then you will have to deal with the same issue in the constructor. But it really depends on what the whole class is doing.

One reason for doing it this way is: What if you want to use these methods when the id is not from the request, for example if you got the id from a query? I wouldn't put that limitation on your methods, just make that the fall back in the event the value is not supplied directly.Otherwise you are going to really limit your flexibility.

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.