Ejemplo n.º 1
0
function get_product(ProductCategory $category)
{
    $data = search_products($category, 0);
    $data_products = $data["Products"];
    if (is_array($data_products)) {
        foreach ($data_products as $data_product) {
            if ($data_product["BestOffer"]["IsAvailable"]) {
                return new Product($data_product["Id"], $data_product["Name"], (double) $data_product["BestOffer"]["SalePrice"], $category, $data_product["Description"], $data_product["BestOffer"]["IsAvailable"]);
            }
        }
    }
    return null;
}
Ejemplo n.º 2
0
<?php

/*----------------------------------------------
------------------------------------------------
-----------------SEARCH PRODUCTS----------------
------------------------------------------------
----------------------------------------------*/
function search_products($string)
{
    // Search products in the DB containing a specific string.
    $array = array();
    require $_SERVER['DOCUMENT_ROOT'] . '/e_commerce/sql/sql_connexion.php';
    $request = $bdd->prepare("SELECT id, name FROM products WHERE name LIKE ?");
    $request->execute(array('%' . $string . '%'));
    while ($fetch = $request->fetch()) {
        array_push($array, array($fetch['id'], $fetch['name']));
    }
    return $array;
}
/*-----------------------------------
-------------------------------------
-----------------MAIN----------------
-------------------------------------
-----------------------------------*/
if (isset($_POST['string'])) {
    echo json_encode(search_products($_POST['string']));
}