나는 대학 프로젝트 및 튜터를위한 장바구니를 만들고 있는데 샘플 코드가 있지만 내 디자인에 맞게 조정하고 있습니다. 쇼핑 카트 PHP 디스플레이 오류
내 사이트 제품
이 같은cutecupcak.es/product.php?id=vanilla_cupcakes
오히려
cutecupcak.es/product.php?id=2
보다 더 장바구니에 제품을 표시하는 장바구니 페이지의 코드로 이름이 지정됩니다
if(is_numeric($id)) {
$cartresult = mysql_query($sql);
하지만, 내 제품 URL이 숫자가 아니므로 여기에서 볼 수 있듯이 장바구니에 결과가 표시되지 않습니다. - http://cutecupcak.es/shoppingcart.php - 테스트하는 경우.
당신이
전에서 변경하려면 어떻게해야합니까하지만 products.php?id=2
등이있는 경우가 작동합니다 - 내 URL 형식으로 작동하도록 is_numeric
?
전체 코드
<?php
session_start(); // start new session or call exisiting session
include('include/dbconnect.php'); // include the database connections
function renderCartForm()
{
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="shoppingcart.php?action=update" method="post" id="cart">';
$output[] = '<div class="form">';
$output[] = '<table border="2" class="formcontainer">';
$output[] = '<tr class="formlabels">';
$output[] = '<td width="400">Cake Name</td>';
$output[] = '<td width="75">Price</td>';
$output[] = '<td width="75">Quantity</td>';
$output[] = '<td width="100">Total</td>';
$output[] = '<td width="100">Remove?</td>';
$output[] = '</tr>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM `CAKE` WHERE `cake_url` = '.$id;
if(is_numeric($id)) {
$cartresult=mysql_query($sql);
while($cartrow = mysql_fetch_array($cartresult)) {
$output[] = '<tr>';
$output[] = '<td>'.$cartrow['cake_name'].'</td>';
$output[] = '<td>'.$cartrow['cake_price'].'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="2" maxlength="2" /></td>';
$output[] = '<td>£'.($cartrow['cake_price'] * $qty).'</td>';
$output[] = '<td><a href="shoppingcart.php?action=delete&id='.$id.'">Remove</a></td>';
$total += $cartrow['cake_price'] * $qty;
$output[] = '</tr>';
}
}
}
$output[] = '</table>';
$output[] = '</div>';
$output[] = '<p>Grand total: <strong>£'.$total.'</strong></p>';
$output[] = '<div class="\"formlabels\"><button type="submit">Update Cart</button></div>';
$output[] = '</form>';
} else {
$output[] = '<p>Your shopping cart is empty.</p>';
}
echo join('
',$output);
}
?>
제품 이름의 형식은 무엇입니까? 당신의 예에는 문자와 밑줄이 있지만 숫자는 가질 수 있습니까? 아포스트로피? 인용 부호? – andrewsi
모든 URL은 vanilla_cupcakes와 같은 숫자가 아니며 기본적으로 한 단어는 하나의 단어를 밑줄로 표시합니다. – FoamyMedia
이 경우 해당 텍스트가 해당 형식인지 확인하는 함수를 작성할 수 있습니다. – andrewsi