0

Im making a website that works around a very basic MVC structure. I have three folders, the Model, View and Controller.
I want to display the content of the database in the dropdown once the page has loaded.

My view has:

<div id="appCalc"> <form id="applianceCalc" method="POST" > <?php
                if(isset($error))
                {
                    foreach($error as $error)
                    {
                        ?> <div class="alert alert-danger"> <?php echo $error; ?> </div> <?php
                    }
                }
                ?>
                Typical Appliance: 


                <select name="appliances"> 

                </select> 
                <br>
            Power Consumption:
            <input id="powerCon" name="txtPower" type="text" required></input> <br>
            Hours of use per day:
            <input id="hoursPD" name="txtHours" type="text" required></input> 
            <br> 
            <input type="submit" name="btn-appCalc" value="Calculate">
            </input> <input type = "submit" value="Reset" onclick="reset();">

The select dropdown that i want populated is called "appliances"

My model currently has:

<?php
require_once('../Controller/config.php');

class AppCalc 
{
    public $dbconn;

    public function __construct()
    {
        $database = new Database();
        $db = $database->dbConnection();
        $this->dbconn = $db;
    }

    public function getApps(){
        $stmt = $dbconn->prepare("SELECT Appliance FROM appliances");
        $stmt->execute();
        $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

}
   ?>

getApps is the function that retreives the data from the database.

and my Controller currently has:

<?php
require_once('../Model/applianceModel.php');

if(isset($_POST['btn-appCalc'])){
    $appPower = strip_tags($_POST['txtPower']);
    $appHours = strip_tags($_POST['txtHours']);

    if($appPower=="")   {
        $error[] = "Please Enter a Power Value";    
    }
    else if($appHours=="")  {
        $error[] = "Please Enter the Number of Hours";  
    }
}
?>

Im really confused and not sure how im supposed to call the method into the view to display the values from the database in the dropdown.
Any help will be appreciated.

Thanks!

Edited by Ts91

2
Contributors
1
Reply
6
Views
1 Year
Discussion Span
Last Post by TexWiller
0

remember the view is the output sent to the browser
suppose you have an array of records

you can prepare a string for your options like this

$options="";
foreach( $records as $k=>$v){
    $options.="<option value='$v'>$k</option>\n"
}

or put the cycle directly in the view (not a best practice)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.