Warning: Illegal string offset "Error bringing the results of a query"

0

Good afternoon, I have the following menu:

I would like to click on each category to show only the products of that category, but not to show me the products shows me those errors, the code is the following for the logic:

    <?php

include 'conexion.php';

//Traer las categorias desde la base de datos

$consulta = $conexion->prepare("SELECT * FROM tcategoria");

$consulta->execute();

$categorias = $consulta->fetchAll();

if(!$conexion){
    die();
}

$idCat=isset($_GET['idCategoria']) ? (int) ($_GET['idCategoria']) : false;

if(!$idCat){
    header('Location: index.php');
}

$statement=$conexion->prepare('SELECT * FROM tproducto WHERE idCategoria = :idCat');
$statement->execute(array(':idCat' => $idCat));

$categoria=$statement->fetch();

if(!$categoria){
    header('Location: index.php');
}

include 'views/listacategorias.view.php';

?>

And the next one for the view:

<div class="catprod">
    <nav class="categorias col-3 col-m-4">
    <ul>
        <?php foreach($categorias as $categoria): ?>
        <li><a href="listacategorias.php?idCategoria=<?php echo $categoria['idCategoria']; ?>"><?php echo $categoria['Categoria']; ?></a></li>
        <?php endforeach; ?>
    </ul>
</nav> 
<article class="imagenes col-9 col-m-8">
    <?php foreach($categoria as $cat):?>
    <div class="imagen1 col-4 col-m-6"> 
        <h2><?php echo $cat['Producto']; ?></h2>
        <a href="fotos.php?idProducto=<?php echo $foto['idProducto']; ?>">
            <img src="albumProductos/<?php echo $cat['Imagen'] ?>" alt="<?php echo $cat['Descripcion'] ?>">
        </a>
        <p><b>$: </b><?php echo $cat['Precio']; ?></p>
        <span class="icon-shopping-cart"></span><input type="submit" value="Comprar">
        </div>
    <?php endforeach;?> 
</article>
<div class="paginacion imagenes">
<?php if($pagina_actual > 1): ?>
    <a href="index.php?p=<?php echo $pagina_actual - 1; ?>" class="izquierda col-6 col-m-6"><span class="icon-arrow-left-alt1"></span> Página Anterior</a>
<?php endif ?>
<?php if($total_paginas != $pagina_actual): ?>
    <a href="index.php?p=<?php echo $pagina_actual + 1; ?>" class="derecha col-6 col-m-6">Página Siguiente <span class="icon-arrow-right-alt1"></span></a>
<?php endif ?>  
    </div>
</div>

I hope you can help me, thank you.

    
asked by Guillermo Ricardo Spindola Bri 18.04.2017 в 20:07
source

1 answer

2

This error is normally produced when you try to access a string as if it were an array.

For example:

<?php

$miArray = array('buenRollo'=>10, 'malRolo'=>0, 'diversion'=>10);
echo $miArray['diversion']; // muestra 10

$texto = "Esta es mi cadena de texto";
echo $texto['buenRollo']; // esto causa error: illegal string offset error...

?>

In your example $categoria=$statement->fetch(); returns a single row, so in the view you can dispense with the foreach to traverse it, but if you want to return all categories or rows and not just one you must use the fetchAll() method thus remaining $categoria=$statement->fetchAll();

Manual fetch ()

Manual fetchAll ()

    
answered by 18.04.2017 в 21:23