有两个表,如Package和PackageService。主要表格为Package Table。我在package.packageService=packageService.packageService id之间加入了这些表。我想使用id在具有JSON的package Table中保存一些数据。我有一个编辑页面。我想当我编辑一些包时,我可以选择一些包的多个功能,然后我可以在包视图和包编辑视图上显示这些功能。如何使用JSON清楚地做到这一点?
我的型号:
public function packages()
{
$this->db->select_max('packageId', 'MaxPackageId');
$this->db->select('IF(packageParent = 0, packageId, packageParent) as parent', FALSE);
$this->db->from('package');
$this->db->group_by('parent');
$subquery = $this->db->get_compiled_select();
$this->db->reset_query();
$this->db->select('package.*, packageService.*');
$this->db->from('package');
$this->db->join("($subquery) t1","t1.MaxPackageId = package.packageId");
$this->db->join('packageService', 'package.packageService = packageService.packageServiceId', 'LEFT');
$query = $this->db->get();
return $query->result();
}
包裹视图:
<section id="main-content">
<section class="wrapper site-min-height">
<!-- page start-->
<div class="row">
<!--price start-->
<div class="text-center feature-head">
<h1> PACKAGES </h1>
<p>Choose Your Special Package Plan. </p>
</div>
<?php foreach($packs as $get) { ?>
<div class="col-lg-3 col-sm-3">
<div class="pricing-table <?php if ($get->packageNameEn == 'Platinum') { echo 'most-popular'; } ?>">
<div class="pricing-head">
<h1> <?php echo $get->packageNameEn; ?> </h1>
<h5><del>€ <?php echo $get->packagePrice ?></del></h5>
<h2><span class="note">€</span> <?php echo $get->packagePriceCut ?> </h2>
</div>
<ul class="list-unstyled">
// I want to show package features with JSON here <?php echo $get->packageServiceNameEn ?>
</ul>
<div class="price-actions">
<a class="btn" href="javascript:;">Get Now</a>
<a class="btn" href="<?php echo base_url("package/edit/$get->packageId"); ?>">Edit</a>
</div>
</div>
</div>
<?php } ?>
</div>
<!-- page end-->
</section>
</section>
<!--main content end-->
我想您不想将packageService
表中的ID列表保存为features。有了这个,你需要删除这条线
$this->db->join('packageService', 'package.packageService = packageService.packageServiceId', 'LEFT');
当你想显示特征时:
控制器内:
// get all feautures
$this->data['feautures'] = $this->db->get('packageService')->result();
在您看来:
<ul class="list-unstyled">
// I want to show package features with JSON here
<?php
$get->packageService = json_decode($get->packageService);
foreach ($feautures as $key=> $feauture) {
if(in_array(feauture->packageServiceId, $package->packageService))
echo $feauture->packageServiceNameEn ;
}
?>
</ul>
要做到这一点,packageService
必须是一个具有packageService
表id的json字符串。