0

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!!!

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
LordVader
  • 1
  • 3
  • : "It looks like you're writing your own ORM. Have you considered using one that's already written, tested, and widely supported like [RedBeanPHP](https://redbeanphp.com/), [Doctrine](http://www.doctrine-project.org/), [Propel](http://propelorm.org/) or [Eloquent](https://laravel.com/docs/master/eloquent)?" – tadman Feb 17 '20 at 22:14
  • Hi Tadman.... It's part of a Uni project, so I am stuck with the tools I have – LordVader Feb 17 '20 at 22:27
  • What you even need `CREATE VIEW result AS` for? See [When to use views in MySQL?](https://dba.stackexchange.com/questions/16372/when-to-use-views-in-mysql) – Martin Zeitler Feb 17 '20 at 22:29
  • In that case best of luck. Do try and pay attention to the little things though like `public` vs. `Public`. PHP may be case-insensitive most of the time but that doesn't mean you shouldn't be consistent. A single character error can cause huge problems so keeping your code clean and organized helps a lot. – tadman Feb 17 '20 at 22:31
  • One thing that could help with shaking out problems is adding unit tests to your code to verify that these functions work correctly. – tadman Feb 17 '20 at 22:32
  • The only thing that would help is getting rid of that view - and edit the table instead. – Martin Zeitler Feb 17 '20 at 22:36
  • Add error management to all query and see if one pops up with an error – nbk Feb 17 '20 at 22:44
  • Tadman.... I'll have a look back over the code and ensure everything is as it should be. – LordVader Feb 17 '20 at 22:53
  • Martin: The view stays. It was "suggested" by our lecturer that using views would be a good thing, and this isn't a "real world" application. I'm not a programmer and a lot of this is rather new to me. I'm surprised I've got anything even resembling working code. I'm just so close to having it work and its frustrating to hell that the flags are resetting. – LordVader Feb 17 '20 at 22:56
  • I'll try to add some unit testing as well. – LordVader Feb 17 '20 at 22:57
  • Is `$resetFlag = $result ->resetFlag();` a typo in your question, or does the code actually have a space before the `->`? – Sloan Thrasher Feb 18 '20 at 00:48
  • That's a typo. Hadn't noticed that. Thanks mate. – LordVader Feb 18 '20 at 10:48

1 Answers1

0

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.

LordVader
  • 1
  • 3