简体   繁体   中英

Pass a parameter to a method using PHP custom MVC

Im using a custom MVC in PHP

I have two tables.

Controles and professors

professors table contains an id of course --> id_professor

controles table contains a field called --> id_professors in which I store a list of profs ids this way ==> 1;2;5;9

In my datatable Im retrieving a list of controles from controles table

for the Model , Im using this class

public function fetchControlesListe(){
    stmt = $this->db->prepare("SELECT * FROM controles ");      
    $stmt->setFetchMode(PDO::FETCH_ASSOC);  
    $stmt->execute();
    return $stmt->fetchAll();       
}

Controller

liste_controles.php

class Liste_Controles extends Controller {
    function __construct(){
        parent::__construct();
    }
        $this->view->fetchCtrlListe= $this->model->fetchControlesListe();
        $this->view->render('liste_controles/index');
    }

VIEW

<table>
    <thead>
        <tr>                                                
            <th>N°</th>
            <th>Module</th>             
            <th>Salle</th>
            <th>Surveillants</th>               
        </tr>
    </thead>
    <tbody>
    <?php foreach($this->fetchCtrlListe AS $key=>$value): ?>
    <tr>                                                
        <td><?php echo $controle['id_controle']; ?></td>
        <td><?php echo $controle['intitule_module']; ?></td>            <
        <td><?php echo $controle['intitule_salle'];?></td>
        <td>
            <?php foreach($this->prof as $profs): ?>

HERE, I would like to retrieve the list of profs from professors table and the field id_profs in professors table stores an array, as i said before, containing a list of ids (Ex: 1;2;3;4;5 ) I would like to loop through that array and retrieve the name of professor from professors table if the id_professor Exists in that array . I know I have to create another METHOD but The PROBLEM is i don't know how to pass parameters in the method and call it from the controller to pass it to the view . and the param would be $controle['id_professor']

            <?php endforeach; ?>
        </td>
    </tr>
<?php endforeach; ?>    
    </tbody>
</table>

The result would be like this:

在此处输入图片说明

EDIT :

I know that is not a good idea to store lots of ids as an array, but my question I just want to know how to do a foreach loop inside another foreach loop using another method with a paramater that will be passed from the first loop

There isn't one solution but what you can do is, because you know that id_professors is a string composed of ids separated by commas, you can retrieve an array of identifiers this way :

$ids = explode(",", $profs[id_professors']);

and then

<?php foreach($this->prof as $profs): ?>
   <?php $ids = explode(",", $profs['id_professors']); ?>
   <?php foreach($ids as $id): ?>
      //DO STUFF with $id
   <?php endforeach; ?>
<?php endforeach; ?>

Notice that the entire thing is not very beautiful because of opening/closing php tags constantly but I hope you'll make a wonderful templates system for your MVC framework :)

From there, it would not be that nice to access the database directly from the view so a better (not perfect) solution would be to provide the view an array made of professors like this one :

$this->view->profs_list = array(
1 => "Prof 1",
2 => "Prof 2",
//etc.
);

with the key of the array being the profs' identifier.

Then you can retrieve their name through the loop :

<?php foreach($this->prof as $profs): ?>
   <?php $ids = explode(",", $profs['id_professors']); ?>
   <?php foreach($ids as $id): ?>
      <span><?php echo $this->profs_list[$id]; ?></span><br />
   <?php endforeach; ?>
<?php endforeach; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM