不在视图上检查数据是否正确

时间:2015-09-07 22:22:49

标签: php codeigniter

我正在开发一个模块安装控制器。当我安装模块时,按钮应显示为修改。但不是出于某种原因

我已经安装了模块幻灯片,如下面的中的var转储结果所示

array (size=1)
  0 => string 'slideshow' (length=9)

但是当我重新加载/刷新我的页面时,属于该模块的按钮仍然显示安装。

问题:如何确保如果安装了模块,那么按钮会使其说明修改

查看

<div class="container-fluid">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="page-header">
<h1>Modules</h1>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="table-responsive">
<table class="table table-bordered table-hover">
<tbody>

<?php if ($extensions) { ?>

<?php foreach ($extensions as $extension) { ?>
<tr>

<td><?php echo $extension['name']; ?></td>

<td class="text-right">
<?php if (!$extension['installed']) { ?>
<a href="<?php echo $extension['install']; ?>" class="btn btn-success">Install</a>
<?php } else { ?>
<a href="<?php echo $extension['modify']; ?>" class="btn btn-primary">Modify</a>
<?php } ?>
</td>

</tr>
<?php }?>

<?php } ?>

</tbody>
</table>
</div>
</div>
</div>
</div>

控制器

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Module extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('extension/model_extension');
        $this->load->model('extension/model_module');
    }

    public function install() {
        $this->model_extension->install('module', $this->uri->segment(4));

        redirect('extension/module');
    }

    public function index() {
        $data['extensions'] = array();

        $extensions = $this->model_extension->get_installed('module');

        echo '<div class="container-fluid"';
        echo '<pre>';
        var_dump($extensions);
        echo '</pre>';
        echo '</div>';

        $files = glob('controllers/modules/*.php');

        if ($files) {
            foreach ($files as $file) {

                $extension = basename($file, '.php');

                $this->lang->load('module/'. $extension, 'english');

                $data['extensions'][] = array(
                    'name' => $this->lang->line('heading_title'),
                    'installed' => in_array($extension, $extensions),
                    'install' => site_url('extension/module/install' .'/'. strtolower($extension)),
                    'modify' => site_url('module/' . $extension)
                );
            }
        }

        $this->load->view('template/common/header_view');
        $this->load->view('template/extension/module_view', $data);
        $this->load->view('template/common/footer_view');
    }
}

模型

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Model_extension extends CI_Model {

    public function get_installed($type) {
        $extension_data = array();

        $this->db->select('*');
        $this->db->from($this->db->dbprefix . 'extension');
        $this->db->where('type', $type);
        $this->db->order_by('code');
        $query = $this->db->get();

        foreach ($query->result_array() as $result) {
            $extension_data[] = $result['code'];
        }

        return $extension_data;
    }

    public function install($type, $code) {
        $data = array(
            'type' => $type,
            'code' => $code
        );

        $this->db->set($data);
        $this->db->insert('extension');
    }

    public function uninstall($type, $code) {
        $this->db->where('type', $type);
        $this->db->where('code', $code);
        $this->db->delete($this->db->dbprefix . 'extension');
    }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

您知道是否安装了扩展程序的方法是评估此

in_array($extension, $extensions)

$extensions是这样的数组

array(
    0 => "extension_name_1",
    1 => "extension_name_2",
)

这个PHP函数

$extension 计算
basename($file, '.php');

PHP basename将返回(例如)extension_name_1.php,因此它永远不会与任何已安装的扩展匹配。

您应该取出basename扩展程序,或将其添加到已安装扩展程序数组的每个位置。