Mysidia Adoptables Support Forum  

Home Community Mys-Script Creative Off-Topic
Go Back   Mysidia Adoptables Support Forum > Mysidia Adoptables > Questions and Supports

Reply
 
Thread Tools Display Modes
  #1  
Old 02-07-2017, 06:15 PM
Dinocanid's Avatar
Dinocanid Dinocanid is offline
Member
 
Join Date: Aug 2016
Location: Maryland, USA
Posts: 512
Gender: Unknown/Other
Credits: 21,416
Dinocanid is on a distinguished road
Default How to subtract a value

This is a very noobish question, but how do you properly subtract a value from all rows in a table? I've tried this:
PHP Code:
$mysidia->db->update("owned_adoptables", array("mood"  => -2)); 
Which changes the value to -2 rather than subtracting 2. And I've tried this:
PHP Code:
$mysidia->db->update("owned_adoptables", array("mood"  2)); 
Which doesn't do anything.
__________________
Reply With Quote
  #2  
Old 02-07-2017, 08:43 PM
Kyttias's Avatar
Kyttias Kyttias is offline
Premium Member
 
Join Date: Jan 2014
Posts: 843
Gender: Unknown/Other
Credits: 36,335
Kyttias is on a distinguished road
Default

You'll have to get the current value(s) first and then subtract. The update function just sets the variable to whatever you tell it to, but cannot math at it.

This would be an example of how to reduce the current user's money:
PHP Code:
$moneyleft $mysidia->user->money $cost;
$mysidia->db->update("users", array("money" => $moneyleft), "username = '{$adopt->owner}'"); 
Going off that, I can do this (but I'm not sure where your code is so this example goes over the top - I'm specifying exactly which adoptable's mood is being modified):
PHP Code:
$adopt = new OwnedAdoptable($aid);
$mood $mysidia->db->select("owned_adoptables", array("mood"), "aid = '{$adopt->getAdoptID()}'")->fetchColumn();
$mysidia->db->update("owned_adoptables", array("mood" => $mood 2), "aid = '{$adopt->getAdoptID()}'"); 
But, again, that might be overboard. The above should work on 'any' page, theoretically. However, $this->adopt->mood might grab the value just fine in levelup.php, or in class_ownedadoptable.php, $this->mood. I'd personally recommend creating a function in class_ownedadoptable.php and calling that from whatever page you need. As mood is a custom variable, you should list it at the top of class_ownedadoptable.php with the other variables if you haven't yet.

PHP Code:
// using "$this->adopt->" in levelup.php:
$mysidia->db->update("owned_adoptables", array("mood" => $this->adopt->mood 2), "aid = '{$this->adopt->getAdoptID()}'");

// or perhaps just "$this->" in class_ownedadoptable.php:
$mysidia->db->update("owned_adoptables", array("mood" => $this->mood 2), "aid = '{$this->aid}'"); 
__________________
Please do not contact me. I cannot offer tech support on any coding I've done prior to 2018.

Last edited by Kyttias; 02-07-2017 at 08:50 PM.
Reply With Quote
  #3  
Old 02-07-2017, 09:31 PM
IntoRain's Avatar
IntoRain IntoRain is offline
Moderator
 
Join Date: Jul 2013
Location: Portugal
Posts: 461
Gender: Female
Credits: 14,456
IntoRain is on a distinguished road
Default

You can also create your own database function to add that functionality from mysql to Mysidia, so you avoid having two transactions, select and then update

class_database.php

PHP Code:
//add this function
public function update_decrease($tableName, array $rows$value$clause NULL){
        return 
$this->_query($tableName$rows'update_decrease'$clause$value);
}

//modify query function - added $value = NULL
private function _query($tableName, array $data$operation$clause NULL$value NULL){
if ( ! 
is_string($tableName)){
            throw new 
Exception('Argument 1 to ' __CLASS__ '::' __METHOD__ ' must be a string');
        }

        
// added "update_decrease" to this list
        
if ( ! in_array($operation, array('insert''update''update_decrease''select''select_distinct''delete'))){
            throw new 
Exception('Unknown database operation.');
        }
    
             
// <new code>
        
if(!$value) {
            
$query call_user_func_array(array(&$this'_' $operation '_query'), array($tableName, &$data));
        }
        else {
            
$query call_user_func_array(array(&$this'_' $operation '_query'), array($tableName, &$data, &$value));    
        }
              
//</new code>
        
        
if ( ! empty($clause)){
            
$query .= ' WHERE ' $clause;
        }
        
//The comments can be removed for debugging purposes.
        //echo $query;
        
$stmt $this->prepare($query);
        
$this->_bind_data($stmt$data);

        if ( ! 
$stmt->execute()){
            
$error $stmt->errorInfo();
            throw new 
Exception('Database error ' $error[1] . ' - ' $error[2]);
        }

        
$this->_total_rows[] = $stmt->rowCount();
        return 
$stmt;

}
//add this function, under _update_query() maybe to keep everything together
private function _update_decrease_query($tableName, &$data, &$num){
        
$setQuery = array();
        foreach (
$data as $field){
            
$setQuery[] = '`' $field '` = `' $field "` -" $num;
        }
        return 
'UPDATE ' $this->_prefix $tableName '
                  SET ' 
implode(', '$setQuery);

Now you can do stuff like

PHP Code:
//decrease mood of every owned adoptable by 2
$mysidia->db->update_decrease("owned_adoptables", array("mood"), 2);

//decrease mood and totalclicks by 1, when an adoptable has totalclicks = 1
$mysidia->db->update_decrease("owned_adoptables", array("mood""totalclicks"), 1"totalclicks = 1"); 

It depends on your taste. I've been adding some things from mysql to mysidia's database class in the last few days, that's why I'm giving this option xD
__________________


asp.net stole my soul.
Reply With Quote
  #4  
Old 12-04-2018, 05:28 AM
draugluin's Avatar
draugluin draugluin is offline
Member
 
Join Date: Oct 2011
Location: germany
Posts: 116
Gender: Unknown/Other
Credits: 4,759
draugluin is on a distinguished road
Default

Yeah ... that's great ! Thank you for sharing this
__________________
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 02:27 AM.

Currently Active Users: 214 (1 members and 213 guests)
Threads: 3,949, Posts: 31,447, Members: 15,539
Welcome to our newest members, Blufox.
BER





What's New?

What's Hot?

What's Popular?


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2018, vBulletin Solutions Inc.
vBCommerce I v2.0.0 Gold ©2010, PixelFX Studios
vBCredits I v2.0.0 Gold ©2010, PixelFX Studios
Emoticons by darkmoon3636