I always have big problems with shielding, because my eyes just break when I try to check everything as an aspect:

while ($row_core = sqlsrv_fetch_array($result_core,SQLSRV_FETCH_NUMERIC)) { $gp++; echo '<input type="checkbox" name ="group-'<?php echo $gp; ?>" > <label for="<?php echo $gp; ?>"> $sql_s_core = "SELECT nameM FROM dbo.menu WHERE head_id=0"; $result_core = sqlsrv_query($conn,$sql); } 

This is a piece of the menu, namely check box, the point is that the label values ​​for it are pulled out of the database in a loop, the checkbox and label have id that should be new for each new box check, a new menu item. How do I properly shield it? In the loop I made a variable $gp which for each round of the loop becomes +1, then its value is displayed in the id of the element.

  • 3
    The approach itself is not very, to pull the base every time in a cycle, and if in a cycle there are 1000 values, 1000 queries? It is necessary to love a label. - Redr01d
  • What exactly needs to be screened? What variable output? - koks_rs
  • @koks_rs output of the gp variable in name = "group- $ gp" id = "group- $ dp" - qaz qaz
  • And why is there (inside echo ) a break in php tags then? XZibit transmits a fiery sign: I put a gap pkhp in the echo, so that you could echo while you echo! - SLy_huh
  • @SLy_huh, and try instead of trolling the next time to explain why this is not correct? - koks_rs

2 answers 2

You have confused the concepts. Shielding:

 echo "Экранирование символов делается \"Так\"."; // Выдаст : Экранирование символов делается "Так". 

You just need to insert the variable into the string:

 echo "<input type='checkbox' name='group-{$gp}'>"; // Выдаст : <input type='checkbox' name='group-1'> 

So it will also work, the result is the same.

  echo "<input type='checkbox' name='group-$gp'>"; 

Braces are needed to display more complex expressions, such as accessing an element of an array:

 $arr = array("value" => "foo"); echo "это {$arr['value']} !"; // Выдаст: это foo ! 
  • thanks a lot, it became clear now - qaz qaz
 echo '<input type="checkbox" name ="group-' . $gp . '">'; echo '<label for="' . $gp . '">'; 
  • one
    please give more detailed answers - cache