Add a for cycle within an echo with php

2

Greetings, I'm doing a for loop in an echo and it gives me a syntax error. This is the code

<?php $resultado = '<html><h2>Productos:</h2><p>'.for ($i=0; $i<count($_POST["productos"]); $i++){ echo '<b>'.$_POST["productos"][$i].'</b>'; }.'</p></html>'; ?> it is worth mentioning that it is an array the products that I am receiving

    
asked by Avancini1 24.11.2017 в 00:09
source

3 answers

2

You can combine PHP / HTML code, but to do so, you must open and close the PHP code labels every time you use HTML and vice versa. As for the for , it is not a string to concatenate it, as I see you are doing:

$resultado = '<html><h2>Productos:</h2><p>'.for

                                           ^ el punto es para concatenar cadenas,
                                             for no es una cadena

In these cases, I particularly prefer to initialize a variable and to concatenate the content using .= and in the end I do echo of that variable.

This makes the code more organized and easier to analyze.

For example in your case, something like this:

<?php 

    /*Inicializar variable sobre la cual se irá concatenando*/
    $resultado = '<html><h2>Productos:</h2><p>';

    for ($i=0; $i<count($_POST["productos"]); $i++){
        $resultado.='<b>'.$_POST["productos"][$i].'</b>';
    }

    $resultado.='</p></html>';
    /*Imprimimos la variable*/
    echo $resultado;
?>

It may be a good idea to improve the code a little bit further, because if there are several products, they will all come together.

If you want a separation of at least one line between each product, you can do something like this:

<?php 

    /*Inicializar variable sobre la cual se irá concatenando*/
    $resultado = '<h2>Productos:</h2>';

    for ($i=0; $i<count($_POST["productos"]); $i++){
        $resultado.='<b>'.$_POST["productos"][$i].'</b><br />';
    }

    /*Imprimimos la variable*/
    echo $resultado;
?>

Note that I have removed the <html> tag, which should be used only if that code snippet is all there is on your page and you have not already created that tag in any other way. Otherwise, putting the label again would duplicate it.

answered by 24.11.2017 / 00:25
source
1

If you want to print the array you can use implode:

<?php
   // suponiendo estos valores para el array
   $_POST["productos"] = array('hola','mundo','con','implode');
   $resultado = '<html><h2>Productos:</h2><p><b>'.implode("<b></b>", $_POST["productos"])'</b></p></html>';
?>

the exit would be:

<html>
    <h2>Productos:</h2>
    <p>
       <b>hola</b>
       <b>mundo</b>
       <b>con</b>
       <b>implode</b>
    </p>
</html>
    
answered by 24.11.2017 в 00:41
1

Good evening. Remove the variable's loop.

<?php 
$datos = [uno, dos, tres, cuatro]; // sustituye a $_POST["productos"]
$resultado = '<h2>Productos:</h2><p>';
for ($i=0; $i<count($datos); $i++){
     $resultado .= '<b>'.$datos[$i].'</b> '; 
    } 
$resultado .= '</p>';
echo $resultado;
?>
    
answered by 24.11.2017 в 00:54