D
D
Denis2017-03-11 10:10:44
PHP
Denis, 2017-03-11 10:10:44

What is the best way to add the parsed data to the database?

Hello. There is a script which uses Simple HTML DOM. It collects some data from other sites. It turns out two arrays in two different functions. It is necessary to write these two arrays to the database so that the element from the first array corresponds to the element from the second array. The question is how best to do it. Enter into the database immediately from the data collection function, then how to correlate data from different arrays? Or get two arrays and then add them to the database? It is also desirable to check if there is an entry in the database with the same content and not add it again. I would like to hear advice from more experienced coders. Thanks in advance.

/**
 * @param $data
 * @return array
 * Getting item from page
 */
function get_item_name($data) {
    $links = [];
    $html = str_get_html($data);
    foreach ($html->find('.product-title') as $res) {
        $links[] = str_replace('Ненужно ', '', $res->plaintext);
    }
    return $links;
}

/**
 * @param $data
 * @return array
 * Getting price from page
 */
function get_item_price($data) {
    $price = [];
    $html = str_get_html($data);
    foreach ($html->find('span.online-price') as $ref) {
        $price[] = $ref->plaintext;
    }
    return $price;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2017-03-11
@webinar

The database should have an architecture such that it would be convenient to work with it later, if you need to write 50 more functions for this, write it. What is the problem?

Q
Quattro Vias, 2018-09-26
@Quattro_Vias

If the data can be collected at the same time, then one call to the database is enough.
In fact, it depends on what kind of information you receive and how you want to enter it - after deciding what will be more convenient ... I'm talking about such nuances: (if one of the sites is unavailable, etc.).
If the information does not have to pass the conditions (selection) before recording and the information from one site does not affect the information from another, I would make the entry in the database separate (by key). In case of problems with one of the sites or a request, it's easier to fix it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question