Php 如何使另一个文件的变量出现在所需文件的heredocs中?
我试图让cart.php中的$count出现在另一个名为component.php的文件的heredocs中,这两个文件已经可以相互通信了 下面是cart.php:Php 如何使另一个文件的变量出现在所需文件的heredocs中?,php,Php,我试图让cart.php中的$count出现在另一个名为component.php的文件的heredocs中,这两个文件已经可以相互通信了 下面是cart.php: <?php session_start(); require_once("php/CreateDb.php"); require_once("php/component.php"); $db = new CreateDb("Productdb2", "Producttb2",); if (isset($_POST['remov
<?php
session_start();
require_once("php/CreateDb.php");
require_once("php/component.php");
$db = new CreateDb("Productdb2", "Producttb2",);
if (isset($_POST['remove'])) {
if ($_GET['action'] == 'remove') {
foreach ($_SESSION['cart'] as $key => $value) {
unset($_SESSION['cart']);
if ($value["product_id"] == $_GET['id']) {
/* echo "<script>alert('Product has been Removed...!')</script>"; */
echo "<script>window.location = 'cart.php'</script>";
}
}
}
}
?>
<!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>Carrinho</title>
<script src="https://kit.fontawesome.com/3da1a747b2.js"></script>
<link href="https://fonts.googleapis.com/css?family=Catamaran&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css">
<link rel="stylesheet" type="text/css" href="semantic/dist/semanticModificado.min.css">
<link rel="stylesheet" href="payment.css">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="cart.css">
</head>
<body>
<div class="container" id="navmobile" style="display: none">
<img src="upload\felinefilogo.svg" style="margin-top: 1rem" height="100px" width="250px" alt="">
<img src="upload\felinefi.svg" height="20px" width="62.5px" alt="">
<nav class="navbar" style="padding: 0; margin-right: 16px; font-size: 1.4rem">
<ul class="nav">
<li><a href="index.php">Início</a></li>
<li><a href="#about">Sobre</a></li>
<li><a href="contact.php">Contato</a></li>
<li><?php include("php/header.php") ?></li>
</ul>
</nav>
</div>
<nav class="navbar" id="navdesktop">
<div class="container">
<div class="imagem"><img src="upload\felinefihorizontalmedium.svg" height="100px" width="250px" alt="">
</div>
<ul class="nav" style="font-size: 1.4rem">
<li><a href="index.php">Início</a></li>
<li><a href="#about">Sobre</a></li>
<li><a href="contact.php">Contato</a></li>
<li><?php require("php/header.php") ?></li>
</ul>
</div>
</nav>
<section class="section-aPayment">
<div class="containerPayment">
<div class="row" style="flex-wrap:nowrap" !important>
<div class="col-75">
<br>
<h2 class="meuCarrinho">Meu carrinho</h2>
<br>
<?php
$total = 0;
if (isset($_SESSION['cart'])) {
$product_id = array_column($_SESSION['cart'], 'product_id');
$result = $db->getData();
while ($row = mysqli_fetch_assoc($result)) {
foreach ($product_id as $id) {
if ($row['id'] == $id) {
cartElement($row['product_image'], $row['product_name'], $row['product_price'], $row['id']);
$total = $total + (float) $row['product_price'];
}
}
}
} else {
echo "<h1>Seu carrinho está vazio!</h1>";
?>
<style type="text/css">
.subCoEsquerda4 {
display: none;
}
#hidethisDiv {
display: none;
}
</style>
<?php
}
?>
</div>
<div class="col-25">
<br>
<h2>Resumo da compra</h2>
<br>
<div class="containerResumo">
<div class="subCoEsquerda1">
<h3 class="esquerda">Itens</h3>
<h3 class="direita2">
<?php
if (isset($_SESSION['cart'])) {
$count = count($_SESSION['cart']);
echo "($count)";
} else {
echo "(0 items)";
}
?></h3>
</div>
<div>
<hr>
</div>
<div class="subCoDireita2">
<h3 class="esquerda">Desconto</h3>
<h3 class="direita2" id="hidethisDiv"> 50% </h3>
</div>
<div>
<hr>
</div>
<div class="subCoEsquerda3">
<h3 class="esquerda">Valor total</h3>
<h3 class="direita2"><?php echo "$ " . number_format($total, 2); ?></h3>
</div>
<hr>
<div class="subCoEsquerda4">
<a href="payment.php"><button class="positive fluid ui button">Continuar</button></a>
</div>
</div>
</div>
</div>
</div>
</section>
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.16/jquery.mask.min.js"></script>
<script src="semantic/dist/semantic.min.js"></script>
<script>
$("input[name='expyear']").mask("00 / 00");
$("input[name='zip']").mask("00000 - 000");
</script>
<script>
//a condição surte efeito quando a página da reload ou aperta f5.
$(document).ready(function() {
if ($(this).width() < 500) {
$('#navdesktop').hide();
$('#navmobile').show();
} else if ($(this).width() > 500) {
$('#navmobile').hide();
$('#navdesktop').show();
}
});
//a condição surte efeito apenas quando mexe na "responsividade" da página.
$(window).resize(function() {
if ($(this).width() < 500) {
$('#navdesktop').hide();
$('#navmobile').show();
} else if ($(this).width() > 500) {
$('#navmobile').hide();
$('#navdesktop').show();
}
});
</script>
<script>
//aumentar e diminuir a quantidade dos itens (respectivamente)
$(function() {
$('#mais').on('click', function() {
var $qty = $(this).closest('h1').find('.qty');
var currentVal = parseInt($qty.val());
if (!isNaN(currentVal)) {
$qty.val(currentVal + 1);
}
});
$('#menos').on('click', function() {
var $qty = $(this).closest('h1').find('.qty');
var currentVal = parseInt($qty.val());
if (!isNaN(currentVal) && currentVal > 0) {
$qty.val(currentVal - 1);
}
});
});
</script>
</body>
</html>
$count
只是会话变量的长度。你就不能只做$count=count($\u SESSION['cart'])代码>也在component.php
中?我已经读到这个代码块不能在heredocs EODY中。你不把它放在here文档中,你把它放在here文档之前。$count=count($\u SESSION['cart'])$元素=仅此而已?那么,我将如何关联该值呢?我在这里试过了,但没用。
<input class="qty" value="1" type="text" style="padding-right: 8px; padding-left: 12px; width: 34px;" maxlength="1" disabled="">
<?php
function cartElement($productimg, $productname, $productprice, $productid)
{
$element = <<< EOD
<form action="cart.php?action=remove&id=$productid" method="post">
<div class="cartContainer">
<div class="sub-container">
<div class="imagem2"><img src="$productimg" alt=""></div>
<div class="conteudodaDiv">
<div class="title-e-icon">
<h3>$productname</h3> <button class="icon" name="remove"><i class="fas fa-trash-alt"></i></button>
</div>
<p>Ref: <span># D12-9970-014-39</span></p>
<p>Vendido por <a href="index.php">Felinefi</a> e entregue em até 8 dias úteis. </p>
<p>Frete GRÁTIS</p>
<p>Tamanho: <span>39 </span></p>
<p>Cor: <span>Preto</span> </p>
</div>
</div>
<hr>
<div class="eita">
<h1>Quantidade:
<button type="button" class="ui circular button" id="menos"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="-5 -15 39 39">
<path d="M12 2c5.514 0 10 4.486 10 10s-4.486 10-10 10-10-4.486-10-10 4.486-10 10-10zm0-2c-6.627 0-12 5.373-12 12s5.373 12 12 12 12-5.373 12-12-5.373-12-12-12zm6 13h-12v-2h12v2z" /></svg></button>
<div class="ui mini icon input focus">
<input class="qty" value="1" type="text" style="padding-right: 8px; padding-left: 12px; width: 34px;" maxlength="1" disabled="">
</div>
<button type="button" class="ui circular button" id="mais"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="-5 -15 39 39">
<path d="M12 2c5.514 0 10 4.486 10 10s-4.486 10-10 10-10-4.486-10-10 4.486-10 10-10zm0-2c-6.627 0-12 5.373-12 12s5.373 12 12 12 12-5.373 12-12-5.373-12-12-12zm6 13h-5v5h-2v-5h-5v-2h5v-5h2v5h5v2z" /></svg></button>
</h1>
<div class="price">
<span>R$ 79.90</span><span>R$$productprice</span>
</div>
</div>
</div>
</form>
EOD;
echo $element;
};