Help to implement the dropdown list. Model:

function region_get(){ $result = $this->mysqli->query("SELECT * FROM t_gs_regions ORDER BY gs_regions_name ASC"); $n=$result->num_rows; for($i=0; $i<$n; $i++) { $row6 = mysqli_fetch_assoc($result); $regions[] = $row6; } return $regions; } 

Controller:

 $reg = region_get(); 

View:

 <select name="regions_id"> <option value="">Π’ΠΈΠ±Π΅Ρ€ΠΈΡ‚Π΅ ΠΈΠ· списка</option> <?php foreach ($regions as $r):?> { '<option value=<?=$r['gs_regions_id']?>><?=$r['gs_regions_name']?>'</option>'; } <?php endforeach ?> } else { '<option value="">Бписок нСдоступний</option>'; } ?> </select> 
  • And the question is what? Where is your problem? What mistake? - MichaelPak 2:46 pm
  • The list is not displayed. Available from the list and the List is not available - Alena
  • Make var_dump($regions); at the end of the model. Is the array full? - MichaelPak
  • And why do you have $reg in the controller and $regions in the view? - MichaelPak
  • In view fixed, var_dump did not display anything - Alena

2 answers 2

If you have an MVC application, you should get something like:

 <?php class Model { private $mysqli; public function getRegions() { $result = $this->mysqli->query("SELECT * FROM t_gs_regions ORDER BY gs_regions_name ASC"); $regions = array(); if ($result) { while($row = $this->mysqli->fetch_assoc($result)) { $regions[] = $row; } } return $regions; } } ?> <?php class Controller { public function regionAction() { $model = new Model(); $regions = $model->getRegions(); return array( 'regions' => $regions ); } } ?> <select name="regions_id"> <option value="">Π’ΠΈΠ±Π΅Ρ€ΠΈΡ‚Π΅ ΠΈΠ· списка</option> <?php if (!empty($regions)) :?> <?php foreach ($regions as $r) : ?> <option value="<?= $r['gs_regions_id']; ?>"><?= $r['gs_regions_name']; ?></option> <?php endforeach;?> <?php else: ?> <option value="">Бписок нСдоступний</option>'; <?php endif; ?> </select> 

    And if so:

     function region_get(){ $result = $this->mysqli->query("SELECT * FROM t_gs_regions ORDER BY gs_regions_name ASC"); $regions = false; if (mysqli_num_rows($result)>0) { $regions = array(); while($row6 = mysqli_fetch_assoc($result)) $regions[] = $row6; } return $regions; } 

    AND

     <select name="regions_id"> <option value="">Π’ΠΈΠ±Π΅Ρ€ΠΈΡ‚Π΅ ΠΈΠ· списка</option> <?php if ($regions) { foreach ($regions as $r) {?> { '<option value=<?=$r['gs_regions_id']?>><?=$r['gs_regions_name']?>'</option>'; } <?php } ?> } else { '<option value="">Бписок нСдоступний</option>'; } ?> </select>