0

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?

asked Nov 5, 2018 at 1:17

1 Answer 1

2

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.

answered Nov 5, 2018 at 1:25

2 Comments

sorry mate, but when try to display it on view.blade it still seeing all array at once for each row. But when check the database. it proper inserting. How can it possible?
well, your initial issue of inserting is fixed. I think it's better that you post another question for the retrieval to avoid confusion on future readers.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.