Old Code

We've all been there. Writing code and then just discarding it. I found some old code I did back in 2011 which had some funny moments, so decided to share it here ^^


class Questions
    {
    // Survey variables
    protected $questions = array();
    protected $surveytitle;
    protected $surveydesc;
    protected $questioncounter;
    protected $areacounter;

    // Holds survey styles
        // AreaStart, AreaStart2, AreaEnd, QuestionStart, QuestionEnd
    protected $styles = array();

    // Function to create new area
    function CreateArea($name)
        {
        $this->questions[] = "Area|" . $name;
        }

    // Function to create new question
    function CreateQuestion($name)
        {
        $this->questions[] = "Question|" . $name;
        }

    // Function to go thru guestions.
    // Send question start and end tag and area start, start2 and end tags
    function GoThruQuestions()
        {
        $areastarted = 0;
        $questionnumber = 0;
        $areanumber = 0;
        $i = 0;
        $questioncount = count($this->questions);

        // Replace tags inside styles with references
        foreach ($this->styles as $key => $arrdata)
            {
            $arrdata = str_replace("[areanumber]", &$areanumber, $arrdata);
            $arrdata = str_replace("[questionnumber]", &$questionnumber, $arrdata);
            }

        // Go thru all the questions
        foreach ($this->questions as $key => $arrdata)
            {
            // Explode data to datatype|data
            $row = explode("|", $arrdata);
            $datatype = $row[0]; $data = $row[1];

            // Area Start
            if ($datatype == "Area" && $areastarted == 0)
                {
                echo $this->styles["AreaStart"] . $data . $this->styles["AreaStart2"];
                $areanumber++;
                }

            // Area End
            if ($i+1 == $questioncount || ($datatype == "Area" && $areastarted == 1))
                {
                echo $this->styles["AreaEnd"];
                $areastarted = 0;
                $questionnumber = -1;
                }

            // Question
            if ($datatype == "Question")
                {
                echo $this->styles["QuestionStart"] . $data . $this->styles["QuestionEnd"];
                }

            $i++;
            $questionnumber++;
            } // -Foreach
        }

    // Getters and setters
    function GetTitle() { echo $this->surveytitle; }
    function SetTitle($title) { $this->surveytitle = $title; }
    function GetDescription() { echo $this->surveydesc; }
    function SetDescription($desc) { $this->surveydesc = $desc; }
    function SetStyle($name, $style) { $this->styles[$name] = $style; }
    }