Double Eleven의 등장으로 우리 장바구니는 안심이 되었습니다. 이 기사는 PHP를 사용하여 장바구니에 간단한 추가 기능을 구현하는 방법에 대해 PHP 개발자에게 큰 도움이 됩니다. 장바구니를 작성하기 전에 먼저 데이터베이스에서 테이블을 호출해야 합니다. 여기서는 로그인 테이블을 사용하여 사용자 이름을 조정합니다. 모든 것이 준비되면 장바구니에 담는 것을 고려해야 하는 세 가지 상황이 있습니다.
첫 번째 상황: 장바구니에 아무것도 없습니다
두 번째 상황: 이미 장바구니에 이 제품이 있습니다. 이 케이스를 다시 추가할 때 수량을 +1로 고려하세요
세 번째 경우: 장바구니에 상품이 있는데 해당 상품이 없습니다
다음 사진은 사용된 데이터베이스 테이블입니다.


다음은 로그인 페이지의 코드입니다:
1 2 3 4 5 6 7 8 9 10 11 | <body>
<form action= "chuli.php" method= "post" >
<p style="margin-left: 500px; margin-top: 200px;
height: 250px; width: 250px; border: 1px dashed black">
<p style= "margin-left: 100px; " ><h3>登录</h3></p>
<p style= "margin-top: 20px" >用户名:<input type= "text" name= "uid" /></p><br/>
<p>密 码:<input type= "password" name= "pwd" /></p><br/>
<p style= "margin-left: 180px" ><input type= "submit" value= "登录" /></p>
</p>
</form>
</body>
|
로그인 후 복사
로그인 페이지가 작성된 후 처리 페이지에 들어가 데이터베이스에서 사용자 이름과 비밀번호를 불러와야 합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php
session_start();
header( "Content-type:text/html;charset=utf-8" );
$uid = $_POST [ "uid" ];
$pwd = $_POST [ "pwd" ];
include ( "DADB.class.php" );
$db = new DADB();
$sql = "select password from login where username='{$uid}'" ;
$arr = $db ->Query( $sql );
if ( $arr [0][0]== $pwd && ! empty ( $pwd ))
{
$_SESSION [ "uid" ]= $uid ;
header( "location:main.php" );
}
else
{
echo "登录失败" ;
}
|
로그인 후 복사
로그인 페이지는 다음과 같습니다. 그림에서:

다음 단계는 홈페이지에 들어가는 것입니다. 이제 데이터베이스에서 모든 과일 정보를 검색하고 장바구니에 추가하는 기능을 구현할 수 있습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <h2>大苹果购物网</h2>
<?php
session_start();
include ( "DADB.class.php" );
$db = new DADB();
?>
<table border= "1" width= "100%" cellpadding= "0" cellspacing= "0" >
<tr>
<td>代号</td>
<td>水果名称</td>
<td>水果价格</td>
<td>原产地</td>
<td>货架</td>
<td>库存量</td>
<td></td>
</tr>
<?php
$uid = $_SESSION [ "uid" ];
$sql = "select * from fruit" ;
$arr = $db ->Query( $sql );
foreach ( $arr as $v )
{
echo "<tr>
<td>{ $v [0]}</td>
<td>{ $v [1]}</td>
<td>{ $v [2]}</td>
<td>{ $v [3]}</td>
<td>{ $v [4]}</td>
<td>{ $v [5]}</td>
<td><a href='add.php?ids={ $v [0]}'>购买</a></td>
</tr>";
}
?>
|
로그인 후 복사
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?php
$ann = array ();
if (! empty ( $_SESSION [ "gwc" ]))
{
$ann = $_SESSION [ "gwc" ];
}
$zhonglei = count ( $ann );
$sum =0;
foreach ( $ann as $k )
{
$sql1 = "select price from fruit where ids='{$v[0]}'" ;
$danjia = $db ->Query( $sql1 );
foreach ( $danjia as $n )
{
$sum = $sum + $n [0]* $k [1];
}
}
echo "购物车有<mark>{$zhonglei}</mark>种商品,总价格为<mark>{$sum}</mark>元" ;
?>
</table>
<p>
<a href= "gouwuche.php" rel= "external nofollow" rel= "external nofollow" >查看购物车</a>
<a href= "main.php" rel= "external nofollow" rel= "external nofollow" >浏览商品</a>
<a href= "zhanghu.php" rel= "external nofollow" rel= "external nofollow" >查看账户</a> </p>
</body>
|
로그인 후 복사
메인 페이지는 다음과 같습니다. 사진에 표시됩니다:

가장 중요한 것은 장바구니 추가 페이지입니다
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <?php
session_start();
$ids = $_GET [ "ids" ];
if ( empty ( $_SESSION [ "gwc" ]))
{
$arr = array (
array ( $ids ,1)
);
$_SESSION [ "gwc" ]= $arr ;
}
else
{
$arr = $_SESSION [ "gwc" ];
$chuxian = false;
foreach ( $arr as $v )
{
if ( $v [0]== $ids )
{
$chuxian = true;
}
}
if ( $chuxian )
{
for ( $i =0; $i < count ( $arr ); $i ++)
{
if ( $arr [ $i ][0]== $ids )
{
$arr [ $i ][1]+=1;
}
}
$_SESSION [ "gwc" ] = $arr ;
}
else
{
$asg = array ( $ids ,1);
$arr [] = $asg ;
$_SESSION [ "gwc" ] = $arr ;
}
}
header( "location:gouwuche.php" );
|
로그인 후 복사
이렇게 하면 장바구니 페이지 코드를 다음과 같이 표시할 수 있습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <h2>购物车中有以下商品:</h2>
<table cellpadding= "0" cellspacing= "0" border= "1" width= "100%" >
<tr>
<td>商品名称</td>
<td>商品单价</td>
<td>购买数量</td>
<td></td>
</tr>
<?php
session_start();
$arr = array ();
if (! empty ( $_SESSION [ "gwc" ]))
{
$arr = $_SESSION [ "gwc" ];
}
include ( "DADB.class.php" );
$db = new DADB();
foreach ( $arr as $v )
{
global $db ;
$sql = "select * from fruit where ids='{$v[0]}'" ;
$att = $db -> Query( $sql ,1);
foreach ( $att as $n )
{
echo "<tr>
<td>{ $n [1]}</td>
<td>{ $n [2]}</td>
<td>{ $v [1]}</td>
<td><a href='shanchu.php?ids={ $v [0]}'>删除</a></td>
</tr>";}
}
?>
</table>
<p>
<a href= "gouwuche.php" rel= "external nofollow" rel= "external nofollow" >查看购物车</a>
<a href= "main.php" rel= "external nofollow" rel= "external nofollow" >浏览商品</a>
<a href= "zhanghu.php" rel= "external nofollow" rel= "external nofollow" >查看账户</a> </p> 14 15
</body>
|
로그인 후 복사
쇼핑을 입력하세요. 장바구니 페이지를 열고 그림과 같이 표시하세요.

위는 장바구니 케이스에 추가하는 간단한 PHP 구현일 뿐이며 데이터베이스의 제품 수를 줄이는 등 나중에 기능을 개선할 예정입니다. 장바구니에 담은 상품을 장바구니에 담은 후 삭제하는 방법입니다. 이 섹션의 내용이 모든 사람에게 도움이 되고 모든 사람에게 장바구니 개발에 대한 보다 명확한 아이디어를 제공할 수 있기를 바랍니다.
관련 추천:
php 장바구니 구현 코드 예시 요약
php 장바구니 php 장바구니 구현 코드
php 장바구니 코드_PHP 튜토리얼
위 내용은 장바구니에 추가 기능 사례를 PHP로 간단하게 구현의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!