A
A
arti_djeims2016-05-24 12:20:31
PHP
arti_djeims, 2016-05-24 12:20:31

What is the best way to modify a js file through php?

Let's say we have a file (our database) in which data is stored in this format:

products = [
  {
  	name: "test1",
    price: 240,
  id: '15',
  img: 'http://cs624326.vk.me/v624326831/29f16/hW5YEUNgNvk.jpg',
  miniAbout:  "бла бла бла бла бла бла бла бла бла ",
  about: "Полное тестовове описание этого товара, этот товар очень классный соетуем его купить, не пожелееете! Ах да, забыли сказать, Бла бла бла бла бла бла бла бла бла бла бла бла бла бла бла бла бла бла бла бла бла бла бла бла бла бла бла бла бла бла бла бла бла ",
  category: "Электроника"
  }, 
  {
  	name: "test2",
    price: 250,
  id: '14',
    img: 'http://cs627419.vk.me/v627419421/1d545/lODqeVtNlyc.jpg',
  miniAbout:  "бла бла бла бла бла бла бла бла бла ",
  category: "Игры"
  },
  {
  	name: "Call Of Duty",
    price: 260,
  id: '13',
    img: 'http://cs7002.vk.me/v7002154/18cec/BOasKNcqf_k.jpg',
  miniAbout:  "бла бла бла бла бла бла бла бла бла ",
  category: "Одежда"
  }
]

So, what is the best way to add new products to such a database, perhaps there is some convenient solution in PHP? Tried file_put_contents but that changes the whole file and we only need to add new products on line 2. What can you advise to do?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander Aksentiev, 2016-05-24
@Sanasol

This is json.
It must be parsed and rewritten completely every time, for such a case.
And it's not a database at all, it's not a database at all.

V
Vyacheslav Grachunov, 2016-05-24
@Qwentor

Use any real database

G
Gluck Virtualen, 2016-05-24
@gluck59

You will have a lot of sex with adding products to your (hehe) "database" ... Think about it and go to the simplest mysql before it's too late.

I
Ivan Ivanov, 2016-05-24
@surlan

Yes, you can't call it a database, just an array in json.
Nevertheless ... if the conditions are exactly like this, then you need:
There are no other options.
And if possible, it's better to switch to a real database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question