Problem in the Foreach array

Asked

Viewed 96 times

1

My problem is this, I hope you can help me:

I am searching some records from the "Products" table of my database and displaying it on the front. I am using a Foreach to bring these records and there is also a button that will create a session and add these products to my cart. The problem is not in the cart itself but in the list where is located the button that will add. It calls an Action referencing whether there is an action (the action, right) and the product id. The path looks something like this:

---> localhost/Loginandregisterlanchenet/carrinho.php? add=cart&id=

The id is empty and does not refer to anything resulting in a URL error not found. Below is the code that below:

<?php
$conect = new db();
$conect = $conect ->conectar();



$query = mysqli_query($conect, "SELECT * FROM produto");
$result = mysqli_fetch_all($query);


echo "<div  class='quadcard1'>CRIAR RESERVA<br><br>";
echo "<div class='quadcard2' > Cardápio<br><br>";
foreach ($result as $item){
    echo "<div class='quadcard'>";
    echo "<div class='produtos'>".$item[1]."<a href='carrinho.php?add=carrinho&id='".$item[0]."'><input class='btn btn-primary' type='button' value='+' style='display: relative; float : right; margin-right :1rem; margin-top: 0.5rem;'></a><br>".$item[3]." <label class='valor'> Valor: R$ ".$item [5]."</label></div></div>";
}

echo "<div></div>";
echo "</div></div>";
?> 

Someone was able to identify the mistake?

In case you need more details, just say that I add.

Grateful! Att.

  • 2

    Before foreach I recommend you check what’s inside your result. Try adding this before foreach: print_r($result)

  • It should not give URL error not found because of id, since it is only a URL parameter. The page itself is carrinho.php.

  • 1

    I think that $item[0] is not the value you are waiting for. As suggested above, a print_r($result) before the foreach, and even print_r($item) within the cycle can help you understand how the array is structured.

1 answer

0


I simulated how the array might look and below there is an explanatory code. Run var_dump to see the output. It looks like what you’re looking for. It is necessary to validate that the ID is not empty in order not to give error in its URL. You have to validate so that a product is not passed without id.

<?php

$result[] = [
    0 => 7, //id
    1 => 'mouse', //produto
    2 => 14, // quantidade
    3 => 12.00 //valor Unitario
];

$result[] = [
    0 => NULL, //id
    1 => 'pendrive', //produto
    2 => 140, // quantidade
    3 => 35.00 //valor Unitario
];

$result[] = [
    0 => '', //id
    1 => '', //produto
    2 => '', // quantidade
    3 => '' //valor Unitario
];

$result[] = [
    0 => NULL, //id
    1 => NULL, //produto
    2 => NULL, // quantidade
    3 => NULL //valor Unitario
];

//var_dump($result); 

foreach ($result as $item) {
    $id = $item[0];
    $produto = $item[1];

    if ($id != '' || !empty($id)) {
        echo "\n Existe " . "ID: " . $id;
        //continue; // Ou seja pula estes nulos
    } else {
        echo "\n Nulos ou vazios (Sem id)";
    }
}

Simulating the above result array to list in view according to its listed code would be: (I just swapped some variables and did the empty validation first to then list the data)

<?php
// Abre conexão
$conect = new db();
$conect = $conect ->conectar();

// Executa a query
$query = mysqli_query($conect, "SELECT * FROM produto");
// Retorna um array com os dados
$result = mysqli_fetch_all($query);

echo "<div  class='quadcard1'>iFood<br><br>";
echo "<div class='quadcard2' > Cardápio<br><br>";

// Percorre os dados
foreach ($result as $item) {
    $id = $item[0];
    $produto = $item[1];
    $qtdeEstoque = $item[2];
    $valor = $item[3];

    if ($id == '' || empty($id)) {
        // Se tiver um id vazio ele pula
        continue;
    } else {
        // Se tiver id ele mostra o produto
        echo "<div class='quadcard'>";
        echo "<div class='produtos'>" . $produto . "<a href='carrinho.php?add=carrinho&id='" . $id . "'><input class='btn btn-primary' type='button' value='+' style='display: relative; float : right; margin-right :1rem; margin-top: 0.5rem;'></a><br>" . $qtdeEstoque . " <label class='valor'> Valor: R$ " . $valor . "</label></div></div>";        
    }
}

echo "<div></div>";
echo "</div></div>";

Browser other questions tagged

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