~~~
<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
use think\Db;
use think\Session;
class Parts extends AdminBase
{
protected function _initialize()
{
parent::_initialize();
$partsClass = model('partsClass')->getLevelList();
$this->assign('partsClass', $partsClass);
$this->assign('brand',model('brand')::All());
$factory=model("factory")->select();
$res=model('Parts')::All();
$this->assign('res',$res);
$this->assign('factory',$factory);
}
public function index($pid=0)
{
$res=model('Parts')->order('id','desc')->select();
$this->assign('res',$res);
return $this->fetch();
}
public function add()
{
return $this->fetch();
}
public function save($pid=0)
{
if ($this->request->isPost()) {
$data = $this->request->param();
// $data['photo']=json_encode($data['photo']);
$data['types'] = json_encode(array($data['name'], $data['value']));
$data['user_id'] = Session::get('admin_id');
if (model('Parts')->allowField(true)->save($data)) {
$userId = Db::name('parts')->getLastInsID();
//关联车型
if(empty($data['configure_id'])){
}else{
foreach ($data['configure_id'] as $k => $v) {
// $carsID[$k]=intval($v);
Db::table('yp_parts_configure')
->data(['parts_id' =>$userId, 'configure_id' => intval($v)])
->insert();
}
// halt($carsID);
}
//关联工厂
if(empty($data['factory'])){
}else {
//halt($data['factory']);
foreach ($data['factory'] as $k => $v) {
$factory[$k] = ['parts_id'=>$userId,'factory_id'=>intval($v['factory_id']),'onename' => $v['onename'], 'twoname' => $v['twoname'], 'threename' => $v['threename']];
Db::table('yp_parts_factory')
->data($factory[$k])
->insert();
}
}
$this->success('添加成功');
}
$this->success('添加失败');
}
}
//老新增备份
/*public function save44($pid=0)
{
if ($this->request->isPost()) {
$data=$this->request->param();
// $data['photo']=json_encode($data['photo']);
$data['types']=json_encode(array($data['name'],$data['value']));
$data['user_id']=Session::get('admin_id');
halt($data);
if(model('Parts')->allowField(true)->save($data)){
//关联车型
foreach ($data['configure_id'] as $k => $v) {
$carsID[$k]=intval($v);
}
model('Parts')->Configure()->attach($carsID);
//关联工厂
foreach ($data['factory'] as $k => $v) {
$factory[$k]=[intval($v['factory_id']),['onename'=>$v['onename'],'twoname'=>$v['twoname'],'threename'=>$v['threename']]];
$factoryID[$k]=intval($v['factory_id']);
}
model('Parts')->Factory()->attach($factoryID);
foreach ($factory as $k => $v) {
db('partsFactory')->where('parts_id',model('Parts')->id)->where('factory_id',$v[0])->update($v[1]);
}
$this->success('添加成功');
}
$this->success('添加失败');
}
}*/
public function view($id)
{
$this->assign('r',model('Parts')::get($id));
return $this->fetch();
}
public function edit($id)
{
$r=model('Parts')->where('id',$id)->find($id);
$rfactory=Db::table('yp_parts_factory')->where('parts_id',$id)->select();
$rconfigure=Db::table('yp_parts_configure')->where('parts_id',$id)->select();
$this->assign('rfactory',$rfactory);
$this->assign('rconfigure',$rconfigure);
$this->assign('r',$r);
return $this->fetch();
}
public function update($id)
{
if ($this->request->isPost()) {
$data=$this->request->param();
if(input('?.photo')){
$data['photo']=json_encode($data['photo']);
}
$data['types']=json_encode(array($data['name'],$data['value']));
if(model('Parts')->allowField(true)->save($data,['id'=>$data['id']])){
//关联车型
if(empty($data['configure_id'])){
Db::table('yp_parts_configure')->where('parts_id','=',$data['id'])->delete();
}else{
$cid='';
foreach ($data['configure_id'] as $k => $v) {
Db::table('yp_parts_configure')->where('parts_id','=',$data['id'])->delete();
$cid.=intval($v).',';
}
foreach (explode(",",substr($cid, 0, -1)) as $key => $value) {
$inserts[$key]['configure_id']=$value;
$inserts[$key]['parts_id']=$data['id'];
}
Db::name('parts_configure')->insertAll($inserts);
}
//关联工厂
if(empty($data['factory'])){
Db::table('yp_parts_factory')->where('parts_id','=',$data['id'])->delete();
}else{
Db::table('yp_parts_factory')->where('parts_id','=',$data['id'])->delete();
foreach ($data['factory'] as $k => $v) {
$factory[$k] = ['parts_id'=>$data['id'],'factory_id'=>intval($v['factory_id']),'onename' => $v['onename'], 'twoname' => $v['twoname'], 'threename' => $v['threename']];
Db::table('yp_parts_factory')
->data($factory[$k])
->insert();
}
}
$this->success('更新成功');
}
$this->success('更新失败');
}
}
//更新备份
/*public function update1($id)
{
if ($this->request->isPost()) {
$data=$this->request->param();
if(input('?.photo')){
$data['photo']=json_encode($data['photo']);
}
$data['types']=json_encode(array($data['name'],$data['value']));
if(model('Parts')->allowField(true)->save($data,['id'=>$data['id']])){
//关联车型
foreach ($data['configure_id'] as $k => $v) {
$carsID[$k]=intval($v);
}
model('Parts')->Configure()->detach();
model('Parts')->Configure()->attach($carsID);
//关联工厂
foreach ($data['factory'] as $k => $v) {
$factory[$k]=[intval($v['factory_id']),['onename'=>$v['onename'],'twoname'=>$v['twoname'],'threename'=>$v['threename']]];
$factoryID[$k]=intval($v['factory_id']);
}
model('Parts')->Factory()->detach();
model('Parts')->Factory()->attach($factoryID);
foreach ($factory as $k => $v) {
db('partsFactory')->where('parts_id',model('Parts')->id)->where('factory_id',$v[0])->update($v[1]);
}
$this->success('更新成功');
}
$this->success('更新失败');
}
}*/
public function del($id = 0, $ids = [])
{
$id = $ids ? $ids : $id;
if ($id) {
$r=model('Parts')->where('id',$id)->find($id);
if ($r->delete()) {
$this->success('删除成功');
} else {
$this->error('删除失败');
}
} else {
$this->error('请选择需要删除的条目');
}
}
}
~~~
- 心灵笔记
- tp5在编辑器一行一个添加信息
- 出库入库处理
- 出库
- 入库
- 后台控制器
- tp5加载更多-流加载
- 配件列表
- Parts.php
- add.html
- edit.html
- 图片管理
- controller
- 1.imgfile.php
- 2.imgfiletag.php
- view
- imgfile
- 1.index.html
- 2.show.html
- 3.simple.html
- 4.more.html
- 5.edit.html
- 6.deittag.html
- imgfiletag
- add.htm
- edit.htm
- model
- imgfile.php
- imgfiletag.php
- api
- upload.php
- js
- admin.js
- tp5整合百度编辑器多图上传
- 1.html代码
- 2.js代码
- 3.api代码
- 4.extend中的拓展代码
- tp5删除图片同时删除服务器图片
- 实用字段的的添加更新
- 根据时间戳获取未来几天星期几
- curl
- 城市按字母排版
- 聚合查询及渲染,处理产品首页
- 过滤字段
- 风控
- 查询id是否在数组里
- 判断更新还是新增操作
- tp5使用引入php文件