html tag within php variable

-3

Can I use html tags within php variable. For example I'm doing a loop where I have a parent item that I want to take <strong> and the child items I just add - , except that if I do this at the time of setting the parent item $varPai = "<strong>{$varValue}"</strong> variable, it does not render the item with strong, is it possible to do this within php?

This is my foreach that is riding, parent > children

foreach($listaLocalidade as $p_id => $p_nome){
$cidades = $this->Localidade->find('list',array('conditions'=>"Localidade.Localidade_id ={$p_id}", 'Localidade.programas is not null'));    
$opcoesPaisCidade[$p_id] = "<strong>{$p_nome}</strong>"; // aqui é o pai, onde eu teria que deixar strong

foreach($cidades as $c_id => $c_nome){
    $opcoesPaisCidade[$c_id] = ' - '.$c_nome;
    //die(print_r($opcoesPaisCidade));
}

}

And I'm riding like this:

<select id="combobox" name="data[orcamento][localidade_id]">
  <option value="">Select one...</option>
   <?php
   foreach($opcoesPaisCidade as $p_id => $p_nome){
   ?>
   <option value="<?=$p_id?>"><?=$p_nome?></option>
   <?}?>
</select>
    
asked by anonymous 25.11.2015 / 17:42

1 answer

0

Yes, you can use any html tag in a php variable, other than these tags, it's just that these values are printed to the browser for rendering.

$num = array(1,2,3,4,5);
$retorno = '';

foreach($num as $numero){
    if($numero % 2 == 1){
        $retorno .= "<strong>{$numero}</strong><br/>";
    } else {
        $retorno .= "{$numero}<br/>";
    }
}

print $retorno;

In this case, the html tag must be enclosed in the quotation mark, both the opening tag and the closing tag.

$var = "<tag>{$var_a}</tag>";

If it is with a list (select), you can concatenate a tag, based on a condition.

$num = array(1,2,3);
$retorno = '';
foreach($num as $numero){
    $retorno .= "<option";
    if($numero == 3){
        $retorno .= " selected>";
    } else {
        $retorno .= ">";
    }
    $retorno .= "{$numero}</option>";
}

print "<select>";
print $retorno;
print "</select>";

For example, you will select number 3 by default to leave this value bold, you can use a class and some css, or you can apply a css property online.

$num = array(1,2,3);
$retorno = '';
foreach($num as $numero){
    $retorno .= "<option";
    if($numero == 3){
        $retorno .= " class=\"bold\">";
    } else {
        $retorno .= ">";
    }
    $retorno .= "{$numero}</option>";
}

print "<select>";
print $retorno;
print "</select>";

This would be the css property:

<style type="text/css">
.bold { font-weight:bold; }
</style>

Or with the css property online:

...
if($numero == 3){
        $retorno .= " style=\"font-weight:bold;\">";
    } else {
        $retorno .= ">";
    }
...
  

NOTE: It is not possible to style part of a option , or even create new tags within one, because they are typically stylized according to the platform in use.

On recommendation, there are custom APIs for styling forms, if you find that what you are currently doing is not enough, use some APIs.

    
25.11.2015 / 18:14