jeudi 30 avril 2015

AngularJS delay loading data from MySQL database

I'm using angular in a simple application like:

app.js

app = angular.module('app', []);
app.controller("NavCtrl",function($scope,$http){
    var serviceBase = 'v1/';
    $http.get(serviceBase + 'orderoverview/58').then(function (results) {
        $scope.categories = results.data;
        $scope.getTotal = function(){
    var total = 0;
    for(var i = 0; i < $scope.categories.length; i++){
        var categories = $scope.categories[i];
        total += (categories.productPrice * categories.orderdetailAantal);
        if(categories.extra1 != "")
        {
          total += categories.extrasPrice;
        }
    }
    return total;
}
    });
});

HTML

<!DOCTYPE html>
<html lang="en" ng-app="app">
  <head>
    <meta charset="utf-8">
    <title>Served</title>
  </head>
  <body ng-cloak="">

<div class="navbar navbar-default megamenu">
  <div class="container" ng-controller="NavCtrl">
      <ul class="nav navbar-nav">
        <li ng-repeat= "p in categories">{{p.orderdetailAantal}} x {{p.productTitle}} {{p.productPrice}} <br> {{p.extra1}} {{p.extra2}} {{p.extra3}} {{p.extra4}} {{p.extrasPrice}}</li>
      </ul>
      <p>Total: {{ getTotal() }} euro</p>
</div>
</div>

    <script src="libs/angular.js"></script>
    <script src="app/app.js"></script>
  </body>
</html>

And this works but every time I load the page there is a delay of almost 3 seconds to get the data from the db and show it in my html page... When I surf directly to the API, I built using PHP, the required data is shown instantly...

How is this possible and how can I speed up this proces?

(if needed, my API and .htaccess)

API

require '.././libs/Slim/Slim.php';
\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();
$app = \Slim\Slim::getInstance();

$app->get('/orderoverview/:customerID', function ($customerID) {

$sql = "SELECT `customerName` FROM `customers` WHERE `customerID` = $customerID";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $customername = $row["customerName"];
    }
} 
else {
    echo "Customer name failure";
}

$sql = "SELECT `orderID` FROM `order` WHERE `customerID` = $customerID";
$result = $conn->query($sql);
if($result->num_rows > 1){
    echo "You already have a pending order! <br>";
}
else if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $orderID = $row["orderID"];
    }
    step2($orderID, $customername);
} else {
    echo "GET orderID failure";
}
});
$app->run();

function step2($orderID, $customername){

mysql_connect($servername, $username, $password);
mysql_select_db($dbname);
$query = mysql_query("SELECT `orderdetailID` FROM `orderdetail` WHERE `orderID` = '$orderID'");
$results = array();
while($line = mysql_fetch_array($query, MYSQL_ASSOC)){
    $results[] = $line;
}
step3($orderID, $results, $customername);
}

function step3($orderID, $results, $customername){
global $conn, $servername, $username, $password, $dbname;

$to = 0;
$extracounter = 1;
$extrasprice = 0;

foreach($results as $key=>$odt)
{
    $extracounter = 1;

    $odb = $odt["orderdetailID"];
    $sql = "SELECT `productID`, `orderdetailAantal` FROM `orderdetail` WHERE `orderdetailID` = '$odb'";
    $sql2 = "SELECT `extraID` FROM `orderdetailextra` WHERE `orderdetailID` = '$odb'";

    $result = $conn->query($sql);
    $result2 = $conn->query($sql2);

    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $sql3 = "SELECT `productTitle` FROM `products` WHERE `productID` = '$row[productID]'";
            $sql4 = "SELECT `productPrijs` FROM `products` WHERE `productID` = '$row[productID]'";

            $result3 = $conn->query($sql3);
            if($result3->num_rows > 0) {
                while($row1 = $result3->fetch_assoc()){
                    $productTitle = $row1["productTitle"];
                }
            } 

            $result4 = $conn->query($sql4);
            if($result4->num_rows > 0) {
                while($row1 = $result4->fetch_assoc()){
                    $productPrice = $row1["productPrijs"];
                }
            } 

            $order[$to] = array(
                "orderdetailID" => $odb,
                "productTitle" => $productTitle,
                "productPrice" => $productPrice,                
                "orderdetailAantal" => $row["orderdetailAantal"],
                "extra1" => "",
                "extra2" => "",
                "extra3" => "",
                "extra4" => "",
                "extrasPrice" => ""
            );
            $to++;
        }
    } else {
        echo 'fout sql 1';
    }                

            }
        }

    }

}
    echoResponse(200, $order);

}
function echoResponse($status_code, $response) {
    global $app;
    $app->status($status_code);
    $app->contentType('application/json');
    echo json_encode($response,JSON_NUMERIC_CHECK);
}

.HTACCESS

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]
RewriteBase /localsites/serf/weap/api/v1

Aucun commentaire:

Enregistrer un commentaire