How to concatenate HTML within PHP

Asked

Viewed 6,054 times

3

<options += '<option value="' + j[i].CodCidade + '">' + j[i].NomeCidade + '</option>';

I have this code and want to add one more attribute after the Nomecidade, if anyone can help.

The code is an HTML generated by Javascript.

I have a select cidades and want to add in the tag option, after the NomeCidade, the ValorTarifa which is received in the same select of the city through the CodCidade.

The problem is: how to put the ValorTarifa in the same option?

It worked with the example that Baccon posted, thank you all soon I return with more doubts.

  • Dude, you completely changed the question. Better make a new one, otherwise you invalidate the answers below completely.

  • Thanks for the tip

3 answers

7


In JS

You can use both the concat as to the +:

 options.concat( '<option value="', j[i].CodCidade, '">',
                 j[i].NomeCidade, ' ', j[i].ValorTarifa, '</option>' );

The Concat can be interesting in this case, because you can use it with several parameters, and has the predictability to be treated as a string. See in operation:

var options = '';
var i;
var j = [
          { 'CodCidade':1, 'NomeCidade': 'Taubate' , 'ValorTarifa':'19,90' },
          { 'CodCidade':2, 'NomeCidade': 'Campinas', 'ValorTarifa':'25,00' },
          { 'CodCidade':3, 'NomeCidade': 'Queluz'  , 'ValorTarifa':'46,50' }
        ];

for ( i = 0; i < 3; i++ ) {
  options = options.concat( '<option value="', j[i].CodCidade,'">', j[i].NomeCidade, ' ', j[i].ValorTarifa, '</option>' );
}

document.body.innerHTML += '<select>' + options + '</select>';


Using += and + also works in this case. Just take care of interpretation problems when mixing numbers and strings.

var options = '';
var i;
var j = [
          { 'CodCidade':1, 'NomeCidade': 'Taubate' , 'ValorTarifa':'19,90' },
          { 'CodCidade':2, 'NomeCidade': 'Campinas', 'ValorTarifa':'25,00' },
          { 'CodCidade':3, 'NomeCidade': 'Queluz'  , 'ValorTarifa':'46,50' }
        ];

for ( i = 0; i < 3; i++ ) {
  options += '<option value="' + j[i].CodCidade + '">' + j[i].NomeCidade + ' ' + j[i].ValorTarifa + '</option>';
}

document.body.innerHTML += '<select>' + options + '</select>';


original answer, before comments and update of the question by the author:

In PHP

The concatenation operator of PHP is .

$options .= '<option value="'.$j[$i]['$CodCidade'].'">'.$j[$i]['NomeCidade'].'</option>';

Note that in your example, it was already being used here: j[i].CodCidade, probably by mistake.

Did you not mean it: j[i]['CodCidade']or even this: j[i]->CodCidade? Also, if it is PHP, were missing the prefixes that indicate variable: $j[$i] etc....

  • 4

    j[i].NomeCidade has the face of js :P

  • 1

    @rray yeah, I think the tag is wrong, or the environment exchange has generated confusion. I just hope it’s not PHP generating JS, then it’s too traumatic kkk.

  • Bacco and @rray: I added another answer. In Javascript I use a lot of arrays to make strings. So I avoid concatenating :)

  • I didn’t explain correctly, this code and a javascript generated html.

  • It worked, thank you very much to the others for the help. Now I will try to put the schedules of each route belonging to the city

1

Here are different variants to concatenate:

$codCidade = 20; // só para o exemplo, podias ter sando o $j[$i]['$CodCidade'] em baixo
$nomeCidade = 'Lisboa';
$options = '';


$options.= '<option value="'.$codCidade.'">'.$nomeCidade.'</option>';
$options.= "<option value=\"$codCidade\">$nomeCidade</option>";
$options.= "<option value='$codCidade'>$nomeCidade</option>";
echo $options;
// outras variantes :
echo '<option value=', $codCidade, '>', $nomeCidade, '</option>';
echo implode('', Array('<option value="', $codCidade, '">', $nomeCidade, '</option>'));

The first is deep down what you seek and what the @stock answered.
The second is an interpolation, using double escaped quotes.
The third is similar to the second but using simple quotes (plicas) in HTML that works the same.
The fourth is a possible way if you use isolated echo. It may not apply to you, but it is applicable in similar cases.
The last one is how I use it most often in Javascript. Creating arrays and then concatenating everything.

ideone (example): https://ideone.com/foKFTL

0

This looks like an HTML being generated by Javascript. So it’s a little different than what you ask for. To make PHP write this, you have to choose which type of delimiter to use in PHP and then set the delimiter that will be escaped.

Example, keeping the original shape:

<options += '<option value="' + j[i].CodCidade + '">' + j[i].NomeCidade + '</option>';

To put this into a PHP variable would look like this:

$foo = "<options += '<option value=\"' + j[i].CodCidade + '\">' + j[i].NomeCidade + '</option>';";

Here we use double quote as delimiter, so all double string tiles must be backslashed

Another way, using heredoc

$foo = <<<HTML
<options += '<option value="' + j[i].CodCidade + '">' + j[i].NomeCidade + '</option>';
HTML;

The advantage is that you don’t need to escape anything except in very specific cases. So just play everything in the original form.

Take care with the closing. In the example above, HTML; cannot contain spaces or tab at the beginning.

This is much cleaner and practical to handle templates because you don’t have to make escapes. It is very convenient to assign extensive codes where give much work to make all escapes.

Let’s complicate it with an example of output buffering

<?php
ob_start();
?>
<options += '<option value="' + j[i].CodCidade + '">' + j[i].NomeCidade + '</option>';
<?
echo ob_get_clean();

Output control is used for specific cases. Obviously it is not used for simple and ordinary cases where we can solve with concatenation or heredoc. An example of using output buffering is building templates.

*terms used:

double quote -> aspa dupla
backslash -> barra invertida
  • This is an html generated by javascript, I wanted to add another variable in front of the City that would receive a value coming from the same select only I do not know how to concatenate the two variables, and if this is possible with the <option tag>

Browser other questions tagged

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