Jump to content
MakeWebGames

My new and edit methods in ACP


Sim

Recommended Posts

        
        public function bindParams(&$db)
        {
          $insert->bindParam(":name", $this->methodData->name);
          $insert->bindParam(":cost", $this->methodData->cost);
          $insert->bindParam(":lvl", $this->methodData->level);
          $insert->bindParam(":hp", $this->methodData->health);
          $insert->bindParam(":str", $this->methodData->strength);
          $insert->bindParam(":def", $this->methodData->defense);
          $insert->bindParam(":spd", $this->methodData->speed);
          $insert->bindParam(":hitpoints", $this->methodData->hitpoints);
        }

        public function method_new () {

            $crime = array();

            if (isset($this->methodData->submit)) {
                $hero = (array) $this->methodData;
                $errors = $this->validateHero($hero);
                
                if (count($errors)) {
                    foreach ($errors as $error) {
                        $this->html .= $this->page->buildElement("error", array("text" => $error));
                    }
                } else {
                    $insert = $this->db->prepare("
                        INSERT INTO heros (heroName, heroCost, heroLVL, heroHP, heroStr, heroDef, heroSpd, heroHitPoints)  VALUES (:name, :cost, :lvl, :hp, :str, :def, :spd, :hitpoints);
                    ");
                    
                    $this->bindParams($insert);
                    $insert->execute();


                    $this->html .= $this->page->buildElement("success", array("text" => "This hero has been created"));

                }

            }

            $hero["editType"] = "new";
            $this->html .= $this->page->buildElement("heroForm", $hero);
        }

        public function method_edit () {

            if (!isset($this->methodData->id)) {
                return $this->html = $this->page->buildElement("error", array("text" => "No hero ID specified"));
            }

            $hero = $this->getHero($this->methodData->id);

            if (isset($this->methodData->submit)) {
                $hero = (array) $this->methodData;
                $errors = $this->validateCrime($hero);

                if (count($errors)) {
                    foreach ($errors as $error) {
                        $this->html .= $this->page->buildElement("error", array("text" => $error));
                    }
                } else {
                    $update = $this->db->prepare("
                        UPDATE heros SET heroName = :name, heroCost = :cost, heroLVL = :lvl, heroHP = :hp,heroStr = :str, heroSpd = :spd, heroDef = :def, heroHitPoints= :hitpoints WHERE heroID= :id
                    ");

                    $this->bindParams($update);
                    $update->bindParam(":id", $this->methodData->id);
                    $update->execute();

                    $this->html .= $this->page->buildElement("success", array("text" => "This crime has been updated"));

                }

            }

            $hero["editType"] = "edit";
            $this->html .= $this->page->buildElement("heroForm", $hero);
        }

So what I did was create a new bimdParams method which just saves from duplicate bindParam code in the new and edit methods. This solves duplicate code issue, and easier to manage for editing or adding new things to DB if need be.

For those that don't know, the & symbol references the variable passed. 

So instead of $update = $update->bindParams($update) and returning $db from the bindParams method, the & automatically does this.

Link to comment
Share on other sites

23 minutes ago, KyleMassacre said:

Maybe what I would have done was looped through the form data, find keys that exist and bound the values to that. It would probably save you from either editing the bind param method you created every time you needed to bind a new param or binding them on the fly

Tried that, the methodData values isn't always DB values, they reference other things besides form values as well. I like shortcuts. I tried, I failed that way. Without making an exclude array anyway.

Then there's also the issue with file uploads, you would still have to manually bind these. 

 

Would be a nice update for @Dayo add a bindAllFormData somewhere somehow. 

  • Like 1
Link to comment
Share on other sites

I’m sure there is a way if you were to get creative. For example, I generally try to wrap my form data or “methodData” in another array key that has something to do with what I want to access. 
 

I mean to be honest, if this was something people were going to use then they should follow your rules just like with anything released. For example, if param 1 of a method/function requires a class object, people can’t put in an integer right?

For people’s input names they could use a predefined key that you want to check for, make them use names of their table columns, and search for that. You can force people to use a key of dbData[column_name] and just loop through $this->methodData->dbData and now you have all the array key=>values you need. 

  • Like 1
Link to comment
Share on other sites

GL method data also comtains URL Params in it. Maybe additional info as well. It's been awhile since I dumped the data. Will redump later to recheck.

Could always do fields = array(1,2,3); and loop threw that. I tend to use same field names as param :whatever, but not everyone follows this. Myself included at times.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...