V
V
Vasily2016-12-04 20:04:07
PHP
Vasily, 2016-12-04 20:04:07

How to include a php file using AngularJS ng-include detectives with the same scope as parent?

Using the ng-include directive, I want to include additional blocks in index.php. The index.php file has a file (because I use it on other pages too) with a database connection. When connecting new blocks, queries that should be executed there give an error like: mysqli_query () expects parameter 1 to be mysqli, null given in ... (that is, there is no connection to the database)
I think this is due to the fact that angular for included files creates its own scopes. I tried to include the same controller that I use in index.php in the included file, but it didn't change anything.
Part of index.php:

<?php	
  
include("include/db_connect.php");// поключение ДБ
include("functions/functions.php");
session_start();
include("include/auth_cookie.php");

$sorting = $_GET["sort"];   
switch ($sorting)
{
  case 'price-asc';
  $sorting = 'price ASC';
  $sort_name = 'Od tanich do drogich';
  break;

  case 'price-desc';
  $sorting = 'price DESC';
  $sort_name = 'Od drogich do tanich';
  break;
  
  case 'popular';
  $sorting = 'count DESC';
  $sort_name = 'Popularne';
  break;
  
  case 'news';
  $sorting = 'datetime DESC';
  $sort_name = 'Nowości';
  break;
  
  case 'model';
  $sorting = 'model';
  $sort_name = 'Nowości';
  break;
  
  default:
  $sorting = 'products_id DESC';
  $sort_name = 'Bez sortowania';
  break;                           
} 


?>
<!DOCTYPE html>
<html ng-app='myApp'>
<head>

  <meta http-equiv="content-type" content="text/html; charset=iso-8859-2" />
  <link href="css/reset.css" rel="stylesheet" type="text/css" />
  <link href="css/style.css" rel="stylesheet" type="text/css" />
  <link href="trackbar/trackbar.css" rel="stylesheet" type="text/css" />
  
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.js"></script>
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular-cookies.js"></script>
  <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.3/angular-route.js"></script>

  <script type="text/javascript" src="/js/controllers.js"></script>
  <script type="text/javascript" src="/js/jquery-1.8.2.min.js"></script> 
  <script type="text/javascript" src="/js/jcarousellite_1.0.1.js"></script> 
  <script type="text/javascript" src="/js/shop-script.js"></script>
  <script type="text/javascript" src="/js/jquery.cookie.min.js"></script>
  <script type="text/javascript" src="/trackbar/jquery.trackbar.js"></script>
  <script type="text/javascript" src="/js/TextChange.js"></script>
      <title>Sklep internetowy sprzętu telekomunikacyjnego</title>
</head>
<body ng-controller="productCtrl">
  <div id="block-body" >
 <!--  <div ng-repeat="products in newParam">{{products}} </div> -->

   <!-- Проблемные блоки -->
  <ng-include src="'include/block-header.php'"></ng-include>

    <div id="block-right">
     <ng-include src="'include/block-category.php'"></ng-include>
     <ng-include src="'include/block-parameter.php'"></ng-include>
     <ng-include src="'include/block-news.php'"></ng-include>
    </div>

    <div id="block-content">

One of the included files:
<div id="block-news" ng-controller="productCtrl">

  <center><img id="news-prev" src="/images/img-prev.png" /></center>


  <div id="newsticker">
    <ul>


      <?php
      $result = mysqli_query($link, "SELECT * FROM news ORDER BY id DESC");
      If (mysqli_num_rows($result) > 0)
      {
        $row = mysqli_fetch_array($result);
        do
        {	

          echo '
          <li>
            <span>'.$row["date"].'</span>
            <a href="" >'.$row["title"].'</a>
            <p>'.$row["text"].'</p>
          </li>

          ';

        }
        while ($row = mysqli_fetch_array($result)); 
      } 
      ?>


    </ul>

  </div>
  <center><img id="news-next" src="/images/img-next.png" /></center>
  




</div>

There is nothing special in the controller yet. Connecting the database in each file I think is not an option.
I understand that since angular attaches files, it may not read php, which in this case can be done without using <?php include "file.php" ?>. I think $http.post is not even worth trying as seen from the provided file. Or how? PS. Angular and mysqli queries on the main page work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eudj1n, 2016-12-10
@eudj1n

No way. Use the language constructs of the language you are writing in - in this case, include .
If you need the subsequent receipt of data in the loaded page - use AJAX.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question