如何在编码器中将数据从控制器传递到我的视图?

时间:2015-12-28 05:15:51

标签: php codeigniter

我的控制器中有一个返回一些数据的方法。我目前只是在我的控制器中回显它现在我想将这些数据传递给我的视图。我希望我的代码在表格中显示但有些如何工作请查看我的代码并帮助我:

这是控制器:

public function index()
{
  $loginarry = $this->data['attendances'] =  $this->attendance_m->join_data();

  $logoutarry =$this->data['attendances'] =$this->attendance_m->second();

  //var_dump($total);
  $timeduration=0;
  $login_i=0;
  $logout_i=0;
  //var_dump($loginarry);
  //        echo ("@@@@<br/><br/>");
  //var_dump($logoutarry);
  echo ("Attay LEngth = " . count($loginarry)." & ".count($logoutarry)."\n<br/>");
  while(true)
  {
    if( $login_i >= count($loginarry) && $logout_i >= count($logoutarry))
      break;

    if( $logout_i >= count($logoutarry))
    {
      echo ("Login[".$login_i."] : ".$loginarry[$login_i]->date_data." and Logout : -----------------\n<br/>");
      $login_i++;
      continue;
    }
    if( $login_i >= count($loginarry))
    {
      echo ("Login : ----------------- and Logout[".$logout_i."] : ".$logoutarry[$logout_i]->date_data."\n<br/>");
                    $logout_i++;
      continue;
    }
    //echo( "******* ".(new DateTime($loginarry[$login_i+1]->date_data))->format('U') . " **** ".(new DateTime($logoutarry[$logout_i]->date_data))->format('U'). "\n<br/>");

    //check if next login time is smaller then current logout time - if so skip current login time
    if($login_i < (count($loginarry)-1) && (new DateTime($logoutarry[$logout_i]->date_data))->format('U') > (new DateTime($loginarry[$login_i+1]->date_data))->format('U'))
    //if( date_diff(date_create($logoutarry[$logout_i]->date_data), date_create($loginarry[$login_i+1]->date_data)) > 0 )
    {
      echo ("Login[".$login_i."] : ".$loginarry[$login_i]->date_data." and Logout : -----------------\n<br/>");
      $login_i++;
      $timeduration += 10;
      continue;
    }
    if( (new DateTime($loginarry[$login_i]->date_data))->format('U') > (new DateTime($logoutarry[$logout_i]->date_data))->format('U') )
    //if( date_diff(date_create($loginarry[$login_i]->date_data), date_create($logoutarry[$logout_i]->date_data)) > 0 )
    {
      echo ("Login : ----------------- and Logout[".$logout_i."] : ".$logoutarry[$logout_i]->date_data."\n<br/>");
      $logout_i++;
      $timeduration += 10;
      continue;
    }

    //if more logout entries then skip to last logout entry
    if( $login_i < (count($loginarry)-1) && $logout_i < (count($logoutarry)-1) && (new DateTime($logoutarry[$logout_i+1]->date_data))->format('U') < (new DateTime($loginarry[$login_i+1]->date_data))->format('U'))
    {
      echo ("Login : ----------------- and Logout[".$logout_i."] : ".$logoutarry[$logout_i]->date_data."\n<br/>");
      $logout_i++;
      $timeduration += 10;
      continue;
    }

    echo ("Login[".$login_i."] : ".$loginarry[$login_i]->date_data." and Logout[".$logout_i."] : ".$logoutarry[$logout_i]->date_data);
    echo (" and Duration = ".((new DateTime($logoutarry[$logout_i]->date_data))->format('U') - (new DateTime($loginarry[$login_i]->date_data))->format('U')) ."\n<br/>");

    $timeduration += ((new DateTime($logoutarry[$logout_i]->date_data))->format('U') - (new DateTime($loginarry[$login_i]->date_data))->format('U'));
    $login_i++;
    $logout_i++;

  }
  echo ("Total Duration = ".$timeduration. " second(s) " );
  echo"<br>";
  echo("Total Duration = ".$timeduration/'3600'."Hours ");

  $this->data['attendance_dropdown'] = $this->attendance_m->get_emp_list();
  $this->data['subview'] = 'admin/attendance/index';
  $this->load->view('admin/_layout_main', $this->data);

} 

守则:

<?php
        $loginarry = $attendances;
        $logoutarry =$attendances_logouts;
        $timeduration=0;
        $login_i=0;
        $logout_i=0;


        //echo ("Array Length = " . count($loginarry)." & ".count($logoutarry)."\n<br/>");
while(true)
{ 
    if( $login_i >= count($loginarry) && $logout_i >= count($logoutarry))
        break;

    if( $logout_i >= count($logoutarry))
    {
        echo ("Login[".$login_i."] : ".$loginarry[$login_i]->date_data." and Logout : -----------------\n<br/>");
        $login_i++;
        continue;
    }
    if( $login_i >= count($loginarry))
    {
        echo ("Login : ----------------- and Logout[".$logout_i."] : ".$logoutarry[$logout_i]->date_data."\n<br/>");
                    $logout_i++;
        continue;
    }
    //echo( "******* ".(new DateTime($loginarry[$login_i+1]->date_data))->format('U') . " **** ".(new DateTime($logoutarry[$logout_i]->date_data))->format('U'). "\n<br/>");

    //check if next login time is smaller then current logout time - if so skip current login time
    if($login_i < (count($loginarry)-1) && (new DateTime($logoutarry[$logout_i]->date_data))->format('U') > (new DateTime($loginarry[$login_i+1]->date_data))->format('U'))
    //if( date_diff(date_create($logoutarry[$logout_i]->date_data), date_create($loginarry[$login_i+1]->date_data)) > 0 )
    {
        echo ("Login[".$login_i."] : ".$loginarry[$login_i]->date_data." and Logout : -----------------\n<br/>");
        $login_i++;
        $timeduration += 10;
        continue;
    }
    if( (new DateTime($loginarry[$login_i]->date_data))->format('U') > (new DateTime($logoutarry[$logout_i]->date_data))->format('U') )
    //if( date_diff(date_create($loginarry[$login_i]->date_data), date_create($logoutarry[$logout_i]->date_data)) > 0 )
            {
        echo ("Login : ----------------- and Logout[".$logout_i."] : ".$logoutarry[$logout_i]->date_data."\n<br/>");
                    $logout_i++;
                    $timeduration += 10;
                    continue;
            }

    //if more logout entries then skip to last logout entry
    if( $login_i < (count($loginarry)-1) && $logout_i < (count($logoutarry)-1) && (new DateTime($logoutarry[$logout_i+1]->date_data))->format('U') < (new DateTime($loginarry[$login_i+1]->date_data))->format('U'))
    {
        echo ("Login : ----------------- and Logout[".$logout_i."] : ".$logoutarry[$logout_i]->date_data."\n<br/>");
                    $logout_i++;
                    $timeduration += 10;
                    continue;
    }

    echo ("Login[".$login_i."] : ".$loginarry[$login_i]->date_data." and Logout[".$logout_i."] : ".$logoutarry[$logout_i]->date_data);
    echo (" and Duration = ".((new DateTime($logoutarry[$logout_i]->date_data))->format('U') - (new DateTime($loginarry[$login_i]->date_data))->format('U')) ."\n<br/>");

    $timeduration += ((new DateTime($logoutarry[$logout_i]->date_data))->format('U') - (new DateTime($loginarry[$login_i]->date_data))->format('U'));
    $login_i++;
    $logout_i++;

}
    echo ("Total Duration = ".$timeduration. " second(s) " );
    echo"<br>";
    echo("Total Duration = ".$timeduration/'3600'."Hours ");


        ?>  

output

1 个答案:

答案 0 :(得分:2)

在CodeIgniter中向数据传递数据的基本概述。

您的控制器

使用控制器创建或获取数据(下面的简化示例)。

<?

Class MyController extends CI_Controller {

    /**
    * Some method I use
    */
    public function index() {

        $data = array(
            'variable_one'   => 'value one',
            'variable_two'   => 'value two',
            'variable_three' => 'value three',
            'variable_four'  => 'value four'
        );

        $this->load->view('my-view', $data);

    }

}

您的观点

然后我可以在我的视图中访问该数据:

<html>
<head>
    <title>Some Sample CI View</title>
</head>
<body>
    <h1><? echo $variable_one; ?></h1>
    <h2><? echo $variable_two; ?></h2>

    <p><? echo $variable_three; ?></p>
    <p><? echo $variable_four; ?></p>
</body>
</html>

如果您在视图中访问该数据时遇到问题,那么就该开始调试您的代码,以确保您提供的数据存在并且采用您期望的格式。