Mysidia Adoptables Support Forum

Mysidia Adoptables Support Forum (http://www.mysidiaadoptables.com/forum/index.php)
-   Questions and Supports (http://www.mysidiaadoptables.com/forum/forumdisplay.php?f=18)
-   -   Allow users to delete an adoptable (http://www.mysidiaadoptables.com/forum/showthread.php?t=5067)

tahbikat 03-07-2016 02:31 PM

Allow users to delete an adoptable
 
I'm attempting to make a feature where users can release a pet to the wild, which basically just means permanently deleting it.

I've just copied and modified my pets' bio functions in their myadopts.php and myadoptsview.php to look like below. In class_ownedadoptables.php, I just have a blank public function "doReleasePet()" because I'm honestly just scared to try experimenting with deleting things. :P Afraid I'll end up deleting a whole database table or something lol. Can someone help me out? ;-; Am I going in the right direction at least?

myadopts.php
PHP Code:

public function releasepet(){
    
$mysidia Registry::get("mysidia");        
    if(
$mysidia->input->post("submit")){            
        
$this->adopt->doReleasePet();
    }
    
$this->setField("adopt"$this->adopt);        
    
$this->setField("image"$this->image);            


myadoptsview.php
PHP Code:

public function releasepet(){
    
$mysidia Registry::get("mysidia");
    
$adopt $this->getField("adopt");        
    
$image $this->getField("image");        
    
$document $this->document;
    
    if(
$mysidia->input->post("submit")){
        
$document->setTitle("Successfully Released Pet");
        
$document->add($image);
        
$document->add(new Comment("<br/>You've successfully released your creature to the wild, where it will live out the rest of its days. "));
        return;
    }
    
    
$document->setTitle("Release ".$adopt->getName());
    
$document->add($image);
    
$document->addLangvar("<br/>You can release {$adopt->getName()} to the wilderness here. Are you sure you want to do this?<br/>");
    
    
$releaseButton = new FormBuilder("releasebutton""""post");
    
$releaseButton->buildButton("Release!""submit""submit");
    
$document->add($releaseButton);           


class_ownedadoptables's doReleasePet()
PHP Code:

public function doReleasePet(){




Kyttias 03-07-2016 04:16 PM

I think this'll work...?
PHP Code:

$mysidia->db->delete("owned_adoptables""aid='{$this->aid} AND owner ='{$this->owner}'"); 


tahbikat 03-07-2016 11:10 PM

Getting errors. I tried adding this to the doReleasePet in the class_ownedadoptable.php and got a fatal error.
Code:

Fatal error: Call to a member function delete() on null in /home/mysgardia/public_html/classes/class_ownedadoptable.php on line 103
Moved it to the function in myadopts.php and it displayed this error on page:
Code:

Database error 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Lavie'' at line 1
EDIT: I changed it to this to do some experimenting (after backing up db lol) and it said I successfully released the pet yet I still have it. :<
Code:

$mysidia->db->delete("owned_adoptables", "aid='{$this->aid}'");

Kyttias 03-07-2016 11:14 PM

Check that there's not an extra quotation mark at the end? That it ends with:
'{$this->owner}'");
not
'{$this->owner}''");

There was maybe a 30 second window in which the typo existed and I'll be surprised if you managed to grab it then.

tahbikat 03-07-2016 11:52 PM

Yea, I made sure everything looked right after adding it. Gives me this error on the myadopts.php page:
Code:

Database error 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' AND owner =''' at line 1
This is how I have it written:
PHP Code:

public function releasepet(){
    
$mysidia Registry::get("mysidia");        
    if(
$mysidia->input->post("submit")){           
       
$mysidia->db->delete("owned_adoptables""aid='{$this->aid} AND owner ='{$this->owner}'");   
    }
    
$this->setField("adopt"$this->adopt);        
    
$this->setField("image"$this->image);            


Edit: when I put a quote after {$this->aid}' it gives me success but i still have the pet. :desudesudesu:

Kyttias 03-08-2016 02:15 AM

Oh, woops! You do need it after {$this->aid}, good job catching that!
PHP Code:

$mysidia->db->delete("owned_adoptables""aid='{$this->aid}' AND owner ='{$this->owner}'"); 

You really really shouldn't still have the pet after that, though. =/ You sure? There is another way to write this, hang on...
PHP Code:

$mysidia->db->query("DELETE FROM adopts_owned_adoptables WHERE aid='{$this->aid}' AND owner ='{$this->owner}'"); 

edit:

However, the problem is where you're putting it. It can't find the data you're telling it to find because you can't call $this->aid inside myadopts.php, because that file does not have a variable set at the top called $aid. You'd have to use $this->adopt->getAdoptID() instead (this file has a variable called $adopt which is a reference to the class_ownedadoptables.php where getAdoptID() exists, possibly even $this->adopt->aid would work with the proper permissions).

I was thinking you'd put this inside doReleasePet(), since you were calling with $this->adopt->doReleasePet(); but left it empty inside class_ownedadoptables.php? I used aid='{$this->aid}' assuming it'd be there (because $aid exists in the context of this file) if you don't want to do that, then I suggest just inside myadopts.php:
PHP Code:

public function releasepet(){ 
    
$mysidia Registry::get("mysidia");         
    if(
$mysidia->input->post("submit")){            
       
$mysidia->db->delete("owned_adoptables""aid='{$this->adopt->getAdoptID()}'");    
    } 
    
$this->setField("adopt"$this->adopt);         
    
$this->setField("image"$this->image);             


The point of having it in the class file, as a function, instead of in the myadopts file is reusability. If you plan on needing an adopt delete function elsewhere on the site, this would be good to have as a function in the class instead. For example, if you're still using the pound at all, you could technically write in a feature that would automatically delete pets that have been in the pound longer than a month. However, if you can't think of another use for that line of code, then it's fine not needing to be inside a function at all.

tahbikat 03-08-2016 10:17 PM

Ahh perfect!

When I tried putting mysidia->db->query part in class_ownedadopts it still gave me an error, so I just used the second bit you gave me instead and it worked! I don't think I'll need to use this anywhere else so I'll just keep it in myadopts.php file for now.

Thank you so so much Kyttias! <3

tahbikat 03-08-2016 11:08 PM

Okay, new question.

I wanted this primarily so that users could delete unwanted offspring their creatures happen to breed. I would also need the offspring's ID to be deleted (more like updated to NULL) from their parents' descendants column in owned_adopts. Is this possible? :c

Otherwise by just deleting the offspring's data, the parents' profile pages bring up an error since the child no longer exists. Also vice versa if a parent was deleted, its ID would need to be updated to NULL in the child's mother/father columns.

I'm using pretty much the exact coding that you and Abronsyth discussed in the Lineage thread.

EDIT: Would this work, or something like this?

PHP Code:

$mysidia->db->query("UPDATE adopts_pounds SET descendants = 'NULL' WHERE descendants = '{$this->adopt->getAdoptID()}'"); 

PHP Code:

$mysidia->db->query("UPDATE adopts_pounds SET mother = 'NULL' WHERE mother = '{$this->adopt->getAdoptID()}'"); 

PHP Code:

$mysidia->db->query("UPDATE adopts_pounds SET father = 'NULL' WHERE father = '{$this->adopt->getAdoptID()}'"); 


Kyttias 03-08-2016 11:51 PM

No, partially because editting the adopts_pounds table won't help you, lol?

The babies are stored as an psuedo-array with the parents. You have to take the adopt, identify its parents, extract the arrays from the database for each parent, explode these arrays from a strings into an an actual arrays PHP can work with, carefully remove the id of the pet you're deleting, implode it back into a string, and reinsert it into the database.

Confirmed working (edit: Updated Mar 9 @ 2:21AM EST!):
PHP Code:

/* . . . REMOVE PET FROM FATHER'S LIST OF CHILDREN . . . */
# Find the father.
$father = new OwnedAdoptable($this->adopt->father); 
# Open the array of the father's kids.
$father_kids explode(","$father->offsprings); 
# Remove specific adoptable from the list of kids.
foreach($father_kids as $i => $kid){
  if(
$kid == $this->adopt->getAdoptID()){
    unset(
$father_kids[$i]);
  }
}
# Close the array of the father's kids.
$father_kids implode(",",$father_kids); 
# Put the updated list of kids back in the database.
$mysidia->db->update("owned_adoptables", array("offsprings" => $father_kids), "aid = '{$father->aid}'");


/* . . . REMOVE PET FROM MOTHER'S LIST OF CHILDREN . . . */
# Find the mother.
$mother = new OwnedAdoptable($this->adopt->mother); 
# Open the array of the mother's kids.
$mother_kids explode(","$mother->offsprings); 
# Remove specific adoptable from the list of kids.
foreach($mother_kids as $i => $kid){
  if(
$kid == $this->adopt->getAdoptID()){
    unset(
$mother_kids[$i]);
  }
}
# Close the array of the mother's kids.
$mother_kids implode(",",$mother_kids); 
# Put the updated list of kids back in the database.
$mysidia->db->update("owned_adoptables", array("offsprings" => $mother_kids), "aid = '{$mother->aid}'"); 

Snuggle the above code directly up to (and probably right before) where the pet itself is deleted.

tahbikat 03-09-2016 12:13 AM

Oh whoops lol! I kinda just copied that from something else and forgot to change the table.

But aw crud. :/ Hmm, I guess that foils my plans until a later date haha. Do you think there would be a way to instead check if the adopt exists before displaying it as a parent and/or offspring on the levelup.php files? Or would that be just as tedious?


All times are GMT -5. The time now is 05:13 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.