Question Regarding Calling Data from the DB

Forum
Last Post
Threads / Messages

dlithehil

Member
Member
Joined
Jun 10, 2022
Messages
62
Points
8
Mysidian Dollar
499
OKAY... I have been at this from every angle I can think of.
I have 3 stats- strength, speed, and stamina - because I want to implement an npc battle system. They're in the owned_adoptables table.... Yet any time I try to reference one of them, it bugs out my site and leaves me with a white screen.

  Spoiler: The Database Entries 

My Code:
PHP:
public function publicprofile(){
        $mysidia = Registry::get("mysidia");
        $document = $this->document;            
        $adopt = $this->getField("adopt");        
        $document->setTitle("Viewing {$adopt->getName()}'s Profile");
        $ownername= $adopt->getOwner();
    //... It continues but this is where I define my variables.

If I try to add $str = $adopt->getStrength(); or $str = $adopt->getField("strength"); it messes up the entire page. I don't even have to call it in the code. Simply defining the variable makes it go funky. Any help would be GREATLY appreciated.
 
Hi again lol. So, to call a value from the DB using something like getStrength you always need to define it first in the model. So for this it will be model/domainmodel/ownedadoptable.php. Like we did with health.

PHP:
/* Add this to the protected functions */
public $strength;

/* Add this to the public functions */
public function getStrength(){
        return $this->strength;
    }

    public function setStrength($strength, $assignMode = ""){
        if($assignMode == Model::UPDATE) $this->save("strength", $strength);
        $this->strength = $strength;
    }

Now that getStrength is defined we can open levelupview and use this to call it wherever you want:

PHP:
$adopt->getStrength();

You can't use $adopt->getField("strength") because getField isn't defined in ownedadoptable.php so it throws an error. I think your site has error reporting turned off because if you didn't it would show an error that could lead you to what's wrong. How you could turn it on would depend on the host you use. For example, here's the error I get when trying to use $adopt->getField("strength"):

2023-11-15 07_17_35-127.0.0.1_mysidia_levelup_profile_2 and 10 more pages - Personal - Microso...png

You can call a value directly from the database like this:

PHP:
/* This calls the database, I use $adoptDB instead of just $adopt because $adopt
is already defined and you can't use it again or it will get overwritten */
$adoptDB = $mysidia->db->select("owned_adoptables", array(), "aid='{$adopt->getAdoptID()}'")->fetchObject();

/*The above selects the owned_adoptables table where the adoptID matches the profile you're viewing*/

/* Then you can use the below to call the value you want */
$profile->add(new Comment("Strength is: {$adoptDB->strength}"));

However, I would always recommend defining getStrength rather than adding a direct call to the DB every time. Saves on code and when you get used to it it's quite user friendly.

And if you want something to increase your adopts strength you would use setStrength to update to the new value.

Off the top of my head, you would do something like (untested):

PHP:
$oldStrength = $adopt->getStrength();
$newStrength = $oldStrength + 2; //Can change to whatever value you want

You could use that as a way to make an item that could increase your adopts strength. Or maybe a training system of sorts.

Hope that helps!
 

Similar threads

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

Threads
4,277
Messages
33,118
Members
1,602
Latest member
BerrieMilk
BETA

Latest Threads

Top