I need to use json_encode on a array before inserting it on database. So my array contains a 9 photo links. The proper way to inserting DB photo links are row by row. But when I try to start insert it. All nine links one row and it keeps going like that. 1 row 9 links, 1 row 9 links. And I don't know what s the problem.
Here is the code
$rota = new \DOMXPath($parser);
$images = $rota->query("//div[@class='areapageDetail']//div[@class='areapageDetailList_item_img']//img");
foreach ($images as $image) {
$photos[] = $image->getAttribute("src");
}
This is the photo url contains array.
and the way I insert is right below.
foreach ($outlineUrl as $results) {
if (strpos($results, 'http://www.daikyo.co.jp/') === 0) {
$html = file_get_contents($results);
$DOMParser = new \DOMDocument();
$DOMParser->loadHTML($html);
$changeForMyDB = [
'region' => '関西',
'photo' => json_encode($photos),
'link' => json_encode($results),
'building_name' => '',
'price' => '',
'old_price' => '',
'extend' => '',
'address' => '',
'total_house' => '',
'rooms' => '',
'cons_finish' => '',
'entry' => '',
'balcony' => '',
'company_name' => '',
];
This is the part of the code. For example $results
is array too which is contain web site links. And there is no problem with that. I can insert $results array links row by row. But not photo links. Why is that happening just for the photo links?
1 Answer 1
You also have to index each photo in that case
$i = 0;
foreach ($outlineUrl as $results) {
if (strpos($results, 'http://www.daikyo.co.jp/') === 0) {
$html = file_get_contents($results);
$DOMParser = new \DOMDocument();
$DOMParser->loadHTML($html);
$changeForMyDB = [
'region' => '関西',
'photo' => json_encode($photos[$i]),
'link' => json_encode($results),
'building_name' => '',
'price' => '',
'old_price' => '',
'extend' => '',
'address' => '',
'total_house' => '',
'rooms' => '',
'cons_finish' => '',
'entry' => '',
'balcony' => '',
'company_name' => '',
];
}
$i++;
}
you also have to make sure that photos count matches your outlineurl array.