Actually, how to run such scripts in separate files to markup separately, php code separately?

<select class="selectpicker selectpicker_patient form-control" name="patient" title="Выберите пациента" data-live-search="true" data-size="7"> <?php include($_SERVER['DOCUMENT_ROOT'].'/php/lib/connect.php'); $query = "SELECT * FROM Patient ORDER BY Lastname ASC"; if ($result = mysqli_query($link,$query)) { while ($row = mysqli_fetch_assoc($result)) { echo '<option value= '.$row['ID_Patient'].'> '.$row['Lastname'].' '.$row['Firstname'].' '.$row['Fathername'].' </option>'; } } ?> </select> 
  • Separate, and access the file through the request. - Zicrael

2 answers 2

Why so hard to do? Create your drop-down list with php in a separate file, and then output it in the right place in html . Here is an example.

file select.php

 $res = query("SELECT id, name FROM `names`"); if(!$res) exit("Ошибка запроса: ".mysqli_error()); if(mysqli_num_rows($res)>0){ $select = '<select name="idname" class="form-control input-sm" id="nameselect">'; while($row = mysqli_fetch_assoc($res)){ $select.= "<option value=".$row['id'].">".$row['name']."</option>"; } $select.="</select>"; } 

main.php file

 <?php include(__DIR__.'/../template/select.php'); echo $select; ?> 
  • your $select variable is not defined in all cases, in the case where mysqli_num_rows($res) zero. And it seems like select should be on the page in any case, judging by the author's code. - Kosta B.
  • Well, the concept can be defined. (and when zero, well, yes, not copied) If the author wants html separately and php separately, then he needs three files. 1 - html code, 2 - code with scripts, 3 - file that includes the previous two. - Nikolay Gabaraev

Something like this:

mian.php

  <select class="selectpicker selectpicker_patient form-control" name="patient" title="Выберите пациента" data-live-search="true" data-size="7"> <?php include 'file-with-query.php'; ?> </select> 

file-with-query.php

 <?php include($_SERVER['DOCUMENT_ROOT'].'/php/lib/connect.php'); $option = ""; $query = "SELECT * FROM Patient ORDER BY Lastname ASC"; if ($result = mysqli_query($link,$query)) { while ($row = mysqli_fetch_assoc($result)) { $option .= '<option value= '.$row['ID_Patient'].'> '.$row['Lastname'].' '.$row['Firstname'].' '.$row['Fathername'].' </option>'; } } echo $option; ?>