Allow users to delete an adoptable

Forum
Last Post
Threads / Messages

tahbikat

Member
Member
Joined
Feb 11, 2014
Messages
408
Points
16
Age
30
Location
Louisiana
Mysidian Dollar
32,360
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:
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:
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:
public function doReleasePet(){

}
 
I think this'll work...?
PHP:
$mysidia->db->delete("owned_adoptables", "aid='{$this->aid} AND owner ='{$this->owner}'");
 
Last edited:
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}'");
 
Last edited:
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.
 
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:
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:
 
Oh, woops! You do need it after {$this->aid}, good job catching that!
PHP:
$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:
$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:
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.
 
Last edited:
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
 
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:
$mysidia->db->query("UPDATE adopts_pounds SET descendants = 'NULL' WHERE descendants = '{$this->adopt->getAdoptID()}'");
PHP:
$mysidia->db->query("UPDATE adopts_pounds SET mother = 'NULL' WHERE mother = '{$this->adopt->getAdoptID()}'");
PHP:
$mysidia->db->query("UPDATE adopts_pounds SET father = 'NULL' WHERE father = '{$this->adopt->getAdoptID()}'");
 
Last edited:
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:
/* . . . 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.
 
Last edited:
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?
 
I updated the code above, give it a shot?

I can also try to make an addendum to the family tree code to prevent nonexistent pets from displaying. That'll definitely be easier in the long run, I think. (Actually no, it wasn't, I learned something from HoF in the process.)

Basically, in the family tree code, we need to wrap the offspring in a try/catch statement, so find the foreach loop:
PHP:
foreach($offsprings as $offspring){
    if ($offspring != 0){
    	try { 
    		$child = new OwnedAdoptable($offspring);
        	$babies[] = ""; # THIS LINE IS JUST REALLY LONG SO I LEFT IT EMPTY, IT'LL HAVE STUFF IN IT
    	} catch (AdoptNotfoundException $anfe) { /* Ignore the error. */}
    }
}

I hope you can find that/it makes sense. @w@...
 
Last edited:
I'm now using code from both post #9 and post #11 just to be safe. While I was discussing this with HoF he expressed some of the same concerns I had, however, so it's worth bringing up:

We don't really know if other side effects will exist. I can't immediately fathom any, but that doesn't mean they don't exist. What if a pet is deleted half way through a trade? Will it corrupt the trade section for one person, both people, everyone? And, is it possible to to keep the pet's profile open, release it to the pound, and because you still have your pet's page open as if you own it - delete it? Would that break the pound? I haven't fully considered these things so please do some testing.

It's hard to officially approve of deleting pets when we don't know what could happen. :catfish:
 
Hmm, alright. I think I'll try getting a tester site up and do testing before releasing this on my site then. Those are some valid points made and could definitely turn into an issue if anyone were to abuse it.

EDIT: Alternatively, perhaps I can instead just make a page just like the pound and allow users to "release" their pets there, clearing any family data, and every few days the pets can be automatically (or manually) deleted thereafter. Pets on this page wouldn't be allowed for readoption.
 
Last edited:
Alright so coming back to this. :p The code that ignores the error when an offspring doesn't exist is working great. Is there a way to have this also work if mothers and fathers were deleted as well? Whenever a mother or father is deleted, the babies get an error on their pages saying so. I tried to look in the code and figure it out myself but not getting anywhere. ><
 
I'll work on this when I get back from... work. *sigh*

edit: See later post for solution.
 
Last edited:
Ahh Kyttias thank you! <3 It's no rush. I just happened to fiddle with it more and noticed that problem unfortunately.

I'm not using grandparents on my site at the moment so it wouldn't be a problem for me luckily, though others following this might find that useful. Not sure if I ever will display grandparents, so I'm mainly just focused on ignoring the error for parents as well. I also still need to do some testing to make sure there aren't any major errors with adding the deleting part. Plan on getting to that soon!
 
The best thing to do will be just to make a try/catch as we did before, actually, with the children. Telling you where to make edits is a little harder, so, from the thread where the family tree was made, post number four, the first bit of code. That. Where-ever you put that, no matter how you designed it within, it'll be easiest just to put this above it, so we have both:

PHP:
if ($adopt->mother != NULL){ try { $mother = new OwnedAdoptable($adopt->mother); } catch (AdoptNotfoundException $anfe){ $adopt->mother = NULL; } }
if ($adopt->father != NULL){ try { $father = new OwnedAdoptable($adopt->father); } catch (AdoptNotfoundException $anfe){ $adopt->father = NULL; } }
if ($adopt->grandmotherA != NULL){ try { $grandmotherA = new OwnedAdoptable($adopt->grandmotherA); } catch (AdoptNotfoundException $anfe){ $adopt->grandmotherA = NULL; } }
if ($adopt->grandmotherB != NULL){ try { $grandmotherB = new OwnedAdoptable($adopt->grandmotherB); } catch (AdoptNotfoundException $anfe){ $adopt->grandmotherB = NULL; } }
if ($adopt->grandfatherA != NULL){ try { $grandfatherA = new OwnedAdoptable($adopt->grandfatherA); } catch (AdoptNotfoundException $anfe){ $adopt->grandfatherA = NULL; } }
if ($adopt->grandfatherB != NULL){ try { $grandfatherB = new OwnedAdoptable($adopt->grandfatherB); } catch (AdoptNotfoundException $anfe){ $adopt->grandfatherB = NULL; } }

And you may not need the grandparent bits, of course.

So why do we need this code first?

Basically, the code from the post I referred to is checking whether or not the current adoptable has a parent held in its database column (it's checking for NULL, so, so long as something there, it should try to render something). Well, we're backing up a step. First we're checking if the field is not NULL - okay, something is there? Okay, but does that pet actually exist? We check that. And if doesn't exist? We inform the code it is NULL, therefore stopping the original code.

If something about this goes strangely, go ahead and send me the file via PM. :meow:
 
You really are the best! <3 It works, at least for mothers and fathers. Someone else would need to test grandparents. :p Thank yoouuu!
 
Still haven't tested if deleting adopts causes any errors, but did wanna say I instead just basically duplicated the pound and made it into the "Wild". Players can't adopt these pets but they can view them and their profiles. So far it seems to be working okay and I may just stick to this. I need to trim and clean up a lot of the code though.

Also working on getting pets to display randomly and limited to only maybe 50 or so at a time. Would be horrible to visit a page with 500+ pets there. :p

I'm also not sure if I wanna delete pets manually every so often (for tidyness), or just keep them there. Probably just gonna get my users to vote on this.

UWtIdwF.png
 

Similar threads

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

Threads
4,274
Messages
33,115
Members
1,602
Latest member
BerrieMilk
BETA

Latest Threads

Latest Posts

Top