使用Ionic框架与PHP文件

Cod*_*ing 5 php ionic

我正在使用Ionic Framework创建一个移动应用程序.我得到了所有在线创建的html页面.现在我想要一些后端代码从sql server获取数据.接收数据与php没有问题.但是当我使用php页面时,我没有使用Ionic创建的界面.

我如何使用php页面(而不是html)仍然可以从离子中获取布局?示例:my scorebord.php

<?php
$servername = "localhost:3306";
$username = "ssss";
$password = "dffd";
$dbname = "ddddd";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT user_username,user_city,user_highscore FROM tbl_user";
$result = $conn->query($sql);
?>
<ion-view style="" title="Scorebord">
    <ion-content class="has-header" overflow-scroll="true" padding="true" style="background: url(img/hX1ml1TVGgABo3ENE6Qg_menu3.png) no-repeat center;">
        <h1 style="">Scorebord</h1>
        <table style="width:100%">
            <?php
            if ($result->num_rows > 0) {
                // output data of each row
                while ($row = $result->fetch_assoc()) {
                    ?>

                    <tr>
                        <td><?php echo $row["user_username"] ?></td>

                    </tr>
                    <?php
                }
            } else {
                echo "0 results";
            }
            $conn->close();
            ?>

        </table> 
    </ion-content>
</ion-view>
Run Code Online (Sandbox Code Playgroud)

顺便说一句:在像这样的php文件中配置我的数据库是否安全?谁有好的选择?

Tar*_*nes 1

与 Web 服务器不同,移动应用程序将保存在可能无法解释 PHP 代码的设备上。如果您不知道/不想要 javascipt 那么 iframe 可能是您唯一的选择。

example.com/table.php

        <h1 style="">Scorebord</h1>
        <table style="width:100%">
            <?php
            if ($result->num_rows > 0) {
                // output data of each row
                while ($row = $result->fetch_assoc()) {?>
                    <tr> <td><?php echo $row["user_username"] ?></td></tr>
                    <?php
                }
            } else {
                echo "0 results";
            }
            $conn->close();
            ?>
        </table> 
Run Code Online (Sandbox Code Playgroud)

你的应用程序

<ion-view style="" title="Scorebord">
    <ion-content class="has-header" overflow-scroll="true" padding="true" style="background: url(img/hX1ml1TVGgABo3ENE6Qg_menu3.png) no-repeat center;">
        <iframe src="example.com/table.php"/>
    </ion-content>
</ion-view>
Run Code Online (Sandbox Code Playgroud)

但更好的解决方案是使用 JavaScript 发出JSON 数据完整 html 模板(如 table.php)的 http 请求。

我会使用 json 后端

高分.php

<?php
$servername = "localhost:3306";
$username = "ssss";
$password = "dffd";
$dbname = "ddddd";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT user_username,user_city,user_highscore FROM tbl_user";
$result = $conn->query($sql);
$json = mysqli_fetch_all ($result, MYSQLI_ASSOC);
echo json_encode($json);
Run Code Online (Sandbox Code Playgroud)

你的应用程序

angular.module('ionicApp', [])

.controller('MainCtrl', function($scope, $http) {
  $http.get('http://example.com/highscores.php').then(function(resp) {
    $scope.users = resp.data;
  }, function(err) {
    console.error('ERR', err);
  })
});

  <html ng-app="ionicApp">
  <body ng-controller="MainCtrl">
    <ion-content>
      <ion-list>
        <ion-item ng-repeat="user in users">
          {{user.user_username}}
        </ion-item>
      </ion-list>
    </ion-content>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)