douxian1923 2014-10-11 21:34
浏览 29
已采纳

从视图到模型的变量。 MVC

I am a beginner and wrote a web page based on the tutorial JREAM (MVC). I've encountered a problem in displaying articles.
How can I send a variable $from (index.php) from the controller to the model?
Of course, I can connect to the database in the index.php but whether that is good?

index.php

    $limit = LIMIT; // article per page
    $article = $this->Ile[0]['num']; // number of articles 

    if (isset($_GET['page'])){
        $page = $_GET['page'];
    } else {
        $page = 1;
    }

    $allpage = round($article/$limit);
    $from = $limit * ($page - 1);

    // $from send value to model and model return array with article

    foreach($this->Article as $key => $value)
    {
        echo '<div style="float:left;width:660px;"><hr/><br/><p><span class="strong" style="color:#CC0000;">';
        echo $value['title'];
        echo '</span></p><br/><p>';
        echo $value['content'];
        echo '</p><div style="height:130px;">';
        echo $value['photo'];
        echo '</div></div>';
    }                   
    echo '<hr/>';
    echo '<div style="width: 660px; position: relative; pointer-events: none; text-align: center; top: 14px;">'.$page.' z '.$allpage.'</div>';

    if ($page == 1 && $page < $allpage)
    {
        echo '<a href="news?page='.($page+1).'" style="float: right;">STARSZE >></a>';
    }
    elseif ($page >= $allpage)
    {
        echo '<a href="news?page='.($page-1).'" style="float: left;"><< NOWSZE</a>';
    } else {
        echo '<a href="news?page='.($page-1).'" style="float: left;"><< NOWSZE</a>';
        echo '<a href="news?page='.($page+1).'" style="float: right;">STARSZE >></a>';
    }
?>

news.php

 class News extends Controller {

   function __construct() 
   {
     parent::__construct();
   }

   function index() 
   {
     $this->view->title = 'News';
     $this->view->Ile = $this->model->Ile();
     $this->view->Article = $this->model->Article();
     // I can put some value in ...->Article(0); and it works, but how connect in index.php

     $this->view->render('header');
     $this->view->render('news/index');
     $this->view->render('footer');
   }

 }

news_model.php

<?php

  class News_Model extends Model
  {
    public function __construct()
    {
      parent::__construct();
    }

    public function Ile()
    {
      return $this->db->select('SELECT COUNT(*) as num FROM `articles`');
    }

    public function Article($from)
    {
      return $this->db->select('SELECT * FROM `articles` ORDER BY `newsid` DESC LIMIT  '.$from.', '.LIMIT.'');
    }

  }

Regards,
Thomas

  • 写回答

1条回答 默认 最新

  • dpdjv9559 2014-10-11 21:56
    关注

    You should do this in your controller not in view :

    Controller :

    $limit = LIMIT; // article per page
    $article = $this->view->Ile[0]['num']; // number of articles 
    
    if (isset($_GET['page'])){
        $page = $_GET['page'];
    } else {
        $page = 1;
    }
    
    $allpage = round($article/$limit);
    $from = $limit * ($page - 1);
    $this->view->Article = $this->model->Article($form); // This is how you can pass $form to model
    

    And then you can assign This variable to your view like :

    $this->view->page = $page
    $this->view->allpage = $allpage
    

    Now in view you can use $this->page instead of $page, same for $allpage

    Hope it helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序