首先:我知道这是一个非常混乱的代码,不是正确的方法。但它首次尝试编写类和函数
我希望它做的是:在div中输出"articles"表中的所有行(再次对每行进行div"repeated"(,并按id和日期对其进行排序。稍后我想通过页面id进行过滤。
它的作用:只输出一行,其他行不显示。。
我希望我的解释有道理。。
这就是我的"文章"表现在的样子:
|id | | page_id | | title | | content | | date | | position |
标题为纯文本,内容为html,来自管理员面板中的所见即所得编辑器,日期为DD-MM-YYYY。
这就是我的"课堂":
include_once('./includes/config.php');
class Content {
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()
{
$sql = "SELECT id, page_id, title, content, date, position FROM articles ORDER BY id, position";
$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() {
$this->article = $this->get_content_articles();
foreach ( $this->article as $article)
$content = '<div class="blok">
<h2>[id:'.$article['id'].']
</br>
[title: '.$article['title'].']</h2>
</br>
[content: '.$article['content'].']
</br>
[date: '.$article['date'].']
</div>';
return $content;
}
}
这就是我使用它的方式:
$content = new Content();
echo$content->display_content((;
此处的测试页面:http://thepiratehenk.nl/pgwe/testcontent.php
有人对如何解决这个问题有建议吗?(对不起,我的英语不好,问题很乱,我希望这一切都有道理。(
好的,谢谢你到目前为止的回答。我知道我不应该在课堂上使用mysql_函数。。但这是我此刻最能理解的唯一方式。
我将display_content((函数改为:
public function display_content() {
$this->article = $this->get_content_articles();
foreach ( $this->article as $article)
$content = "";
$content .= '<div class="blok">
<h2>[id:'.$article['id'].']
</br>
[title: '.$article['title'].']</h2>
</br>
[content: '.$article['content'].']
</br>
[date: '.$article['date'].']
</div>';
return $content;
}
}
这就是你想要的样子吗?无论如何,结果不会改变。。
$content = '<div class="blok">...';
在foreach循环的每一次迭代中,您都将用条目的值重新分配变量$content
。因此$content
将只包含最后一个条目。
使用附加运算符.=
。
$content .= '<div class="blok">...';
编辑:正如@JoseAreas在评论中所说,你应该在进入循环之前写下:
$content = "";
以避免引起注意。
这是正确的方式,@bwoebi建议:
public function display_content() {
$this->article = $this->get_content_articles();
$content = "";
foreach ( $this->article as $article) {
$content .= '<div class="blok">
<h2>[id:'.$article['id'].']
</br>
[title: '.$article['title'].']</h2>
</br>
[content: '.$article['content'].']
</br>
[date: '.$article['date'].']
</div>';
}
return $content;
}