How to include elements from different webpages using PHP or JavaScript to my home page?
ElQuestionaire asked:
I have a homepage (MyHome.php). On the homepage, I’d like to display the latest 3 posts from another page (Post.php). For example. I want to include the first 3
I have a homepage (MyHome.php). On the homepage, I’d like to display the latest 3 posts from another page (Post.php). For example. I want to include the first 3


It really depends on how you are accessing your database….doesn’t matter if it is SQL or flat files, but why not send a url variable to your post.php file like:
include (‘post.php?amount=3′); That way when you hit your post.php script you can check if there is an amount variable via the GET method and just do a quick for loop to display that many posts. Hope this helps.
There could easily be better ways to do this, but without actually seeing how your data is organized I only have one workable suggestion.
In post.php, add each post to array and then echo the array contents to output the posts. Then include post.php in homepage.php.
post.php
————-
$post = array();
$post[0] = “Post #1This is post #1″;
$post[1] = “Post #2This is post #2″;
$post[2] = “Post #3This is post #3″;
foreach ($post as $x) {
echo $x;
}
echo “More postsMore posts here!”;
myhome.php
——————-
require_once(‘post.php’); // Gives you access to all the stuff in post.php
echo “Let’s see the first three posts:”;
foreach ($post as $x) {
echo $x;
}
echo “Stuff on homepage other than top posts.”;
You can do this in different ways as explained below
I. Using “curl” and fetch the page u want then parse it according to your display needs.
II. If website supports RSS Feeds just call the rss – xml and parse it according to your display needs
III. If website supports web services use REST services to get the content from the web service provider.
On this 3 methods you can choose any of them depending upon the site from which u r getting data or depending on your knowledge on those concepts.
If you use curl then u need to know regix pattern matching to parse data.
The rest two can be parsed by using xpath.