0

I am making a website, where data is stored in a text file, and I am using ajax with api-s to acces this text file and perform several tasks with it. I am stuck on how can I sort this products with php? Here is my products.txt JSON array of objects.

[
    {
        "id": "59d278cae7017",
        "name": "A",
        "price": "1",
        "quantity": 3,
        "image": "img_webshop\/productimage-59d74304917c2.jpg"
    },
    {
        "id": "59d27e20c8028",
        "name": "B",
        "price": "2",
        "quantity": 3,
        "image": "img_webshop\/productimage-59d743233c0cf.jpg"
    },
    {
        "id": "59d6a7ae16d15",
        "name": "C",
        "price": "3",
        "quantity": 2,
        "image": "img_webshop\/productimage-59d743392fbb5.jpg"
    },
    {
        "id": "59d6d6ee5f752",
        "name": "D",
        "price": "4",
        "quantity": 4,
        "image": "img_webshop\/productimage-59d74352d5b94.jpg"
    },
    {
        "id": "59d743d207bd5",
        "name": "E",
        "price": "5",
        "quantity": 1,
        "image": "img_webshop\/productimage-59d743d1e6e64.jpg"
    },
    {
        "id": "59d74451225ac",
        "name": "F",
        "price": "6",
        "quantity": 1,
        "image": "img_webshop\/productimage-59d7445120871.jpg"
    }
]

And obviously here is the start of the php:

//GETTING FROM FILE:

$sajProducts = file_get_contents( 'products.txt' );
$ajProducts = json_decode( $sajProducts );

?>

For the fornt-end I only got the html yet:

 <!-- PRODUCTS display and editing is handled here according USER or ADMIN-->

    <div id="pageViewProducts" class="page">
            <div class="lblWrapper"> 
                  <button type="button" class="btnShowPage" id="btnCreateProduct" data-showThisPage="pageCreateProduct"><i class="fa fa-plus"></i>&nbsp;Add Product</button>
                  <form id="frmSortBy">
                    <p>Sort by:</p>
                    <select>
                      <option id="oPriceLowToHigh">PRICE (LOW TO HIGH)</option>
                      <option id="oPriceHighToLow">PRICE (HIGH TO LOW</option>
                      <option id="oOnSale" selected>ON SALE</option>
                    </select>
                  </form>
                  <div id="lblProductList">
                    <!-- Generated dynamically -->
                  </div>
            </div>
    </div>

First of all I am interested how to handle this in the api then, the javascript on the front-end can come later.

codeDragon
  • 555
  • 1
  • 8
  • 28

0 Answers0