Page 32 - 3-2
P. 32

/*
             * Following code will list all the products
             */


            // array for JSON response
            $response = array();


            // include db connect class
            require_once __DIR__ . '/db_connect.php';


            // connecting to db
            //$db = new DB_CONNECT();


            // get all products from products table
            $ r e s u lt  =  m y s q li_ q u e r y ( $ c o n , " S E L E C T  * F R O M  m d p _ t a b le 1 " )  o r
            d ie ( m y s q li_ e r r o r ( ) ) ;


            // check for empty result
            if (mysqli_num_rows($result) > 0) {
                // looping through all results
                // products node
                $response["products"] = array();


                while ($row = mysqli_fetch_array($result)) {
                    // temp user array
                    $product = array();
                    $product["code"] = $row["code"];
                    $product["name"] = $row["name"];
                    $product["num"] = $row["num"];
                    $product["price"] = $row["price"];


                    // push single product into final response array
                    array_push($response["products"], $product);
                }
                // success
                $response["success"] = 1;


                // echoing JSON response
                echo json_encode($response);
            } else {
                // no products found
                $response["success"] = 0;
                $response["message"] = "No products found";


                // echo no users JSON


                                                         - 32 -
   27   28   29   30   31   32   33   34   35   36   37