dongping6974
2018-01-24 13:38
浏览 53
已采纳

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

I am passing the data from the controller to view with the variable value but in my view, I have two forms and I want to display the form according to the variable data I have. I tried using if(isset$_GET[]) method but it doesn't work. Below is my code. Please rectify me where I am doing the mistake.

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>

图片转代码服务由CSDN问答提供 功能建议

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

  public function epacat($ pa_cat_id)
 {
 $ this-&gt; top(); 
 $ data [  'pa_cat'] = $ this-&gt; adata-&gt; parentCategories(); 
 
 //以下我通过数据
 $ data ['epa_cat'] = $ this-&gt; adata-&gt; epacatQ(  $ pa_cat_id); 
 $ this-&gt; load-&gt; view('am / parent-categories / parent_categories',$ data); 
 $ this-&gt; bottom(); 
} 
 
&lt;  div class =“row”&gt; 
 //如果我得到变量
&lt;?php if(isset(['epacat'])),我想要显示:?&gt; 
&lt; div class =“col-  md-12“&gt; 
&lt;?php echo form_open('admin / apacat');?&gt; 
&lt; label&gt;&lt; h4&gt;更新父类别&lt; / h4&gt;&lt; / label&gt; 
&lt;  ;?php echo form_input(['name'=&gt;'pa_cat_name','class'=&gt;'form-control','placeholder'=&gt;'输入您的父类别','value'=&gt; set_value(  'pa_cat_name',$ pc-&gt; pa_cat_name)]);?&gt; 
&lt; button type =“reset”class =“btn btn-warning”&gt;重置&lt; / button&gt;  &lt; button type =“submit”class =“btn btn-primary”&gt;提交&lt; / button&gt; 
&lt;?php form_close();?&gt; 
&lt; / div&gt; 
 
 // WANT 显示如果我没有变量
&lt;?php else:?&gt; 
&lt; div class =“col-md-12”&gt; 
&lt;?php echo form_open('admin / apacat');  ?&gt; 
&lt; label&gt;&lt; h4&gt;添加父类别&lt; / h4&gt;&lt; / label&gt; 
&lt;?php echo form_input(['name'=&gt;'pa_cat_name','class'=&gt;  ;'form-control','占位符'=&gt;'输入您的父类别','value'=&gt; set_value('pa_cat_name')]);?&gt; 
&lt; button type =“reset”class =  “btn btn-warning”&gt;重置&lt; / button&gt;  &lt; button type =“submit”class =“btn btn-primary”&gt;提交&lt; / button&gt; 
&lt;?php form_close();?&gt; 
&lt; / div&gt; 
&lt;?php endif  ;?&gt; 
&lt; / div&gt; 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doujuan9698 2018-01-24 13:49
    已采纳

    You're doing it correctly in the controller

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

    In the view, $data['epa_cat'] becomes $epa_cat.

    So for example, if $epa_cat is a string you'd do

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

    Or if $epa_cat is a boolean, you could check it like so

    if ($epa_cat) {
        // Do stuff
    }
    
    已采纳该答案
    打赏 评论
  • dongqian6234 2018-01-24 13:43

    You can write like this

    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>
    
    打赏 评论
  • dro59505 2018-01-24 14:02

    Just for fun ( and not tested )

    Use the one block of HTML and derive the bits that are different depending upon $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>
    

    Still not sure where $pc->pa_cat_name comes from, but with what was provided, this would be a first attempt.

    I also changed <?php echo to <?= as a demonstration that can be used.

    打赏 评论

相关推荐 更多相似问题