Note that there are some explanatory texts on larger screens.

plurals
  1. POFilter page_id in content display class
    text
    copied!<p>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..</p> <p>(items in my menu link give ?page=x, x = id)</p> <p>My class so far:</p> <pre><code>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-&gt;conn = mysql_connect(Config::DB_HOST, Config::DB_USER, Config::DB_PASS); mysql_select_db(Config::DB_NAME, $this-&gt;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-&gt;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-&gt;article = $this-&gt;get_content_articles($page); $content = ""; foreach ( $this-&gt;article as $article) $content .= ' &lt;div class="blok"&gt; &lt;h2&gt; &lt;/br&gt; '.$article['title'].'&lt;/h2&gt; &lt;/br&gt; '.$article['content'].' &lt;/br&gt; '.$article['date'].' &lt;/div&gt; '; return $content; } } </code></pre> <p>How i run the class:</p> <pre><code> include("classes/content.class.php"); $content = ""; $content = new Content(); echo $content-&gt;display_content($_GET['page']); </code></pre> <p>Db table if necessary:</p> <p>|id| |page_id| |title| |content| |date| |position|</p> <p>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.</p> <hr> <p>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:</p> <pre><code>include("classes/content.class.php"); global $page; $page = $_GET['page']; $content = new Content(); echo $content-&gt;display_content($page); </code></pre> <p>With this i get the following error: Warning: Invalid argument supplied for foreach() in ... content.class.php on line 42</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload