I am reworking a school project and in php code I need to insert html markup with php code inside. Confused with quotes. Here is what you need to insert into php code:

$txt = "<form> <table> <tr> <td style='text-align: center'><strong>Инвентарь</strong></td> <td style='text-align: center'><strong>Метод</strong></td> <td style='text-align: center'><strong>Номер род. кат.</strong></td> <td style='text-align: center'><strong>Уровень</strong></td> </tr> <tr> <td> <?php echo '<select name="inv_name">'; $query = mysqli_query($link,'select * from inventory'); if(mysqli_num_rows($query)) while($inv = mysqli_fetch_array($query)) echo '<option value="'.$inv["id_inventory"].'">'.$inv["name"].'</option>'; echo '</select>'; ?> </td> <td><input name='method_name' type='text' id='method_name'></td> <td><input name='method_pid' type='text' id='method_pid'></td> <td><input name='method_level' type='text' id='method_level'></td> </tr> <tr> <td colspan='4' style='text-align: center'><input type='submit' value='Добавить запись'></td> </tr> </table> </form>"; 
  • 2
    Never interfere with HTML and PHP ! Share the logic and presentation finally, tea dinosaurs are already extinct. - user207618
  • one
    Or at least first create content with a list and paste the variable into HTML . And use the HEREDOC syntax - this will HEREDOC problems with quotes. - user207618 4:16 pm

3 answers 3

In order not to be confused with quotes, it is necessary to separate the logic and representation.

  1. You can use a good template engine (Twig for example).
  2. You can build your code so that data acquisition and output are not in the same place.
  3. In your example, the easiest and fastest way to use formatting functions.

http://php.net/manual/en/function.sprintf.php

 <?php // Создаём выподающий список $dropDown = '<select name="inv_name">'; $query = mysqli_query($link,'select * from inventory'); if(mysqli_num_rows($query)) { while($inv = mysqli_fetch_array($query)) { $dropDown .= sprintf( '<option value="%s">%s</option>', $inv["id_inventory"], $inv["id_inventory"] ); } } $dropDown .= '</select>'; // Вставляем его в форму. $txt = sprintf("<form> <table> <tr> <td style='text-align: center'><strong>Инвентарь</strong></td> <td style='text-align: center'><strong>Метод</strong></td> <td style='text-align: center'><strong>Номер род. кат.</strong</td> <td style='text-align: center'><strong>Уровень</strong></td> </tr> <tr> <td>%s</td> <td><input name='method_name' type='text' id='method_name'></td> <td><input name='method_pid' type='text' id='method_pid'></td> <td><input name='method_level' type='text' id='method_level'></td> </tr> <tr> <td colspan='4' style='text-align: center'><input type='submit' value='Добавить запись'></td> </tr> </table> </form>", $dropDown ); 
  • Instead of a combo box, it simply shows 0 - unit
  • What is the value of $ dropDown? - E_p
  • @unit Javascript did not switch :) corrected example changed "+" to ".". - E_p
  • Understood! Thank! - unit
  • And dependent fields with the list to do by the same principle? - unit

'se lect name = "'. 'inv_name'. '">'; . $ inv ["'.' name '.'"]

In theory, so (the points are surrounded by single)

    It is necessary to escape internal double quotes, thus:

     \" 
    • Swears at this line: echo '<option value = \ "'. $ Inv [\" id_inventory \ "]. '\">'. $ Inv [\ "name \"]. '</ Option>'; Not expected string after: [Expected: identifier, variable, NUM_STRING, define - unit
    • where php variables are made inside single quotes, and the double quotes left in html need to be escaped - MasterAlex
    • In such a complex example, it will take more time for a war with quotes than rewriting with formatting functions. - E_p
    • @E_p, yes, I don’t mind that it’s crooked to insert php via html :) but anything can be useful - MasterAlex