写一个thinkphp5.1代码,功能是在10个商品中只能选2个商品,打5折
时间: 2023-06-03 14:04:42 浏览: 52
以下是您需要的 ThinkPHP5.1 代码,实现在10个商品中只能选2个商品,并将总价打5折:
```
// 获取选中的商品数量
$count = count($_POST['goods']);
// 判断选中的商品数量是否超过2个,如果超过则提示错误信息
if ($count > 2) {
echo '抱歉,最多只能选择2个商品!';
exit;
}
// 获取选中的商品信息,计算总价并打5折
$total = 0;
foreach ($_POST['goods'] as $good) {
$info = Db::name('goods')->where('id', $good)->find();
$total += $info['price'];
}
$total *= 0.5;
// 输出总价
echo '选中商品的总价为:' . $total . '元';
```
其中,假设您的商品信息存储在数据库中,商品编号为 `id`,价格为 `price`。在表单中以 `goods[]` 的方式提交选中的商品编号。
相关问题
用thinkphp5.1 写一个layui 分页功能
好的,下面是使用thinkphp5.1和layui实现分页的步骤:
1. 安装layui和thinkphp5.1
在项目根目录下使用composer安装layui和thinkphp5.1:
```
composer require topthink/think:5.1.* -vvv
composer require layui/layui
```
2. 创建控制器和视图文件
在控制器中定义一个方法用于获取数据和渲染视图:
```php
namespace app\index\controller;
use think\Controller;
use think\Db;
class Index extends Controller
{
public function index()
{
// 获取数据
$list = Db::name('user')->paginate(10);
// 渲染视图
$this->assign('list', $list);
return $this->fetch();
}
}
```
在视图文件中使用layui的分页组件:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>分页演示</title>
<link rel="stylesheet" href="/static/layui/css/layui.css">
</head>
<body>
<div class="layui-container">
<table class="layui-table">
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
{volist name="list" id="vo"}
<tr>
<td>{$vo.id}</td>
<td>{$vo.name}</td>
<td>{$vo.age}</td>
</tr>
{/volist}
</tbody>
</table>
<div id="page"></div>
</div>
<script src="/static/layui/layui.js"></script>
<script>
layui.use(['laypage', 'layer'], function(){
var laypage = layui.laypage;
var layer = layui.layer;
// 初始化分页组件
laypage.render({
elem: 'page',
count: {$list.total},
limit: {$list.list_rows},
curr: {$list.current_page},
jump: function(obj, first){
if(!first){
// 点击分页按钮时触发的回调函数
window.location.href = '?page=' + obj.curr;
}
}
});
});
</script>
</body>
</html>
```
3. 实现分页功能
在控制器中获取分页数据:
```php
namespace app\index\controller;
use think\Controller;
use think\Db;
class Index extends Controller
{
public function index()
{
// 获取当前页码
$page = $this->request->get('page', 1);
// 获取分页数据
$list = Db::name('user')->paginate(10, false, ['page' => $page]);
// 渲染视图
$this->assign('list', $list);
return $this->fetch();
}
}
```
在视图文件中根据当前页码显示对应的数据:
```html
<tbody>
{volist name="list" id="vo"}
<tr>
<td>{$vo.id}</td>
<td>{$vo.name}</td>
<td>{$vo.age}</td>
</tr>
{/volist}
</tbody>
```
然后在分页组件的回调函数中设置跳转链接:
```js
jump: function(obj, first){
if(!first){
// 点击分页按钮时触发的回调函数
window.location.href = '?page=' + obj.curr;
}
}
```
这样就可以实现使用thinkphp5.1和layui实现分页功能了。
写个thinkphp5.1的新人优惠券代码
我可以回答这个问题,以下是一个简单的thinkphp5.1的新人优惠券代码:
```php
// 定义一个新人优惠券的控制器
namespace app\index\controller;
class Coupon {
// 新人注册成功后,执行此方法发放优惠券
public function giveCoupon() {
// 获取新用户ID
$newUserId = request()->param('userId');
// 判断是否为已注册用户
$isRegister = Db::name('user')->where('id', $newUserId)->value('is_register');
if ($isRegister == 1) {
// 发放优惠券
$couponData = [
'user_id' => $newUserId,
'coupon_name' => '新人优惠券',
'coupon_value' => 10,
'start_time' => time(),
'end_time' => strtotime('+7 days'),
'create_time' => time()
];
Db::name('coupon')->insert($couponData);
return json(['code' => 200, 'msg' => '发放成功']);
} else {
return json(['code' => 500, 'msg' => '用户已经注册过了']);
}
}
}
```
以上代码可以在thinkphp5.1框架下实现新人注册成功后自动发放一张优惠券,优惠券面值为10元,有效期7天。