Display data into dropdown list from database using PHP MVC

124
January 20, 2022, at 02:30 AM

i have tired to display animal id in views dropdown list when i want add a new animal in my database i cant call id of animal check image beloW i wanna to use loop for in view I have a model

    <?php 
Class Medicines 
{
        function getTableMedicines()
        {
            $DB = new Database();
            $result = $DB->read("SELECT * FROM medication");
            return $result;
        }
        function getMedicinesById($id)
        {
            $DB = new Database();
            $result = $DB->read("SELECT id, animal_id, docteur, datereg, datedebut, datefin, daterdv FROM medication WHERE animal_id=$id");
            return $result;
        }
        function updateMedicinesById($id,$docteur,$datereg,$datedebut,$datefin,$daterdv)
        {
            $DB = new Database();
            $sql = ("UPDATE `medication` SET `docteur`='$docteur',`datereg`='$datereg',`datedebut`='$datedebut',`datefin='$datefin',`daterdv`='$daterdv' WHERE `id` = '$id'");
            // le model appele le Database (DAO) et pas mysqli
            if(false === $DB->write($sql))
            {
                return false;
            }
            return true;
        }
        function deleteMedicinesById($id)
        {
            $DB = new Database();
            $sql = ("DELETE from medication WHERE `id` = $id");
            if(false === $DB->write($sql)) {
                return false;
            }
            return true;
        }
        function addMedicines($docteur, $datereg, $datedebut, $datefin, $daterdv, $idAnimal)
        {
            $_SESSION['error'] = "";
            $arr['docteur'] = $docteur;
            $arr['datereg'] = $datereg;
            $arr['datedebut'] =$datedebut;
            $arr['datefin'] = $datefin;
            $arr['daterdv'] = $daterdv;
            $arr['id_animal'] = $idAnimal;
            $query = "insert into medication (docteur,datereg,datedebut,datefin,daterdv,id_animal) values (:docteur,:datereg,:datedebut,:datefin,:daterdv,:id_animal)";
            $DB = new Database();
            $data = $DB->write($query,$arr);
            return $data;
        }
        function show_Ids()
        {
            $DB = new Database();
            $result = $DB->read("SELECT id_animal FROM medication");
            return $result;
        
        }      
}
?>

the parms to get data from my database medication is In the localhost i dont have any error for this parms My Controller

    <?php  
Class Medicines_ctrl extends Controller 
{
    
    
    function index()
    {
        $data['page_title'] = "Medicines";
        if(isset($_SESSION['user_name'])==NULL){
            // Haven't log in
            header("Location: login");
            die();
        }
        $medicines = $this->loadModel("medicines");
        
        // comme kayen action edit w id f get donc yedkhol direct hna
        //donc kayen deux choix ndiro action f la balise form
        //deuxieme choix ndiro had la partie apres l post hab ykol ida kayen post joz direct liha
        // cette partie c'est pour evité d'utilise plusieur controller donc on va utiliser le user_ctrl
        if(isset($_GET['action'])) {
            // pour verifie le type de l'action a executé
            if(isset($_GET['id']) && $_GET['action'] == 'edit') {
                // pour recuper que le medicines avec id
                $data['entity'] = $medicines->getMedicinesById($_GET['id']);
                $this->view("medicines_edit",$data);
                die();
            }
            
            // pour verifie le type de l'action a executé
            if(isset($_GET['id']) && $_GET['action'] == 'delete') {
                // pour recuper que le medicines avec id
                $data['entity'] = $medicines->deleteMedicinesById($_GET['id']);
                $data['entity'] = $medicines->getTableMedicines();
                $this->view("medicines_list",$data);
                die();
            }
            
            if($_GET['action'] == 'add') {
                // pour recuper que le medicines avec id
                $this->view("medicines_add",$data);
                die();
            }
            
        }
        // ici aussi il faut metrre le traitment ta3 l post ta3 l formulaire edit
        //on peux debegi avec deux chose 
        //soit error_log sans arrete l'executon 
        // soit var_dump();die();
        // l post mafihch l id 
        if(isset($_POST['action'])) {
            if(isset($_POST['id']) && $_POST['action'] == 'update') {   
                //tous les champs from POST donc le formulaire edit medicines  
                $docteur = $_POST['docteur'];
                $datereg = $_POST['datereg'];
                $datedebut = $_POST['datedebut'];
                $datefin = $_POST['datefin'];
                $daterdv = $_POST['daterdv'];
                $id = $_POST['id'];
                // controler appele l model avec les params et pas la base
                $result = $medicines->updateMedicinesById($id, $docteur, $datereg, $datedebut, $datefin, $daterdv);
            }
            if($_POST['action'] == 'create') {  
                $id_animal = $_POST['id_animal']; 
                $docteur = $_POST['docteur'];
                $datereg = $_POST['datereg'];
                $datedebut = $_POST['datedebut'];
                $datefin = $_POST['datefin'];
                $daterdv = $_POST['daterdv'];
                $result = $medicines->addMedicines($docteur, $datereg, $datedebut, $datefin, $daterdv, $id_animal);
            }
        }
        
        // pour recuperer tout les medicines pour la liste
        $data['entity'] = $medicines->getTableMedicines();
        $this->view("medicines_list",$data);
        $data['ids'] = $medicines->show_Ids();
        
    }
}

My View

    <?php $this->view("includes/header",$data);?>
<?php $this->view("includes/sidenav",$data);?>
  <main class="main-content position-relative max-height-vh-100 h-100 border-radius-lg ">
    <div class="container-fluid py-4">
      <div class="row">
        <div class="col-12">
          <div class="card my-4">
            <div class="card-header p-0 position-relative mt-n4 mx-3 z-index-2">
              <div class="bg-gradient-primary shadow-primary border-radius-lg pt-4 pb-3">
                <h6 class="text-white text-capitalize ps-3">Ajouter Un Docteur</h6>
              </div>
            </div>
            <div class="card-body px-0 pb-2"> 
                <div class="card-body">
                  <form role="form" method="POST" action="medicines_ctrl">
                    <label class="form-label">Bovins</label>
                      <div class="input-group input-group-outline mb-3">
                        <select class="form-control">
                        <option value="none" selected="selected">------------Selectioné un animal------------</option>
                          <!----- Displaying fetched cities in options using foreach loop ---->
                          <?php foreach($ids as $animal_id):?>
                          <option value="<?php echo $animal_id->id?>"><?php echo $animal_id->id?></option>
                          <?php endforeach;?>
                        </select>
                      </div>
                    <label class="form-label">Le Medecin</label>
                    <div class="input-group input-group-outline mb-3">
                      <input type="text" name="docteur"  class="form-control">
                    </div>
                    <label class="form-label">Date de création</label>
                    <div class="input-group input-group-outline mb-3">
                      <input type="date" name="datereg" class="form-control">
                    </div>
                    <label class="form-label">Date de Début</label>
                    <div class="input-group input-group-outline mb-3">
                      <input type="date" name="datedebut"  class="form-control">
                    </div>
                    <label class="form-label">Date de Fin</label>
                    <div class="input-group input-group-outline mb-3">
                      <input type="date" name="datefin"  class="form-control">
                    </div>
                    <label class="form-label">Date de RDV</label>
                    <div class="input-group input-group-outline mb-3">
                      <input type="date" name="daterdv" class="form-control">
                    </div>
                    <div class="text-center">
                      <button type="submit" name="action"  value = "create" class="btn btn-lg bg-gradient-primary btn-lg w-100 mt-4 mb-0">Ajouter</button>
                    </div>
                  </form>
                </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </main>
<?php $this->view("includes/footer.php", $data); ?>
<?php $this->view("includes/scripts.php", $data); ?>

the option select in my view page this is my view items i get all data from database else id of animal table Please check images below for more information about pages and errors and database animal tablemedication tableview html select

Rent Charter Buses Company
READ ALSO
Why Sequelize Raw Queries don&#39;t return records from MySQL when I search by date?

Why Sequelize Raw Queries don't return records from MySQL when I search by date?

I have a MySQL database and I try to search in a tale using created_at and updated_at fields which are timestamp fields using this query

92
Web Push Same Subscriber Different Endpoint Issue

Web Push Same Subscriber Different Endpoint Issue

I am trying to implement the web-push service in one of my projectsSo, I actually need to know the actual subscriber numbers in my app

110
Why is the html view port meta tag breaking my CSS when I want to make my page responsive?

Why is the html view port meta tag breaking my CSS when I want to make my page responsive?

I am working on a small website for uni and have started out by designing the mobile site firstIt looked exactly how I wanted it to look on a 320px mobile device

94
SOLVED - addClass only once onClick ReactJS

SOLVED - addClass only once onClick ReactJS

Hi I'm new to ReactJS so I'm importing some work of mine to my project and turning it into componentsI have a span acting as a button with keyframe animations, but when i load the page it gets animated

126