Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中使用模型中控制器的变量_Php_Oop_Model View Controller_Pagination - Fatal编程技术网

在PHP中使用模型中控制器的变量

在PHP中使用模型中控制器的变量,php,oop,model-view-controller,pagination,Php,Oop,Model View Controller,Pagination,我想在我的模型中使用来自控制器的变量$page1,但我收到了消息 “未定义变量:文章中的第1页\u模型” 现在,我的问题是:我如何声明这个变量以便模型能够使用和识别 我的控制器 <?php require MODELS . "articles_model.php"; class Articles { function index() { $articlesModel = new ArticlesModel(); $articles =

我想在我的模型中使用来自控制器的变量
$page1
,但我收到了消息

“未定义变量:文章中的第1页\u模型”

现在,我的问题是:我如何声明这个变量以便模型能够使用和识别

我的控制器

   <?php

require MODELS . "articles_model.php";


class Articles {

    function index() {

        $articlesModel = new ArticlesModel();
        $articles = $articlesModel->getAll();
        $art = $articlesModel->getArticlesByLimit();


        $page =$_GET['page'];

        if($page==""||$page=="1") {
          $page1 = 0;
        } else {
          $page1 = ($page*5)-5;
        }


        $cou = count($art);
        $x = $cou/5;
        $x = ceil($x);
控制器

<?php

require MODELS . "articles_model.php";


class Articles {

    function index() {

        $articlesModel = new ArticlesModel();
        $articles = $articlesModel->getAll();


        $page =$_GET['page'];

        if($page==""||$page=="1") {
          $page1 = 0;
        } else {
          $page1 = ($page*5)-5;
        }




        $art = $articlesModel->getArticlesByLimit($page1);
        $cou = count($art);
        $x = $cou/5;
        $x = ceil($x);

class Articles{$public$page1=0;function index(){$articlesModel=new articlesModel();$articlesModel=$articlesModel->getAll();$art=$articlesModel->getArticlesByLimit();$page=$\u GET['page'];if($page==“”| |$page==“1”){$this->page1=0;}else{$this->page1=($page*5)-5;}$cou=count($art);$x=$cou/5;$x=ceil($x);为什么不按原样声明小时模型方法中的变量:getArticlesByLimit($page),然后从控制器传递它:$articlesModel->getArticlesByLimit(1);对于第1页…?它不起作用…上面描述的2个都不起作用…而不是尝试在系统的主要组件之间传递单个值。即控制器、模型和视图。您是否考虑过使用“数据传输对象”?它可以保存有关数据有效性的状态指示器,以及验证值和错误消息圣人。它没有逻辑,应该是不变的。在我看来,它很有用。想澄清你的答案吗?你的代码有什么不同?
<?php

require MODELS . "articles_model.php";


class Articles {

    function index() {

        $articlesModel = new ArticlesModel();
        $articles = $articlesModel->getAll();


        $page =$_GET['page'];

        if($page==""||$page=="1") {
          $page1 = 0;
        } else {
          $page1 = ($page*5)-5;
        }




        $art = $articlesModel->getArticlesByLimit($page1);
        $cou = count($art);
        $x = $cou/5;
        $x = ceil($x);
<?php
require_once "db_model.php";

class ArticlesModel extends DB {

     function getAll() {
      $statement = $this->executeQuery("SELECT * FROM articles");
      return $statement->fetchAll(PDO::FETCH_ASSOC);
    }

    function getArticlesByLimit($page1) {
        $statement = $this->executeQuery("SELECT * FROM articles limit $page1,5");
        return $statement->fetchAll(PDO::FETCH_ASSOC);
    }