Print value out of foreach

0

I have generated a list that is generated in the following way:

foreach ($productos as $p) { 
    echo '<tr>'; 
    echo '<td>' . $p->codigoProducto . '</td>'; 
    echo '<td>' . $p->categoriaNombre . '</td>'; 
    echo '<td>' . $p->marcaNombre . '</td>'; 
    echo '<td>' . $p->detalleProducto . '</td>'; 
    echo '<td>' . $p->precioProducto . '</td>'; 
    echo '</tr>'; 
}

As a category it is repeated, I would like to print it once only so that it remains

Categoria
Código | Marca | Detalle | Precio
1
2
3
...

But I can not call the value of the field outside the foreach .

    
asked by Maru 06.04.2017 в 01:41
source

5 answers

1

@Maru, elaborating my previous answer:

function cmp($a, $b) {
    return strcmp($a->categoriaNombre, $b->categoriaNombre);
}
usort($productos, "cmp");

$categoria = false;

foreach ($productos as $p) { 
    if ($p->categoriaNombre != $categoria) {
        echo '<tr>'; 
        echo '<td colspan="4">' . $p->categoriaNombre . '</td>'; 
        echo '</tr>';
        $categoria = $p->categoriaNombre; 
    }

    echo '<tr>'; 
    echo '<td>' . $p->codigoProducto . '</td>'; 
    echo '<td>' . $p->marcaNombre . '</td>'; 
    echo '<td>' . $p->detalleProducto . '</td>'; 
    echo '<td>' . $p->precioProducto . '</td>'; 
    echo '</tr>'; 
}

I think this is exactly what you need.

You may also be interested in adding htmlentities() to all fields.

    
answered by 07.04.2017 в 01:31
0

If you want to group by category by entering the name of the category and then the products in that category, do the following:

  • Sort the $productos by category
  • create a variable $categoria equal to false
  • in the loop, check if $p->categoriaNombre == $categoria
  • if it's not the same, write the category and assign $categoria = $p->categoriaNombre;
  • write all the product data as you do now, except the category

Forgive me for not writing more code but I'm with my mobile phone.

    
answered by 06.04.2017 в 02:11
0

I would recommend you use a array to save your variable category in case of repeating or repeating a value more than once:

foreach ($productos as $p) { 
    $categoria = $p->categoriaNombre;
    $vars_región = array("categoria");
    echo '<tr>'; 
    echo '<td>' . $p->codigoProducto . '</td>'; 
    echo '<td>' . $p->marcaNombre . '</td>'; 
    echo '<td>' . $p->detalleProducto . '</td>'; 
    echo '<td>' . $p->precioProducto . '</td>'; 
    echo '</tr>'; 
} 

And at the end of foreach you print it.

    
answered by 06.04.2017 в 02:09
0

You should try one foreach inside another, one to iterate the categories and another to iterate the products within it, here is a basic example:

$categorias = cargar_categorias()

foreach( $categorias as $cat){
   echo $cat->nombre;
   $productos = cargar_productos($cat->id);

   foreach( $productos as $prod ){
      echo $prod->nombre;
      echo $prod->precio;
   }
}

Greetings.

    
answered by 05.05.2017 в 17:53
0

If category is repeated you can save it a variable so that you use it outside of foreach I leave the example:

foreach ($productos as $p) { 
    $categoria = $p->categoriaNombre;
    echo '<tr>'; 
    echo '<td>' . $p->codigoProducto . '</td>'; 
    echo '<td>' . $p->marcaNombre . '</td>'; 
    echo '<td>' . $p->detalleProducto . '</td>'; 
    echo '<td>' . $p->precioProducto . '</td>'; 
    echo '</tr>'; 
}

echo $categoria;

Note: if the category value changes you will place the last value that was saved.

    
answered by 06.04.2017 в 01:48