简体   繁体   中英

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. So i began trying to make it work with $_GET but so far it only gave errors..

(items in my menu link give ?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:

|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:

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

ok, the problem is that you are using global $page in your class definition. If you want to make $page global, you have to put global $page at the beginning of a method, not the class.

You can't use following lin inside class:

global $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. 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. Your method then returns false, and the foreach loop can't take 'false' as an argument.

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). 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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