Katalog PHP wczytujący produkty z bazy danych i wyświetlający je dla użytkownika (eCommerce)

0

Jako ze nie moge linków wstawić, bo to mój pierwszy post, a w kodzie mam linki wewnetrzne, wrzucam caly zapytanie do pierwszego komentarza.

0

Cześć, mam problem z jednym plikiem PHP, który ma służyć do przeskanowania tabelki "product" gdzie są kolumny "product_id" "product_name" "product_description" "product_price". Potem mają być wyświetlone na ekranie i funkcje kup teraz lub zobacz opis, ale niestety nie dzieje się nic, nawet żaden błąd mi nie wyskakuje, po prostu jest czysta strona. Podejrzewam, że mogłem coś zepsuć przy robieniu pętli, ale niestety nie jestem w stanie znaleźc przyczyny.

<?php

// include('dbConfig.php');

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Products</title>
    <style>
    body{
        font-family:"verdana", sans-serif;
        color:white;
    }
    .buy{
        background-color:green;
        color:white;
    }
    .product_wrapper{
        margin:10px 1% 0 0;
        width:40%;
        padding:4%;
        height:inherit;
        background-color:grey;
        float:left;
        
    }
    .product_image{
        width:100px;
        height:75px;
    }
    </style>

</head>
<body>

<?php

    //display products
    
$servername = "localhost";
$username = "root";
$password = "";
$database = "eCommerce";
    try {
        $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password); //building a new connection object
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        //Selecting multiple rows from a MySQL database using the PDO::query function.
        
        $sql = $conn->prepare("SELECT * FROM product ORDER BY product_id DESC");
                
        $sql -> execute(); //execute the statement
        $product_array = $sql->fetchAll();
    
        if($sql->rowCount()) { //check if we have results by counting rows
            foreach($product_array as $row)
                {   
//
//
//
//
                    $current_product = $row['product_id'];
                    
                    try{
                        $sql2 = $conn->prepare('SELECT * FROM product_images WHERE product_id= :product_id');
                        $sql2 -> execute(['product_id' => $current_product]); //execute the statement
                        $row2 = $sql2->fetch();
                        $product_image = $row2['product_image_filename'];
                        echo "product image is". $product_image;
                        exit();  
                    }
                    catch(PDOException $e)
                    {
                        echo $sql2 . "<br>" . $e->getMessage(); //If we are not successful we will see an error
                    }
                    
                    //include a code snippet to show preview of each product
                    echo "<div class='product_wrapper'>
                        <img class='product_image' src='".$product_image."'>
                        <form method='post' action=''>
                        <input type='hidden' name='product_id' value=".$row['product_id']." />
                        
                        <div class='name'>".$row['product_name']."</div>
                        <div class='price'>£".number_format($row['product_price'], 2)."</div>
                        <div class='more_info'><a href=''>More info</a></div>
                        <button type='submit' class='buy'>Buy Now</button>
                        </form>
                        </div>";
                }   
                
            }
        else
            {
                echo 'no products to show'; //message to display if the search returned no results
            }    
        }
    catch(PDOException $e)
        {
        echo $sql . "<br>" . $e->getMessage(); //If we are not successful we will see an error
        }
    ?>

    <div style="clear:both;"></div>

    <div class="message_box" style="margin:10px 0px;">
    <?php echo $status; ?>
    </div>

</div>
</body>
</html>
0
  1. Gdzie masz inicjalizowanie dla $status?
<div class="message_box" style="margin:10px 0px;">
    <?php echo $status; ?>
</div>
  1. Weź zrób z tym porządek...
echo "<div class='product_wrapper'>
                        <img class='product_image' src='".$product_image."'>
                        <form method='post' action=''>
                        <input type='hidden' name='product_id' value=".$row['product_id']." />

                        <div class='name'>".$row['product_name']."</div>
                        <div class='price'>£".number_format($row['product_price'], 2)."</div>
                        <div class='more_info'><a href=''>More info</a></div>
                        <button type='submit' class='buy'>Buy Now</button>
                        </form>
                        </div>";

To już lepiej coś takiego:

<?php
[..]
?>
   <div class='product_wrapper'>
          <img class='product_image' src='<?= $product_image ?>'>
          <form method='post' action=''>
                        <input type='hidden' name='product_id' value='<?= $row['product_id'] ?>' />

                        <div class='name'><?= $row['product_name'] ?></div>
                        <div class='price'>£ <?= number_format($row['product_price'], 2) ?></div>
                        <div class='more_info'><a href=''>More info</a></div>
                        <button type='submit' class='buy'>Buy Now</button>
           </form>
    </div>
<?php
[..]
0

Dziękuję za odpowiedź, niestety w dalszym ciągu nie umiem doprowadzić tego do ładu, żeby jakikolwiek output był

//edit
udało mi się, dziękuję za pomoc

1 użytkowników online, w tym zalogowanych: 0, gości: 1