How to concatenate HTML within PHP

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

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

The code is HTML generated by JavaScript.

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

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

It worked with the example that Baccon posted, thanks to all soon I come back with more doubts.

    
asked by anonymous 07.01.2016 / 03:46

3 answers

7

In JS

You can use both concat and + :

 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 it has the predictability of being treated as a string. See it working:

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. You only have to be careful about 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 response, before the comments and update the question by the author:

In PHP

The PHP concatenation operator 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 this: j[i]['CodCidade'] or even this: j[i]->CodCidade ? Also, if it is PHP, the prefixes that indicated variable were missing: $j[$i] etc ...

    
07.01.2016 / 03:48
1

Here are different variants for concatenating:

$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 what you are looking for and what the @bacco replied .
The second is an interpolation, using escaped double quotation marks. The third is similar to the second but using single quotes in the HTML works on it . The fourth is a possible way if you use echo alone. It may not apply to your case, 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): link

    
07.01.2016 / 07:06
0

This looks like JavaScript being generated by JavaScript. So the thing is a bit different than it asks. To make PHP write this, you will have to choose which type of delimiter to use in PHP and then define the delimiter that will be escaped.

Example, retaining the original form:

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

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

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

Here we use the double quote as a delimiter, so all double quotes of the string must be escaped with backslash

Another way, using hered

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

The advantage is that you do not have to escape anything except in very specific cases. So just throw everything in the original form.

Be careful with closing. In the above example, HTML; can not contain spaces or tabs at the beginning.

This is much cleaner and handy for dealing with templates since it does not need to leak. It is very convenient to assign extensive codes where it would take a lot of work to do all the leaks.

Let's complicate this 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 not used for simple and common cases where we can solve with concatenation or heredoc. An example of using output buffering is compiling templates.

* terms used:

double quote -> aspa dupla
backslash -> barra invertida
    
07.01.2016 / 07:07