Categorized | PHP

How to include elements from different webpages using PHP or JavaScript to my home page?

Posted on 19 February 2010 by Abidoon

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

tags from Post.php into a
tag on MyHome.php. Is it possible to do this?

Tags | , , , , , , , ,

3 Responses to “How to include elements from different webpages using PHP or JavaScript to my home page?”

  1. George H says:

    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.

  2. Fjandr says:

    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 #1

    This is post #1″;
    $post[1] = “

    Post #2

    This is post #2″;
    $post[2] = “

    Post #3

    This is post #3″;
    foreach ($post as $x) {
    echo $x;
    }
    echo “

    More posts

    More 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.”;

  3. Sandeep M says:

    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.


Leave a Reply

 

February 2010
M T W T F S S
« Oct   Mar »
1234567
891011121314
15161718192021
22232425262728