Home » Php » PHP/MySQL resetting flags in a view

PHP/MySQL resetting flags in a view

Posted by: admin February 25, 2020 Leave a comment

Questions:

So, I have an application that I’m writing and at it is, for the most part, working well.

However, there’s some unexpected behaviour that I cannot, for the life of me, understand why it is behaving as such.

The code creates a view from a MySQL table, and then a series of questions are asked. These are simple yes/no questions, and (depending on the result) true flags are set to false. When there is only one true flag remaining, it then loads a page which is supposed to display a card with information about the character that was left.

The yes/no is iterating the view and changing flags as it should. However, when there is one character remaining, it’s resetting all the flags back to true, and displaying all the characters, instead of the one. It has to be something to do with the MySQL, but I cannot see what it could be.

Below is the results.class.php in which the finalResult method resides.

<?php

class result
{
    protected $Conn;

    public function __construct($Conn)
    {
        $this->Conn = $Conn;

    }

    public function dropResult()
    {
        $drop = "DROP VIEW if EXISTS result;";
        $stmt = $this->Conn->prepare($drop);
        $stmt->execute(array());
    }

    public function createResult()
    {
        $view = "CREATE VIEW result AS SELECT id, flag, name, image, quote FROM person;";
        $stmt = $this->Conn->prepare($view);
        $stmt->execute(array());
        return $view;
    }

    public function resetFlag()
    {
        $update = "UPDATE person SET flag = true";
        $stmt = $this->Conn->prepare($update);
        $stmt->execute(array());

        }

    public function selectResult()
    {
        $select= "SELECT id, flag from result";
        $stmt = $this->Conn->prepare($select);
        $stmt->execute(array());
        $select = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $select;
    }
    Public function iterateResult($id)
    {
        $changeFlag = "UPDATE result SET flag = false WHERE  id= :id";
        $stmt = $this->Conn->prepare($changeFlag);
        $stmt->execute(array(':id' => $id));

    }
    public function countResult()
    {
        $flag = "SELECT flag from result WHERE flag = true";
        $stmt = $this->Conn->prepare($flag);
        $stmt->execute(array());
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function finalResult()
    {
        $characterResult = "SELECT name, image, quote FROM result WHERE flag = true;";
        $stmt = $this->Conn->prepare($characterResult);
        $stmt->execute(array());
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

}


?>

And here is the results.php code

<?php
$result = new result($Conn);
$dropResult = $result->dropResult();
$createResult = $result->createResult();
$resetFlag = $result ->resetFlag();
$finalCharacter = $result->finalResult();
$smarty->assign('result', $finalCharacter);

?>

I’m sure it’s something really simple, but I just can’t see the wood for the trees here. Any tips?

PS… Originally, I had the selectResult method as the method that was called for $finalCharacter, so I created a new method out of sheer desperation!!!

How to&Answers:

Fixed it. Turns out that I had the resetFlag method in TWO controllers, so the method was being used in the result page and resetting the flags to 1. Removed the method call in the result page and bob’s your uncle. Thanks for the help… very much appreciated.