Answer the question
In order to leave comments, you need to log in
What could be the problem(array, foreach)?
Good day!
I parse matches and everything seems to be fine. So I started testing the code, and stumbled upon a problem. The bottom line is that when it turns out that there is 1 record (about a match) in the array, then it is not written to the database, if there are 2 or more of them, then everything is fine. And in general, the first entry in the array (about the match) behaves somehow strangely. Maybe I messed up somewhere?
(the file is scheduled to run)
<?php
use DiDom\Document;
class Task_VKparse extends Minion_Task {
protected static $_timeout = 1;
protected function _execute(array $params) {
//Получаем id матчей
$html = new Document('http://www.gosugamers.net/dota2/gosubet', TRUE);
$elements = $html->find('div.box')[0]->find('table tbody tr');
$matches = array();
foreach ($elements as $el) {
$gg_link = $el->find('td a')[0]->attr('href');
//Преобразование данных
$match = array(
'gg_link' => $gg_link,
);
$matches[] = $match;
}
// Переход на страницу матча
foreach ($matches as $match) {
// Получение html кода страницы матча
$html = new Document('http://www.gosugamers.net' . $match['gg_link'], TRUE);
$format = $html->find('p.bestof')[0]->text();
$streams = $html->find('span.match-stream-tab');
$match['format'] = (new GG)->format($format);
$match['team1'] = $html->find('div.opponent1 h3')[0]->text();
$match['team2'] = $html->find('div.opponent2 h3')[0]->text();
$match['tournament'] = $html->find('fieldset legend a')[0]->text();
$match['stream_ru'] = (new GG)->stream_ru($streams);
$match['stream_en'] = (new GG)->stream_en($streams);
// Занесение полученных данных в массив
$matches[] = $match;
// Minion_CLI::write(var_dump($matches));
}
// Запись результата в БД
foreach ($matches as $match) {
$match_orm = new Model_Match;
// Загрузка матча, если он уже есть в БД
$match_orm->where('gg_link', '=', $match['gg_link'])->find();
// Внесение новых данных о матче
$match_orm->values($match);
$match_orm->save();
}
}
}
Answer the question
In order to leave comments, you need to log in
The above is shitty code . The $matches array is filled in two arrays with different data. Use different variables or null before use. And in general, trace the variable at each stage, check what data is stored in the variable.
Apparently, you need to supplement the existing array elements with information from the match page in the second foreach. You add the match information to the end of the $matches array.
To change the current array element in the foreach loop, you need to add an array element passing to foreach by pointer and change the code a bit. I think this is how it will work:
<?php
use DiDom\Document;
class Task_VKparse extends Minion_Task {
protected static $_timeout = 1;
protected function _execute(array $params) {
//Получаем id матчей
$html = new Document('http://www.gosugamers.net/dota2/gosubet', TRUE);
$elements = $html->find('div.box')[0]->find('table tbody tr');
$matches = array();
foreach ($elements as $el) {
$gg_link = $el->find('td a')[0]->attr('href');
//Преобразование данных
$match = array(
'gg_link' => $gg_link,
);
$matches[] = $match;
}
// Переход на страницу матча
foreach ($matches as &$match) {
// Получение html кода страницы матча
$html = new Document('http://www.gosugamers.net' . $match['gg_link'], TRUE);
$format = $html->find('p.bestof')[0]->text();
$streams = $html->find('span.match-stream-tab');
$match['format'] = (new GG)->format($format);
$match['team1'] = $html->find('div.opponent1 h3')[0]->text();
$match['team2'] = $html->find('div.opponent2 h3')[0]->text();
$match['tournament'] = $html->find('fieldset legend a')[0]->text();
$match['stream_ru'] = (new GG)->stream_ru($streams);
$match['stream_en'] = (new GG)->stream_en($streams);
// Minion_CLI::write(var_dump($matches));
}
// Запись результата в БД
foreach ($matches as $match) {
$match_orm = new Model_Match;
// Загрузка матча, если он уже есть в БД
$match_orm->where('gg_link', '=', $match['gg_link'])->find();
// Внесение новых данных о матче
$match_orm->values($match);
$match_orm->save();
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question