0

I am getting an Id from a table, which I need to get a DB record of a news item, so the admin can update it. It gets to the controller just fine, but gives me the error when I try to pass it to the model and do the query.

What really puzzles me is that I have used the exact same structure many times throughout this project and never gotten this error before.

This is the controller:

    include_once'models/Admin_NewsTable.class.php';
    $news = new Admin_NewsTable( $dbh );

    // This one works fine
    $aNews = $news->getAllNews();

    $newsTable = h('');

    foreach($aNews as $news){
        $newsTable .= '<tr>';
        $newsTable .= ' <td>'.$news['title'].'</td>';
        $newsTable .= ' <td>'.substr($news['content'], 0, 50).'</td>';
        $newsTable .= ' <td>'.$news['author_name'].'</td>';
        $newsTable .= ' <td>'.$news['date_created'].'</td>';
        $newsTable .= '<td id="updateNews'.$news["news_id"].'" class="newsUpdateButton updateButton text-center" type="submit" name="updateNews" action="index.php?page=admin-news"><i class="fa fa-pencil-square"></i></td>';
        $newsTable .= '<td id="deleteNews'.$news["news_id"].'" class="newsDeleteButton deleteButton text-center"  data-toggle="modal"><i class="fa fa-minus-circle"></i></td>';
        $newsTable .= '</tr>';
    }

    $view = include_once'views/admin-news-html.php';

    // This one does not work
    if(isset($_POST['updateNews'])){
         $updateId = $_POST['updateNews'];
         // Calling the function in the model
         $news->getNewsPost($updateId);
    }

This is the model:

    class Admin_NewsTable{
    private $dbh;

    // Connect to database
    public function __construct ( $pdo ) {
        $this->dbh = $pdo;
    }

    // This one works fine
    function getAllNews(){

    $sth = $this->dbh->prepare('SELECT news.news_id, news.title, news.content, news.date_created, users.username FROM news INNER JOIN users WHERE users.user_id = news.author_id');

    $sth->execute();

    // Set up an array to hold the individual post as an array
    $newsData = array();

    if($sth){
        while($r=$sth->fetch()){
            // Temp post array
            $tmpNews = array();

            $tmpNews['news_id'] = $r['news_id'];
            $tmpNews['title'] = $r['title'];
            $tmpNews['content'] = $r['content'];
            $tmpNews['date_created'] = $r['date_created'];
            $tmpNews['author_name'] = $r['username'];

            array_push($newsData, $tmpNews);

        }

        return $newsData;
    }

}


    // THis one is the problem
    public function getNewsPost($updateId){

    $sth = $this->dbh->prepare('SELECT news.news_id, news.title, news.content, news.date_created, users.username FROM news INNER JOIN users WHERE users.user_id = news.author_id AND news.news_id = ?');

    $sth->execute(array($updateId));

    if($sth){
        while($r=$sth->fetch()){
            // Temp post array
            $newsPost = array();

            $newsPost['news_id'] = $r['news_id'];
            $newsPost['title'] = $r['title'];
            $newsPost['content'] = $r['content'];
            $newsPost['date_created'] = $r['date_created'];
            $newsPost['author_name'] = $r['username'];

        }
    }

    return $newsPost;
}

It breaks before it even gets to the model, so what am I doing wrong?

I have the same structure for another query using the same controller and model, which works fine, so I am really puzzled...

EDIT: I managed to solve the problem by calling the query function from another controller. I have other controllers, where this structure doesn't cause any problems. Can anyone explain to me why this doesn't work in this particular case?

MaDaHoPe
  • 133
  • 10

1 Answers1

2

Because in foreach loop you have taken news as variable which overides the object value of the news and makes it a variable..

You can try changing the variable name to some other names or change the object name.In the related line use

$obj = new Admin_NewsTable( $dbh );

and invoke class method using

$obj->getNewsPost($updateId);
pritesh
  • 2,162
  • 18
  • 24
  • Oh my. What a blunder from my side, totally missed that one!! Thank you for lending me your sharp eye for detail. Another lesson in naming your variables proper! Thank you so much my good man! – MaDaHoPe Jan 25 '16 at 17:36
  • Yeah it happens sometimes...never mind...learning from mistakes is good always... – pritesh Jan 25 '16 at 17:48