html tag inside php variable

Asked

Viewed 954 times

-3

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

this is my foreach that’s riding, father > sons

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>
  • 1

    Yes, it is possible. Just put it in quotes, treating it as a string. In the case of the tag strong, it wouldn’t be better to use css to style no?

  • then in case, the way I did is right?

  • Only now have I understood what you really intended, styling part of an option is out of the question. You just can’t.

1 answer

0

Yes, it is possible to use any html tag in a php variable, since these tags, it is enough that these values are printed for the browser, so that they are rendered.

$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 within the quotes, both the opening and closing tag.

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

If it is 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>";

This one, for example, will select the number 3 by default to bold that value, you can use a class and some css, or you can apply an inline css property.

$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 online css property:

...
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 usually stylized according to the platform in use.

In recommendation, there are own Apis to style forms, if you think that what you are currently doing is not enough, use some API.

  • I’m doing it this way, but the options of my select remain the same, will it be something of the option that does not allow?

  • @Michelhenriq read the points I added.

  • edited my question and put as I am riding my foreach

  • Which output command are you using ? (print, echo, print_r, ...) ?

  • I added in the question again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.