如何将变量从控制器传递到视图以及如何根据我在CodeIgniter中获得的变量显示视图?

时间:2018-01-24 13:38:24

标签: php codeigniter

我将控制器中的数据传递给具有变量值的视图,但在我看来,我有两种形式,我想根据我拥有的变量数据显示表单。我尝试使用if(isset$_GET[])方法,但它不起作用。以下是我的代码。请纠正我在哪里犯错误。

public function epacat($pa_cat_id)
{ 
  $this->top();
  $data['pa_cat'] = $this->adata->parentCategories();

  //BELOW I AM PASSING DATA
  $data['epa_cat'] = $this->adata->epacatQ($pa_cat_id);
  $this->load->view('am/parent-categories/parent_categories',$data);
  $this->bottom();
}

<div class="row">
  //WANT TO DISPLAY IF I GOT VARIABLE
  <?php if(isset(['epacat'])):?>
    <div class="col-md-12">
      <?php echo form_open('admin/apacat');?>
      <label><h4>Update Parent Category</h4></label>
      <?php echo form_input(['name'=>'pa_cat_name','class'=>'form-control','placeholder'=>'Enter your parent category','value'=>set_value('pa_cat_name',$pc->pa_cat_name)]);?>
      <button type="reset" class="btn btn-warning">Reset</button> <button type="submit" class="btn btn-primary">Submit</button>
     <?php form_close();?>
    </div>

   //WANT TO DISPLAY IF I NOT GOT VARIABLE
  <?php else:?>
    <div class="col-md-12">
      <?php echo form_open('admin/apacat');?>
      <label><h4>Add Parent Category</h4></label>
      <?php echo form_input(['name'=>'pa_cat_name','class'=>'form-control','placeholder'=>'Enter your parent category','value'=>set_value('pa_cat_name')]);?>
      <button type="reset" class="btn btn-warning">Reset</button> <button type="submit" class="btn btn-primary">Submit</button>
      <?php form_close();?>
    </div>
  <?php endif;?>
</div>

3 个答案:

答案 0 :(得分:1)

您已在控制器中正确执行此操作

$data['epa_cat'] = $this->adata->epacatQ($pa_cat_id);
$this->load->view('am/parent-categories/parent_categories',$data);

在视图中,$data['epa_cat']变为$epa_cat

例如,如果$epa_cat是一个字符串,那么

<p><?php echo $epa_cat; ?></p>

或者,如果$epa_cat是布尔值,您可以像这样检查

if ($epa_cat) {
    // Do stuff
}

答案 1 :(得分:0)

你可以这样写

public function epacat($pa_cat_id)
{ 
$this->top();
$data['pa_cat'] = $this->adata->parentCategories();

//BELOW I AM PASSING DATA
$data['epa_cat'] = $this->adata->epacatQ($pa_cat_id);
$this->load->view('am/parent-categories/parent_categories',$data);
$this->bottom();
}

<div class="row">
//WANT TO DISPLAY IF I GOT VARIABLE
<?php if($epacat):?>
<div class="col-md-12">
<?php echo form_open('admin/apacat');?>
<label><h4>Update Parent Category</h4></label>
<?php echo form_input(['name'=>'pa_cat_name','class'=>'form-control','placeholder'=>'Enter your parent category','value'=>set_value('pa_cat_name',$pa_cat->pa_cat_name)]);?>
<button type="reset" class="btn btn-warning">Reset</button> <button type="submit" class="btn btn-primary">Submit</button>
<?php form_close();?>
</div>

//WANT TO DISPLAY IF I NOT GOT VARIABLE
<?php else:?>
<div class="col-md-12">
<?php echo form_open('admin/apacat');?>
<label><h4>Add Parent Category</h4></label>
<?php echo form_input(['name'=>'pa_cat_name','class'=>'form-control','placeholder'=>'Enter your parent category','value'=>set_value('pa_cat_name')]);?>
<button type="reset" class="btn btn-warning">Reset</button> <button type="submit" class="btn btn-primary">Submit</button>
<?php form_close();?>
</div>
<?php endif;?>

</div>

答案 2 :(得分:0)

只是为了好玩(而不是测试)

使用一个HTML块并根据$ epa_cat

派生不同的位
<?php
if($epa_cat){
    $label = 'Update';
    $value = set_value('pa_cat_name', $pc->pa_cat_name);
} else {
    $label = 'Add';
    $value = set_value('pa_cat_name');
}
?>
<div class="row">
    <div class="col-md-12">
        <?= form_open('admin/apacat'); ?>
        <label><h4><?=$label;?> Parent Category</h4></label>
        <?= form_input(['name' => 'pa_cat_name', 'class' => 'form-control', 'placeholder' => 'Enter your parent category', 'value' => $value]); ?>
        <button type="reset" class="btn btn-warning">Reset</button>
        <button type="submit" class="btn btn-primary">Submit</button>
        <?php form_close(); ?>
    </div>
</div>

仍然不确定$pc->pa_cat_name来自哪里,但是根据提供的内容,这将是第一次尝试。

我还将<?php echo更改为<?=作为可以使用的演示。