Cheesecake
Cheesecake

Reputation: 13

PHP and MySQL query outputting data twice

I have a CMS Page module and when you go to a page it shows it twice. This is what calls the function:

$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();
}
}

and this is the function:

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>'; 
    }
    }
    }

but when I go to the page in the browser it shows the query data twice.

Upvotes: 1

Views: 133

Answers (2)

Timothy Ha
Timothy Ha

Reputation: 399

I think your code in pages() is called twice because your class doesn't have a constructor and you have two lines $pages = new pages; and $pages->pages(); producing same results.

Upvotes: 1

Isuru Madusanka
Isuru Madusanka

Reputation: 373

Try DISTINCT select data. It will merge same rows as one.

SELECT DISTINCT * FROM pages WHERE pagename = '$page'

Try this might be work because sometime when calling $rows = mysqli_fetch_array($data) in while loop must be problem.

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>'; 
    }
    }

Upvotes: 1

Related Questions