gpt4 book ai didi

php - CAKE-PHP如何在MYSQL表中保存动态数组

转载 作者:行者123 更新时间:2023-11-29 23:04:46 26 4
gpt4 key购买 nike

我正在创建一个动态数组并将其保存到表中,这是我的保存模型

型号:我有类别,类别有很多产品,然后产品有很多子产品,子产品可能有很多选项

这是我创建动态复选框的查看代码

<tr>
<td style="background-color:#f3f5f6;">
<?php // debug($Product);exit; ?>

<?php

//foreach ($Product as $Products): ?>
<?php echo $this->Form->checkbox('Artdetail.products.', array('value'=>$Product['Product']['id'],'hiddenField'=>false)); ?>
<?php echo $Product['Product']['product_name'].'<br/>' ?>

<?php

foreach ($Product['SubProduct'] as $subproducts):?>

<?php echo $this->Form->checkbox('Artdetail.products.'.$Product['Product']['id'].'.subproducts.', array('value'=>$subproducts['id'],'hiddenField'=>false)); ?>
<?php echo $subproducts['subproduct_name'].'<br/>' ?>




<?php

foreach ($subproducts['Option'] as $options): ?>


<?php echo $this->Form->checkbox('Artdetail.products.'.$Product['Product']['id'].'.subproducts.'.$subproducts['id'].'.options.', array('value'=>$options['id'],'hiddenField'=>false)); ?>
<?php echo $options['optname'].'<br/>' ?>

<?php

endforeach;?>

<?php

endforeach; ?>

<?php endforeach;?>
</td>
</tr>

这是我的 Controller 代码

if ($this->request->is('post')) {
$this->Artproject->create();


$this->request->data['Artproject']['ainum'] = 'AI-' . $this->request->data['Artproject']['ainum'];
$this->request->data['Artproject']['createdby'] = $this->Auth->user('id');

if ($this->Artproject->save($this->request->data['Artproject'])) {
$artid = $this->Artproject->getLastInsertID();

foreach ($this->request->data['Artdetail']['products'][$prdid]['subproducts'] as $subproducts):

//debug($subproducts);

$this->request->data['Artdetaill']['category_id'] = $sportid;
$this->request->data['Artdetaill']['product_id'] = $prdid;
$this->request->data['Artdetaill']['subproduct_id'] = $subproducts;
$this->request->data['Artdetaill']['user_id'] = $this->request->data['Artproject']['user_id'];
$this->request->data['Artdetaill']['art_id'] = $artid;

if(!empty($subproducts['options'])){

foreach ($subproducts['options'] as $options):

$this->request->data['Artdetaill']['option_id'] = $options;
$this->Artdetail->saveAll($this->request->data['Artdetaill']);
endforeach;

}else{

$this->Artdetail->saveAll($this->request->data['Artdetaill']);
}


endforeach;
exit;
}

}

提交后我得到这个数组

array(
'Artproject' => array(
'ainum' => '1024',
'teamname' => 'Basketball',
'user_id' => '10',
'createdby' => ''
),
'Artdetail' => array(
'products' => array(
(int) 0 => '1',
(int) 1 => array(
'subproducts' => array(
(int) 0 => '1',
(int) 1 => array(
'options' => array(
(int) 0 => '1',
(int) 1 => '2'
)
),
(int) 2 => '2',
(int) 3 => '3'
)
)
)
)
)

我想将它保存在艺术细节表中id
user_id
艺术编号类别_id
产品ID
子产品_id
option_id
创建了

![这是我提交的表格 Click Here to view image

最佳答案

为什么不通过序列化数组来存储数据?简单地序列化($yourarr);并使用您想要识别数组的唯一 key 进行存储

关于php - CAKE-PHP如何在MYSQL表中保存动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28344412/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com