Convert a PHP "Object" to a SESSION? - php

I have a {$obj->product_stock} which contains a numeric value - the stock of the product.
How can I convert that {$obj->product_stock} to a SESSION? Here is what I tried...
$_SESSION['product_stock'] = {$obj->product_stock};
Why does it error and how can I get round this?
<?php
session_start();
include_once("config.php");
// Current URL of the Page. cart_update.php redirects back to this URL
$current_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="keywords" content="">
<title>Product Page</title>
<?php include("includes/header-includes.php"); ?>
</head>
<body>
<?php include("includes/navigation.php"); ?>
<?php include("includes/basket-warning.php"); ?>
<!-- Products List Start -->
<?php
$ProductID = intval($_REQUEST['ProductID']);
$results = $mysqli->query("SELECT * FROM products where ProductID = '$ProductID';");
if($results) {
$_SESSION['product_stock'] = $obj->product_stock;
echo "STOCK: ". $_SESSION['product_stock'] ."";
$products_item = '<ul class="products">';
// Fetch results, set as object and output HTML
while($obj = $results->fetch_object()) {
$products_item .= <<< EOT
<li class="product">
<form method="post" action="cart_update.php">
<div class="product-content">
<h3>{$obj->ProductName} <strong>(STOCK: {$obj->product_stock}</strong>)</h3>
<div class="product-image" style="background: url({$obj->TypeImage}) no-repeat; background-size: 100%; min-height: 551.328px;">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<div class="carousel-inner" role="listbox">
<div class="item active"><img src={$obj->TypeImage} class="img-responsive"></div>
<div class="item"><img src={$obj->AlternativeImagesOne} class="img-responsive"></div>
<div class="item"><img src={$obj->AlternativeImagesTwo} class="img-responsive"></div>
<div class="item"><img src={$obj->AlternativeImagesThree} class="img-responsive"></div>
</div>
</div>
</div>
<div class="product-content-cont">
<h4>{$obj->ProductName} - Overview</h4>
<p>{$obj->ProductDescription}</p>
</div>
</div>
<div class="like-what-you-see">
<fieldset>
<label>
<span>Price</span>
{$currency}{$obj->Price}
</label>
<label>
<input type="" size="1" maxlength="2" name="product_qty" value="1" class="form-control" />
</label>
</fieldset>
<input type="hidden" name="ProductCode" value="{$obj->ProductCode}" />
<input type="hidden" name="type" value="add" />
<input type="hidden" name="return_url" value="{$current_url}" />
<button type="submit" class="btn btn-warning">Add</button>
</div>
</form>
</li>
EOT;
}
$products_item .= '</ul>';
echo $products_item;
}
?>
<!-- Products List End -->
<?php include("includes/footer.php"); ?>
</body>
</html>

Your code:
$_SESSION['product_stock'] = $obj->product_stock;
echo "STOCK: ". $_SESSION['product_stock'] ."";
is outside the while loop. Therefore $obj is not yet defined.
Move the code above into the while loop like this
$products_item = '<ul class="products">';
// Fetch results, set as object and output HTML
while($obj = $results->fetch_object()) {
$_SESSION['product_stock'] = $obj->product_stock;
echo "STOCK: ". $_SESSION['product_stock'] ."";
....

Related

keeping items in cart when click add item to cart

I'm trying to create a shopping cart based on an array but I can't seem to add more then one item into my cart. can someone help me??
above you see the array so it does continue to put things into my array but it only shows one item what am I doing wrong? have been trying to fix this for 2 months now
// code for the shopping cart
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errrors', '1');
// session_destroy();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="" />
<meta name="author" content="" />
<title>Cart</title>
<!-- Favicon-->
<link rel="icon" type="image/x-icon" href="assets/favicon.ico" />
<!-- Bootstrap icons-->
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons#1.5.0/font/bootstrap-icons.css" rel="stylesheet" />
<!-- Core theme CSS (includes Bootstrap)-->
<link href="css/styles.css" rel="stylesheet" />
<link href="css/stylecart.css" rel="stylesheet" />
<script src="js/scripts.js" async></script>
</head>
<style>
body {
font-size: 20px;
}
</style>
<body>
<a class="back" href="index.php"> <i class="bi bi-arrow-left-circle-fill bi-5x"></i></a>
<?php
include "config.php";
print_r($_SESSION['basket']);
array_push($_SESSION['basket'], $_GET['broodjes_ID']);
// oke het is neit geweldig maar het laat iets s
?>
<div class="text-center" style="font-size: 100px;">🛍</div>
<h2 class="text-center">Winkelmandje</h2><br>
<section class="container content-section">
<!-- <h2 class="section-header">CART</h2> -->
<div class="cart-row">
<span class="cart-item cart-header cart-column">ITEM</span>
<span class="cart-price cart-header cart-column">PRICE</span>
<!-- <span class="cart-quantity cart-header cart-column">QUANTITY</span> -->
</div>
<?php
$broodjes_ID = $_GET['broodjes_ID'];
//onthouden van vorige get request
//$count = count($_SESSION['basket']); // count basket puts 3 times the same item in the shopping cart
if($_SESSION['basket'] > 0){
for($i = 0; $i<$_SESSION['basket']; $i++){
//$basket = $_SESSION['basket'][$i];
$sql = "SELECT broodjes_ID, broodnaam, prijs FROM broodjes WHERE broodjes_ID = ?";
// uitvoeren, resultaat tonen in tabel.
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $broodjes_ID);
$stmt->execute();
$result = $stmt->get_result(); // get the mysqli result
//while loop zorgt er volgens mij voor dat hij blijft optellen
if($row = $result-> fetch_assoc()){
$sum = 0;
echo '<div class="cart-items">';
echo '<div class="cart-row">';
echo '<div class="cart-item cart-column">';
echo '<span class="cart-item-title">'. $row['broodnaam'] . '</span>';
echo '</div>';
echo '<span class="cart-price cart-column"> €'. $row['prijs'] . '</span>';
echo '</div>';
echo '</div>';
$sum = $row['prijs'];
break;
}
}
?>
<div class="cart-total">
<strong class="cart-total-title">Total</strong>
<span class="cart-total-price"> € <?php echo $sum;?></span>
</div>
<?php
}else{
echo 'mandje is leeg.';
?>
<div class="cart-total">
<strong class="cart-total-title">Total</strong>
<span class="cart-total-price"> € 0</span>
</div>
<?php
}
?>
<div class="text-center">
<button class="btn btn-outline-primary" type="button">PURCHASE</button>
</div>
</section>
</body>
this is the code for the store page
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errrors', '1');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="" />
<meta name="author" content="" />
<title>Shop Homepage - Start Bootstrap Template</title>
<!-- Favicon-->
<link rel="icon" type="image/x-icon" href="assets/favicon.ico" />
<!-- Bootstrap icons-->
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons#1.5.0/font/bootstrap-icons.css" rel="stylesheet" />
<!-- Core theme CSS (includes Bootstrap)-->
<link href="css/styles.css" rel="stylesheet" />
<link href="css/stylecart.css" rel="stylesheet" />
<script src="js/scripts.js" async></script>
</head>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 20px;
}
#myBtn {
display: none;
position: fixed;
bottom: 20px;
right: 30px;
z-index: 99;
font-size: 18px;
border: none;
outline: none;
background-color: black;
color: white;
cursor: pointer;
padding: 25px;
border-radius: 50px;
}
#myBtn:hover {
background-color: #555;
}
</style>
<body>
<!-- Navigation-->
<?php include "navbar.php"?>
<?php if(isset($_SESSION['USER_ID'])){ ?>
<!-- Header-->
<header class="bg-dark py-5">
<div class="container px-4 px-lg-5 my-5">
<div class="text-center text-white"> <!--Het werkt -->
<h1 class="display-4 fw-bolder">Goedemorgen, <?php echo $_SESSION['USERNAME'];?></h1>
<p class="lead fw-normal text-white-50 mb-0">Shop in style</p>
</div>
</div>
</header>
<?php
} else {
?>
<!-- Header-->
<header class="bg-dark py-5">
<div class="container px-4 px-lg-5 my-5">
<div class="text-center text-white"> <!--het werkt-->
<h1 class="display-4 fw-bolder">Goedemorgen, Gast</h1>
<p class="lead fw-normal text-white-50 mb-0">Shop in style</p>
</div>
</div>
</header>
<?php } ?>
<!--miscchien dit toch in een whileloop gooien dat zal ook de code korter maken-->
<!-- Section-->
<button onclick="topFunction()" id="myBtn" title="Go to top">↑</button>
<?php
include "config.php";
$sql = 'SELECT * FROM broodjes';
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->get_result(); // get the mysqli result
while($row = $result-> fetch_assoc()){
// $_SESSION['basket'] = array();
//echo $row['broodnaam'];
echo ' <section class="py-5">
<div class="container px-4 px-lg-5 mt-5">
<div class="row gx-4 gx-lg-5 row-cols-2 row-cols-md-3 row-cols-xl-4 justify-content-center">
<div class="col mb-5">
<div class="card h-100">
<!-- Product image dit kan later nog wel-->
<div class="fw-image">
<img class="card-img-top" src="https://dummyimage.com/450x300/dee2e6/6c757d.jpg" alt="..." />
</div>
<!-- Product details-->
<div class="card-body p-4">
<div class="text-center">
<div class="test1">
<input type="hidden" name="broodjes_ID" value=', $row['broodjes_ID'] , '/>
</div>
<!-- Product name-->
<div class="test3">
<input type="hidden" name="broodnaam" value=', $row['broodnaam'] , '/>
<h5 class="fw-bolder">'.$row['broodnaam']. '</h5>
</div>
<!-- Product price-->
<div class="test2">
<input type="hidden" name="broodnaam" value=', $row['prijs'] , '/>
<h3> Prijs: </h3> <h3 class="fw-price"> €'.$row['prijs']. '</h3><br>
</div>
<!--voorraad-->
'; if($row['voorraad'] == 0){
echo '
<h3 class="fw-voorraad" style="color: red;"> uitverkocht</h3><br>
'; }else{
echo '
Voorraad: '.$row['voorraad'].'<br>
</div>
</div>
<!-- Product actions-->
<form action="cart.php?broodjes_ID='. $row['broodjes_ID'].'" method="post" name="broodjes" value"<?php echo htmlentities(serialize($_SESSION["basket"])); ?>
<div class="card-footer p-4 pt-0 border-top-0 bg-transparent">
<div class="text-center"><button class="btn btn-outline-dark mt-auto shop-item-button" type="submit" name="add_to_cart">Add to cart</button></div>
</div>
</form>
'; }
echo '</div>
</div>
</div>
</div>
</section>';
if (isset($_POST['add_to_cart'])) {
$_SESSION['basket'] = array();
//$_SESSION['basket'][] = $row['broodjes_ID'];
$_SESSION['basket'] = array_push($_SESSION['basket'], $row['broodjes_ID']);
}
}
?>
<!-- Footer-->
<?php include "footer.php";?>
<!-- Bootstrap core JS-->
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<!-- Core theme JS-->
<script src="js/scrolltotop.js"></script>
</body>
</html>
it seems like you are using wrong conditions for your loop
instead of $i<$_SESSION['basket'] use $i<count($_SESSION['basket'])
same problem for you if condition
count() gives the count of number of element in the array
Here is the fixed code
<?php
$broodjes_ID = $_GET['broodjes_ID'];
//onthouden van vorige get request
//$count = count($_SESSION['basket']); // count basket puts 3 times the same item in the shopping cart
//use count
if(count($_SESSION['basket']) > 0){
//use count
for($i = 0; $I<count($_SESSION['basket']); $i++){
//$basket = $_SESSION['basket'][$i];
$sql = "SELECT broodjes_ID, broodnaam, prijs FROM broodjes WHERE broodjes_ID = ?";
// uitvoeren, resultaat tonen in tabel.
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $broodjes_ID);
$stmt->execute();
$result = $stmt->get_result(); // get the mysqli result
//while loop zorgt er volgens mij voor dat hij blijft optellen
if($row = $result-> fetch_assoc()){
$sum = 0;
echo '<div class="cart-items">';
echo '<div class="cart-row">';
echo '<div class="cart-item cart-column">';
echo '<span class="cart-item-title">'. $row['broodnaam'] . '</span>';
echo '</div>';
echo '<span class="cart-price cart-column"> €'. $row['prijs'] . '</span>';
echo '</div>';
echo '</div>';
$sum = $row['prijs'];
break;
}
}
?>

array_push while loop shopping cart item into basket without javascript

hello I want to add a cart item into a cart by using a button. For this I want to array_push this into my array. And then show that array into my cart. this is not what is happening right now:
right now it shows the last item from the while loop in the shopping cart and thats not what I want.
I want that if the user pressed the add_to_cart button then array_push this broodjes_ID, broodnaam, and prijs into the cart. what am I doing wrong?
index.php <- the shop
<!-- session_start(); session has already started -->
<?php
error_reporting(E_ALL);
ini_set('display_errrors', '1');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="" />
<meta name="author" content="" />
<title>Shop Homepage - Start Bootstrap Template</title>
<!-- Favicon-->
<link rel="icon" type="image/x-icon" href="assets/favicon.ico" />
<!-- Bootstrap icons-->
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons#1.5.0/font/bootstrap-icons.css" rel="stylesheet" />
<!-- Core theme CSS (includes Bootstrap)-->
<link href="css/styles.css" rel="stylesheet" />
<link href="css/stylecart.css" rel="stylesheet" />
<script src="js/scripts.js" async></script>
</head>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 20px;
}
#myBtn {
display: none;
position: fixed;
bottom: 20px;
right: 30px;
z-index: 99;
font-size: 18px;
border: none;
outline: none;
background-color: black;
color: white;
cursor: pointer;
padding: 25px;
border-radius: 50px;
}
#myBtn:hover {
background-color: #555;
}
</style>
<body>
<!-- Navigation-->
<?php include "navbar.php"?>
<?php if(isset($_SESSION['USER_ID'])){ ?>
<!-- Header-->
<header class="bg-dark py-5">
<div class="container px-4 px-lg-5 my-5">
<div class="text-center text-white"> <!--Het werkt -->
<h1 class="display-4 fw-bolder">Goedemorgen, <?php echo $_SESSION['USERNAME'];?></h1>
<p class="lead fw-normal text-white-50 mb-0">Shop in style</p>
</div>
</div>
</header>
<?php
} else {
?>
<!-- Header-->
<header class="bg-dark py-5">
<div class="container px-4 px-lg-5 my-5">
<div class="text-center text-white"> <!--het werkt-->
<h1 class="display-4 fw-bolder">Goedemorgen, Gast</h1>
<p class="lead fw-normal text-white-50 mb-0">Shop in style</p>
</div>
</div>
</header>
<?php } ?>
<!--miscchien dit toch in een whileloop gooien dat zal ook de code korter maken-->
<!-- Section-->
<button onclick="topFunction()" id="myBtn" title="Go to top">↑</button>
<?php
include "config.php";
$sql = 'SELECT * FROM broodjes';
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->get_result(); // get the mysqli result
while($row = $result-> fetch_assoc()){
$_SESSION['basket'] = array();
//echo $row['broodnaam'];
echo ' <section class="py-5">
<div class="container px-4 px-lg-5 mt-5">
<div class="row gx-4 gx-lg-5 row-cols-2 row-cols-md-3 row-cols-xl-4 justify-content-center">
<div class="col mb-5">
<div class="card h-100">
<!-- Product image dit kan later nog wel-->
<div class="fw-image">
<img class="card-img-top" src="https://dummyimage.com/450x300/dee2e6/6c757d.jpg" alt="..." />
</div>
<!-- Product details-->
<div class="card-body p-4">
<div class="text-center">
<div class="test1">
<input type="hidden" name="broodjes_ID" value=', $row['broodjes_ID'] , '/>
</div>
<!-- Product name-->
<div class="test3">
<input type="hidden" name="broodnaam" value=', $row['broodnaam'] , '/>
<h5 class="fw-bolder">'.$row['broodnaam']. '</h5>
</div>
<!-- Product price-->
<div class="test2">
<input type="hidden" name="broodnaam" value=', $row['prijs'] , '/>
<h3> Prijs: </h3> <h3 class="fw-price"> €'.$row['prijs']. '</h3><br>
</div>
<!--voorraad-->
'; if($row['voorraad'] == 0){
echo '
<h3 class="fw-voorraad" style="color: red;"> uitverkocht</h3><br>
'; }else{
echo '
Voorraad: '.$row['voorraad'].'<br>
</div>
</div>
<!-- Product actions-->
<form action="cart.php?broodjes_ID=', $row['broodjes_ID'] , '" method="post">
<div class="card-footer p-4 pt-0 border-top-0 bg-transparent">
<div class="text-center"><button class="btn btn-outline-dark mt-auto shop-item-button" type="submit" name="add_to_cart">Add to cart</button></div>
</div>
</form>
'; }
echo '</div>
</div>
</div>
</div>
</section>';
}
?>
<!-- Footer-->
<?php include "footer.php";?>
<!-- Bootstrap core JS-->
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<!-- Core theme JS-->
<script src="js/scrolltotop.js"></script>
</body>
</html>
cart.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="" />
<meta name="author" content="" />
<title>Cart</title>
<!-- Favicon-->
<link rel="icon" type="image/x-icon" href="assets/favicon.ico" />
<!-- Bootstrap icons-->
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons#1.5.0/font/bootstrap-icons.css" rel="stylesheet" />
<!-- Core theme CSS (includes Bootstrap)-->
<link href="css/styles.css" rel="stylesheet" />
<link href="css/stylecart.css" rel="stylesheet" />
<script src="js/scripts.js" async></script>
</head>
<?php
include "config.php";
$sql = 'SELECT * FROM broodjes';
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->get_result(); // get the mysqli result
while($row = $result-> fetch_assoc()){
$_SESSION['basket'] = array();
if(isset($_POST['add_to_cart'])){
array_push($_SESSION['basket'], $row['broodjes_ID'], $row['broodnaam'], $row['prijs']);
}
print_r($_SESSION['basket']);
}
?>
<div class="text-center" style="font-size: 100px;">🛍</div>
<h2 class="text-center">Winkelmandje</h2><br>
<section class="container content-section">
<!-- <h2 class="section-header">CART</h2> -->
<div class="cart-row">
<span class="cart-item cart-header cart-column">ITEM</span>
<span class="cart-price cart-header cart-column">PRICE</span>
<span class="cart-quantity cart-header cart-column">QUANTITY</span>
</div>
<?php
// query maken waarbij wij naam en prijs ophalen van de ids.
$count = count($_SESSION['basket']);
if($count > 0){
$sum = 0;
for($i = 0; $i<$count; $i++){
$basket = $_SESSION['basket'][$i];
$sql = "SELECT broodjes_ID, broodnaam, prijs FROM broodjes WHERE broodjes_ID = ?";
// uitvoeren, resultaat tonen in tabel.
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $basket);
$stmt->execute();
$result = $stmt->get_result(); // get the mysqli result
while($row = $result-> fetch_assoc()){
echo '<div class="cart-items">';
echo '<div class="cart-row">';
echo '<div class="cart-item cart-column">';
echo '<span class="cart-item-title">'. $row['broodnaam'] . '</span>';
echo '</div>';
echo '<span class="cart-price cart-column"> €'. $row['prijs'] . '</span>';
echo '</div>';
echo '</div>';
$sum += $row['prijs'];
}
}
?>
<div class="cart-total">
<strong class="cart-total-title">Total</strong>
<span class="cart-total-price"> € <?php echo $sum;?></span>
</div>
<?php
}else{
echo 'mandje is leeg.';
?>
<div class="cart-total">
<strong class="cart-total-title">Total</strong>
<span class="cart-total-price"> € 0</span>
</div>
<?php
}
?>
<div class="text-center">
<button class="btn btn-primary btn-purchase" style="margin: 5%;" type="button">PURCHASE</button>
</div>
</section>

How to create a edit profile page for users with PHP?

I've created a page but I can´t put the Save button working and regardless of the user I enter with, it appears the same user information for them all.
I think it's a loop problem but I'm not sure what it is.
<?php
require_once('LoginConfig.php');
session_start();
if(isset($_SESSION["email"])){
$user_check = $_SESSION["email"];
$result = $connect->prepare("SELECT * FROM users WHERE user_email = :usercheck");
$result->execute(array(":usercheck"=>$user_check));
$row = $result->fetch(PDO::FETCH_ASSOC);
}else
{
header("location:UsersLogin.php");
}
?>
<!DOCTYPE html>
<html lang="PT-pt">
<head>
<title> Editar Perfil - JamJam </title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:700, 600,500,400,300' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<link rel="stylesheet" href="http://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
<link rel="stylesheet" type="text/css" href="assets/css/style-menu.css">
<link rel="stylesheet" type="text/css" href="assets/css/style-articlelist2.css">
<script src="https://kit.fontawesome.com/8a368d3752.js"></script>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/data.js"></script>
<script src="assets/js/main.js"></script>
<style>
</style>
</head>
<body>
<div class="header">
<div class="logo">
<i style='font-size:24px' class='fas'></i>
<span style='font-size:24px'>JamJam</span>
</div>
<span></span>
</div>
<div class="side-nav">
<div class="logo">
<i style='font-size:24px; color:rgb(0,255,0)' class='fas'></i>
<span style='font-size:24px'>JamJam</span>
</div>
<nav>
<ul>
<li>
<a href="UsersMenu.php">
<span><i style='color:rgb(0,255,0)' class="fas fa-home"></i></span>
<span>Página Inicial</span>
</a>
</li>
<li class="active">
<a href="UsersProfile.php">
<span><i style='color:rgb(0,255,0)' class="fa fa-user"></i></span>
<span>Perfil</span>
</a>
</li>
<li>
<a href="UsersArticleList.php">
<span><i style='color:rgb(0,255,0)' style='font-size:16px' class='fas'>&#xf001</i></span>
<span>Músicas</span>
</a>
</li>
<li>
<a href="Logout.php">
<span><i style='color:rgb(0,255,0)' class="fas fa-sign-out-alt"></i></span>
<span>Logout</span>
</a>
</li>
</ul>
</nav>
</div>
<div class="main-content">
<div class="title">
Editar Perfil
</div>
<div class="main">
<div class="widget" style="lex-basis: 98%; height: 750px;">
<?php
if(isset($_SESSION["email"])){
$result = $connect->prepare("SELECT user_id, user_name, user_bio, user_local, user_image FROM users");
$result->bindParam(':userid', $id);
$result->execute();
$row = $result->fetch()
?>
<form style="text-align: left; margin: 15px; " action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input $id="user_id" name="user_id" type="hidden" value= "<?php echo $row['user_id']; ?>">
<div >
<label for="username">Nome:</label><br>
<input type="text" name="username" value="<?php echo $row['user_name']; ?>">
</div>
<div>
<label for="bio">Bio:</label>
<br>
<textarea type="text" class="textarea" name="bio" rows ="4"><?php echo $row['user_bio']; ?></textarea>
</div>
<div>
<label for="local">Sítio onde vive:</label><br>
<input type="text" name="local" value="<?php echo $row['user_local']; ?>">
</div>
<div>
<label for="image">Foto de perfil atual:</label><br>
<img src="<?php echo 'images/' . $row['user_image']; ?>">
</div>
<div>
<br> <form action="upload.php" method="post" enctype="multipart/form-data">
<lable for="image">Selecionar nova foto de perfil:</lable><br>
<input type="file" name="fileToUpload" id="fileToUpload">
</form>
</div>
<div style="text-align: left; margin: 10px;" class="box">
<button style="float: left;" class="saveButton" type="submit">Guardar</button>
<a style="float: right;" class="cancelButton" href="UsersProfile.php">Cancelar</a>
</div>
</form>
<?php
}
?>
</div>
</div>
</body>
</html>

php while loop not printing contents of an array horizontally in bootstrap

I am using a while loop in PHP to extract data for some items and print each query like a separate card in the same row as in a shopping cart. Bootstrap seems to print them vertically in separate rows. Expected layout
code
<!DOCTYPE html> <html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Shopping Cart</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="cart.css"> </head> <body>
<div class="container">
<?php
$connect = mysqli_connect('localhost','root','','cart');
$query = 'SELECT * FROM products ORDER BY id ASC';
$result = mysqli_query($connect, $query);
if (mysqli_num_rows($result)>0) {
while($product = mysqli_fetch_assoc($result)){
?>
<div class="col-sm-4 col-md-3">
<form method="post" class="" action="index.html">
<div class="products ">
<img src="<?php echo $product['image']; ?>" class="img-fluid" />
<h4 class="text-info"><?php echo $product['name']; ?></h4>
<h4>INR <?php echo $product['price']; ?></h4>
<input type="text" name="quantity" class="form-control" value="1" />
<input type="submit" name="add_to_cart" class="btn btn-info" value="Add" />
</div>
</form>
</div>
<?php
}
}
?>
</div>
</body>
</html>
I have tried using class="row-fluid" before the start of the loop or even the .product but it doesn't help.
Actual output in normal screen or developer tool
To allow for the products to be shown in 'rows' (horizontally), you need to add a <div class="row"> to your HTML code. I've banged together a small demo, that shows the basic idea (and the use of HEREDOC which allows for (IMHO) cleaner separation of HTML and PHP code).
Function renderProduct() is invoked through a foreach() loop on a dummy product range stored in array $products to imitate your database while loop.
The gist:
<?php
function renderProduct($product = 'n/a') {
$htmlProduct = <<<HEREDOC
<div class="col-sm-4 col-md-3">
<form method="post" class="" action="index.html">
<div class="products ">
<img src="$product" class="img-fluid" />
<h4 class="text-info">$product</h4>
<h4>INR $product</h4>
<input type="text" name="quantity" class="form-control" value="1" />
<input type="submit" name="add_to_cart" class="btn btn-info" value="Add" />
</div>
</form>
</div>
HEREDOC;
return $htmlProduct;
}
$htmlStart = <<<HEREDOC
<!DOCTYPE html> <html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Shopping Cart</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="cart.css">
</head>
<body>
<div class="container">
<div class="row"> <!-- ADDED -->
HEREDOC;
$htmlEnd = <<<HEREDOC
</div> <!-- close row -->
</div> <!-- close container -->
</body>
</html>
HEREDOC;
// render page
echo $htmlStart;
$products = ['product1', 'product2', 'product3', 'product4', 'product5', 'product6', 'product7'];
foreach($products as $prod) {
echo renderProduct($prod);
}
echo $htmlEnd;

HTTP Error 500 PHP

I am getting an HTTP Error 500 on my website. The PHP code checker doesn't see anything suspicious.
I think it's caused by PDO but I'm not sure because when I remove all the PHP code it still gives me the same error.
Here's my code:
Index.php:
<?php
include_once('includes/db_connect.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable="no>
<!-- Custom CSS -->
<link rel="stylesheet" href="css/custom.css">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css" />
<!-- Optional theme (flatly) -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootswatch/latest/flatly/bootstrap.min.css" />
<!-- Font awesome -->
<link href="//maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css" rel="stylesheet" />
<link rel="stylesheet" href="css/contact.css" rel="stylesheet">
<!-- jQuery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="js/contact.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="//netdna.bootstrapcdn.com/bootstrap/latest/js/bootstrap.min.js"></script>
<title><?php
foreach($dbh->query('SELECT * FROM page_info') as $row) {
echo $row['title'];
}
?></title>
</head>
<body>
<div class="banner">
<div class="name"><?php
foreach ($dbh->query('SELECT * FROM page_info') as $row) {
echo $row[“name”];
}
?></div>
</div>
<div class="about">
<div class="about-container">
<div class="about-main-text">
<h1>About me:</h1>
<?php
foreach ($dbh->query('SELECT * FROM about') as $row) {
echo '
<span>'.$row["text"].'</span>
';
}
?>
</div>
</div>
</div>
<div class="projects">
<div class="projects-container">
<div class="projects-main-text">
<h1>My projects:</h1>
</div>
<div class="row">
<?php
foreach($dbh->query('SELECT * FROM projects') as $row) {
echo '
<div class="col-md-4 nopadding">
<div class="project-box">
<img src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'" alt="Project Image">
<h1>'.$row["name"].'</h1>
<p>'.$row["desc"].'</p>
</div>
</div>
';
}
?>
</div>
</div>
</div>
<div class="contact">
<div class="contact-container">
<div class="contact-main-text">
<h1>Contact me:</h1>
</div>
<div class="row">
<form action="contact/send.php" method="post" style="float: left;">
<div class="field name-box">
<input type="text" id="name" name="name" placeholder="Who Are You?"/>
<label for="name">Name</label>
<span class="ss-icon">check</span>
</div>
<div class="field email-box">
<input type="text" id="email" name="email" placeholder="name#email.com"/>
<label for="email">Email</label>
<span class="ss-icon">check</span>
</div>
<div class="field msg-box">
<textarea id="msg" rows="4" placeholder="Your message goes here..."/></textarea>
<label for="msg">Msg</label>
<span class="ss-icon">check</span>
</div>
<input class="button" type="submit" value="Send" />
</form>
</div>
</div>
</div>
<div class="footer">
<span class="copyright">
© <?php
foreach ($dbh->query('SELECT * FROM page_info') as $row) {
echo $row['copyright'];
}
echo ' ' . date("Y");
?> - All rights reserved
</span>
</div>
</body>
</html>
And here is db_connect.php:
<?php
$uname = ‘rik_root’;
$upass = ‘*********’;
$dbh = new PDO('mysql:host=localhost;dbname=nijdeken’, $uname, $upass);
?>
I hope someone can help me. Thanks in advance!
Don't use a word processor to edit your code:
$uname = ‘rik_root’;
^--------^
those are not valid php quote characters, and are probably killing your code with a fatal parse error:
$dbh = new PDO('mysql:host=localhost;dbname=nijdeken’, $uname, $upass);
^--start of string ^--NOT an end-of-string

Categories