我有一个CMS页面模块,当你转到页面时,它会显示两次。这就是调用函数的原因:
$pages = new pages;
if(isset($_GET['action']) && ($_GET['action'] == "pages")){
if($_GET['action'] === 'pages' && isset($_GET['page'])){
$pages->pages();
} elseif ($_GET['action'] === 'pages' && !isset($_GET['page'])) {
$pages->pagelist();
}
$pages->pagesAdminBar();
if(isset($_GET['mode'])){
if($_GET['mode'] == "edit"){
$pages->editPage();
}
if($_GET['mode'] == "addpage"){
$pages->addpage();
}
if($_GET['mode'] == "pageadmin"){
$pages->pageadmin();
}
$pages->pagesAdminBar();
}
}
这是功能:
public function pages(){
global $dbc, $parser, $layout, $main, $settings, $core;
if(isset($_GET['page'])){
$page = mysqli_real_escape_string($dbc, $_GET['page']);
if(!isset($_GET['page']) && !isset($_GET['action'])){
$page = $settings['home_display'];
}
$query = "SELECT * FROM pages WHERE pagename = '$page'";
$data = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($data)) {
echo '<div class="shadowbar">';
echo'<h3>' . $row['pagename'] . '</h3>';
$parsed = $parser->parse($row['body']);
echo '<pre>'.$parsed.'</pre>';
echo '</div>';
}
}
}
但是当我在浏览器中转到页面时,它会显示两次查询数据。
答案 0 :(得分:1)
尝试DISTINCT选择数据。它会将相同的行合并为一个。
SELECT DISTINCT * FROM pages WHERE pagename = '$page'
尝试这可能是有效的,因为有时在while循环中调用$ rows = mysqli_fetch_array($ data)时必须有问题。
public function pages(){
global $dbc, $parser, $layout, $main, $settings, $core;
if(isset($_GET['page'])){
$page = mysqli_real_escape_string($dbc, $_GET['page']);
if(!isset($_GET['page']) && !isset($_GET['action'])){
$page = $settings['home_display'];
}
$query = "SELECT * FROM pages WHERE pagename = '$page'";
$data = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($data);
echo '<div class="shadowbar">';
echo'<h3>' . $row['pagename'] . '</h3>';
$parsed = $parser->parse($row['body']);
echo '<pre>'.$parsed.'</pre>';
echo '</div>';
}
}
答案 1 :(得分:1)
我认为你在pages()中的代码被调用了两次,因为你的类没有构造函数,你有两行
$pages = new pages;
和$pages->pages();
产生相同的结果。