Y
Y
Yevhenii K2015-10-27 15:25:55
PHP
Yevhenii K, 2015-10-27 15:25:55

The variable is not passed to the method, what's the problem?

Good afternoon! I need to display data (news) from the database by id, which is passed from outside (when iterating over an array from another table (category)). On the result page, the table data is displayed, but after the id is not passed to the method. And an error pops up. What is the problem?
Fatal error: Call to a member function fetchALL() on boolean in C:\OpenServer\OpenServer\domains\site.loc\add_news_script.php on line 32
html form

<!DOCTYPE html>
<html>
<head>
    <title>My blog</title>
    <meta charset="utf-8">
<link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body>
    <div class="header">
        <div class="container">
            <div class="logo">
               <!--<img src="css/images/logo.svg" height="210" width="50">-->
            </div>
            <div class="search">
                <form class="search-form">
                    <input type="text" placeholder="Search NewsRoom">
                    <button type="submit" class="search-btn">Search</button>
                </form>
            </div>
            <div class="vxod">
                <form name="authentication" method="POST" action="authentication_script.php">
                    <input type="text" name="login" placeholder="LOGIN">
                    <input type="password" name="password">
                    <input type="submit" value="Войти">
                </form>
            </div>
            <div class="reg">
                <button><a href="registration_form.html">Регистрация</a></button>
            </div>
        </div>
    </div>
    <div class="nav">
        <div class="menu">
            <ul class="category">
                <?php

include ("add_category_script.php");


$r = Category::getCategory();

foreach ($r as $v) {
    echo "<li>";
    echo $v['title'];
    echo "</li>";
} 
?>
            </ul>
        </div>
    </div>
    <div class="main">
        <div class="container">
            <?

include("add_news_script.php");

foreach ($r as $v) {
echo "<div class=rubrik><h2>".$v['title']."</h2></div>";
    //var_dump($v);

    $n = News::getNews($v['id']);
    foreach ($n as $k) {
    //var_dump($n);
    //var_dump($k);
    echo "<div class=title>";
        echo "<a href={$k['resource_link']} target=_blank><h3>{$k['title']}</h3></a>";
        echo 
            "<div class=part-content>".mb_substr($k['content'],0,80)."<a href=news.php?id={$k['id']}><span class=readmore>...Читать на сайте и комментировать...</span></a>
            </div>";
        echo "";
    echo "</div>";
    }

}

?>
        </div>
    </div>
</body>
</html>

php part
<?php
include_once ("db.php");

class News extends DB {
  public $title;
  public $content;
  public $res_link;
  public $res_name;
  public $date;
  public $category;

  public function __construct(Array $a) {
    $this->title = $a['title'];
    $this->content = $a['content'];
    $this->category = $a['category'];
    $this->res_link = $a['resource_link'];
    $this->res_name = $a['resource_name'];
    $this->date = date ('Y-m-d');
  }

  public function addNews() {                    
    $new_news = "INSERT INTO `news` (`id`, `title`, `content`, `category_id`, `date`, `resource_name`, `resource_link`) VALUES ('', '$this->title', '$this->content','$this->category', '$this->date', '$this->res_name', '$this->res_link')";
    $update = self::obj()->connect()->exec($new_news);
  }
  
  public static function getNews($id) {   //Эта часть не работает
    $cat_news = "SELECT * FROM `news` WHERE `news`.`category` = $id";
    $getnews = DB::obj()->connect()->query($cat_news);
      if(is_null($getnews)) {
        return [];
      }
    return $getnews->fetchALL(PDO::FETCH_ASSOC);
  }

  public static function getNewsById($id) {
    $id_news = "SELECT * FROM `news` WHERE `news`.`id` = $id";
    $getnews_id =DB::obj()->connect()->query($id_news);
      if(is_null($getnews)) {
        return [];
      }
    return $getnews->fetchALL(PDO::FETCH_ASSOC);
  }
}
//$new= new News($_POST);
//$new->addNews();
//header("Location: http://site.loc/add_news_form.html");

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Khizhniy, 2015-10-27
@AFI19

Perhaps the id is not returned in the $r = Category::getCategory(); method, but only the title.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question