Codeigniter-编码,解码JSON

时间:2018-08-02 21:56:08

标签: javascript php json codeigniter codeigniter-3

有两个表,例如Package和PackageService。主要表是包装表。我在package.packageService = packageService.packageServiceId之间加入了这些表。我想使用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-->

1 个答案:

答案 0 :(得分:2)

我想您不会将packageService表中ID的列表另存为功能。 为此,您需要删除此行

$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必须是ID为packageService表的json字符串。