【问题标题】:Codeigniter - Is it possible to add multiple dynamic datas to viewCodeigniter - 是否可以添加多个动态数据来查看
【发布时间】:2018-12-03 12:50:42
【问题描述】:

当我更改此 $this->load->view('profile',['getPlacetype'=>$getPlacetype],$custom_errors );对此 $this->load->view('profile',['getPlacetype'=>$getPlacetype],$custom_errors ); 将没有未定义的索引,但选择数据将是无

profilecon

    public function index(){

    $this->load->model('Placetype');
    $getPlacetype = $this->Placetype->getPlacetype();


    if (!isset($_SESSION['username']) || empty($_SESSION['username'])) {
        header("Location: ".base_url()."index.php/login_con/login");
        die();

    }
    $custom_errors["nadate"] = false;
    if (isset($_POST['profile'])) {
        $this->form_validation->set_rules('mobilenum', 'Mobile Number', 
     'required');
        $this->form_validation->set_rules('homeadd', 'Home Address', 
     'required');
        $this->form_validation->set_rules('startdate', 'Start Date', 
     'required');
        $this->form_validation->set_rules('enddate', 'End Date', 
     'required');

        $custom_errors["nadate"] = ($this->verifydate($_POST["startdate"], 
      $_POST["enddate"]) < 3 ? false : true);


        //if form validation true
         if($this->form_validation->run() == TRUE && $this- 
        >verifydate($_POST["startdate"], $_POST["enddate"]) < 3) {
        $data = array(
                'username' => $this->session->userdata("username"),
                'mobilenum' => $_POST['mobilenum'],
                'homeadd' => $_POST['homeadd'],
                'startdate' => $_POST['startdate'],
                'enddate' => $_POST['enddate'],
            );

            $_SESSION["profile_data"] = $data;
            redirect("shopping_cart","refresh");
          }

          }

        $this->load->view('profile',     
        ['getPlacetype'=>$getPlacetype],$custom_errors );


            }

placetype_model

  <?php


  class Placetype extends CI_MODEL{


public function getPlacetype(){
$query = $this->db->get('placetype');
if($query->num_rows() > 0){
    return $query->result();
}
}



}

profile_view

        <div class="form-group">
        <label for="place_type">City/Province</label>
        <select name="place_type">
        <option value="place_type">-Please Select One-</option>
        <?php if(count($getPlacetype)):?>
        <?php foreach($getPlacetype as $getplacetype):?>
        <option value=
        <?php echo $getplacetype->place_id;?>>
       <?php echo $getplacetype->place_type;?> 
       <?php echo $getplacetype->place_price;?><span> PHP Shipping 
       Fee</span>
       </option>

      <?php endforeach;?>
       <?php else:?>
       <?php endif;?>
         </select>
       </div>
        <button class="btn btn-primary btn-block" 
      name="profile">Proceed</button>
      </div>
      </form>

     </div>

【问题讨论】:

  • 问题是?您面临哪些错误?预期的结果是什么? ...?
  • 有没有其他方法可以添加多个动态数据来查看,我面临的错误是我的 $custom_errors 变量变成了未定义的索引。

标签: php codeigniter


【解决方案1】:

向视图发送大量数据并不难。要了解的是如何使用您发送到视图的数组。简而言之,数组中的每个key 将是视图中variable 的名称。数组必须是关联数组。

考虑到这一点

//this line
$getPlacetype = $this->Placetype->getPlacetype();
// should be changed to this
$view_data['getPlacetype'] = $this->Placetype->getPlacetype();

然后

$custom_errors["nadate"] = ($this->verifydate($_POST["startdate"],
// changed to
$nadate = ($this->verifydate($_POST["startdate"],
                        $_POST["enddate"]) < 3 ? false : true);

你可以删除该行

$custom_errors["nadate"] = false;

改变这个

if($this->form_validation->run() == TRUE && 
   $this->verifydate($_POST["startdate"], $_POST["enddate"]) < 3) {

if($nadate === TRUE && $this->form_validation->run() === TRUE) {

如果上面的块没有被执行,那么创建一个错误消息并加载视图

$view_data['custom_errors'] = "Start and End dates were wrong";
$this->load->view('profile', $view_data);

视图的相关部分可以这样完成。

<div class="form-group">
    <label for="place_type">City/Province</label>
    <select name="place_type">
        <option value="place_type">-Please Select One-</option>
        <?php
        if(count($getPlacetype)) :
            foreach ($getPlacetype as $getplacetype):
                ?>
                <option value=<?php echo $getplacetype->place_id; ?>>
                    <?php
                    echo $getplacetype->place_type;
                    echo $getplacetype->place_price;
                    ?>
                <span> PHP Shipping Fee</span>
                </option>

                <?php
            endforeach;
            else:
               echo isset($custom_errors) ? $custom_errors : NULL;
            endif;
            ?>
    </select>
</div>

希望这能说明如何做你想做的事。

【讨论】:

    【解决方案2】:

    试试这个:定义数组空白。

       public function index(){
      $data = array();
    

    //这里你可以使用 $data 和 firstArray 一样你可以在你的视图页面中传递。你可以像这样使用多个数组然后在视图中传递。

    $data['firstArray']=get data;
    $data['secondArray']=get data;
     $this->load->view(''profile', $data);
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-21
      • 2010-09-14
      • 1970-01-01
      • 2015-04-21
      • 1970-01-01
      • 2019-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多