0

Using the code below, I am getting divs with class 'post' from an external HTML file and print them to the posts page. Everything shown below works as it should be, however I would like to supplement the code with some automation. More precisely, instead of having hard coded values in the arrays I want to pass variable which were obtained by iterating through all divs with the needed class. Could you please help to implement such action. Thanks in advance.

PHP

<?php

$doc = new DOMDocument();
$doc->loadHTMLFile($url);

$xpath = new DOMXpath($doc);

$post = $xpath->query("//*[contains(@class, 'post')]");

$array = iterator_to_array($post);
?>

HTML posts page

<div><?php  echo $doc->saveHTML($array[0]);?></div>
<div><?php  echo $doc->saveHTML($array[1]);?></div>
<div><?php  echo $doc->saveHTML($array[2]);?></div>

HTML external file

<div class="post">
    post 1 
</div>      
<div class="post">
    post 2 
</div>      
<div class="post">
    post 3
</div>
4
  • 1
    Not clear: instead of having hard coded values in the arrays: which arrays? The only hard-coded literal in your code is the XPath query, but that is not an array, so I don't understand what you want. Commented Feb 27, 2016 at 17:04
  • means instead of post 1 in html, you want dynamic php variable? can you elaborate ? Commented Feb 27, 2016 at 17:04
  • Instead of having this $array[0];$array[1];$array[2] I want to have this $array[$some_variable] :) Commented Feb 27, 2016 at 17:06
  • @EmilGurbanov can you check my updated answer if it solves your question ? Commented Feb 27, 2016 at 17:07

1 Answer 1

1

Not sure if i understood your question .. is this what u wanted ?

foreach ($array as $key => $value)
 echo "<div> $value </div>";

OR

foreach ($array as $key => $value)
 echo "<div> ".$doc->saveHTML($value)." </div>";

Update

If you are looking for a function [Askd in comments]

function printdiv($value){
 echo "<div> ".$doc->saveHTML($value)." </div>";
}

then call it from wherever you want eg. via foreach loop

foreach ($array as $key => $value)
 printdiv($value);
Sign up to request clarification or add additional context in comments.

6 Comments

This actually works, but is there any way to have the divs hard coded and pass the $value to each of them?
So I don't want the divs will be created dynamically as well, because the post will contain lots of different elements in the main post div, so I just need to have a string of php in it, if I explain clear :)
@Aamir don't use function name as print
@aamir instead of echo in the function, use return.
@jigar that was a typo , i mentioned printdiv while calling but mentioned print in function declaration .. anyways updated, thanks... Emil glad it worked!
|

Your Answer

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.