简体   繁体   English

在内容显示类别中过滤page_id

[英]Filter page_id in content display class

I have built a class to display content from database. 我建立了一个类来显示数据库中的内容。 I want to filter it by page_id now, so I can link content to the right page_id. 我现在想按page_id进行过滤,因此可以将内容链接到正确的page_id。 So i began trying to make it work with $_GET but so far it only gave errors.. 所以我开始尝试使其与$ _GET一起使用,但到目前为止,它只给出了错误。

(items in my menu link give ?page=x, x = id) (我的菜单链接中的项为?page = x,x = id)

My class so far: 到目前为止,我的课程是:

include_once('./includes/config.php');
class Content {

global $page;    
public $id;
public $page_id;
public $title;
public $content;
public $position;



var $conn;

function Content()
        {
                $this->conn = mysql_connect(Config::DB_HOST, Config::DB_USER, Config::DB_PASS);
                mysql_select_db(Config::DB_NAME, $this->conn);
        }


function get_content_articles($page)
{
    $sql = "SELECT id, page_id, title, content, date, position FROM articles ORDER BY id, position WHERE page_id = ".$page." ";
    $result = mysql_query($sql, $this->conn);

                if ( !$result )
                        return false;
    $fetch_all = array();

    while($article = mysql_fetch_assoc($result))
    $fetch_all[] = $article;

return $fetch_all;
}


public function display_content($page) {
    $this->article = $this->get_content_articles($page);
    $content = "";
    foreach ( $this->article as $article)   

    $content .= '
        <div class="blok">
        <h2>
        </br>
        '.$article['title'].'</h2>
        </br>
        '.$article['content'].'
        </br>
        '.$article['date'].'
        </div>
    ';

    return $content;
  }
}

How i run the class: 我如何上课:

    include("classes/content.class.php");


$content = "";
$content = new Content();

echo $content->display_content($_GET['page']); 

Db table if necessary: db表(如有必要):

|id| | id | |page_id| | page_id | |title| |标题| |content| |内容| |date| |日期| |position| |位置|

Since i know this is probably not the proper way to do this, can you give me some tips to do this? 由于我知道这可能不是执行此操作的正确方法,因此您可以给我一些提示吗? I'm pretty new to classes. 我上课很新。


Ok.. so i changed the code a bit, removed the global $page from the class and replaced it to the page where i exec the class: 好的..所以我稍微更改了代码,从类中删除了全局$ page并将其替换为我执行该类的页面:

include("classes/content.class.php");

global $page; 
$page = $_GET['page'];
$content = new Content();
echo $content->display_content($page); 

With this i get the following error: Warning: Invalid argument supplied for foreach() in ... content.class.php on line 42 与此有关,我得到以下错误:警告:第42行上... content.class.php中为foreach()提供了无效参数。

ok, the problem is that you are using global $page in your class definition. 好的,问题是您在类定义中使用了global $page If you want to make $page global, you have to put global $page at the beginning of a method, not the class. 如果要使$ page全局,则必须将global $page放在方法的开头,而不是类的开头。

You can't use following lin inside class: 您不能在课程内部使用以下lin:

global $page; 全球$ page;

You don't require this. 您不需要这个。 Just comment it out and it will work. 只需注释掉它,它将起作用。

The 'invalid argument supplied...' error means that the variable you pass in your foreach loop is not an array. “提供了无效的参数...”错误意味着您在foreach循环中传递的变量不是数组。 By looking at your code, one thing I can think of is that you have an error in your query. 通过查看您的代码,我可以想到的一件事是查询中存在错误。 Your query seems fine so I'd say there is an invisible character somewhere that MySQL doesn't understand. 您的查询似乎很好,所以我想说在MySQL无法理解的地方有一个看不见的字符。 Your method then returns false, and the foreach loop can't take 'false' as an argument. 然后,您的方法返回false,并且foreach循环不能将“ false”作为参数。

I would suggest trying mysql_query($sql, $this->conn) or die(mysql_error()) (I suggest you use this only for radical debugging as it might display some informations about your table's structure). 我建议尝试使用mysql_query($sql, $this->conn) or die(mysql_error()) (我建议您仅将其用于基本调试,因为它可能会显示有关表结构的某些信息)。 This will try the query, and print you the error if there is one. 这将尝试查询,如果有错误,则将您打印出来。

(Sorry I added another answer for this, but the text is too long for a comment) (很抱歉,我为此添加了另一个答案,但是文本太长,无法发表评论)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM