1

I have a XML feed with the following URL with IDs of trips to be retrieved.

http://www.expeditiontrips.com/xml/triplist.xml

Based on the each trip information can be retrieved from following URL where the ID becomes the XML name

http://www.expeditiontrips.com/xml/trips/2945.xml

I need to show this feed on my website using PHP. I retrieved trip IDs using the following code but then I have no clue how to use that information to get the individual trip details and show them in my site.

<?php 

    $ch = curl_init('http://www.expeditiontrips.com/xml/triplist.xml');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $xml_raw = curl_exec($ch);
    curl_close($ch);
    $trips = simplexml_load_string($xml_raw);

    foreach ($trips as $trip):
        echo '<li><div class="title">'.$trip.'</div></li>';
    endforeach;

?>
3
  • What is your expected result, a page with all the links? Or do you want to display the information of one of these ids? Commented Jul 2, 2015 at 10:11
  • I want to show a grid with these information.. like one grid item will consist of title(should be clickable and goes to the url) and the image. is it clear? Commented Jul 2, 2015 at 10:59
  • not really, You have the IDs of trips. you want to pick all the IDs and then show the information for every id on 1 page? Commented Jul 2, 2015 at 11:22

2 Answers 2

1

I still wasn't sure how you would like your layout, but this will get you started. I just showed you how to get a single value and how to get a values from an array.

<?php 

    $ch = curl_init('http://www.expeditiontrips.com/xml/triplist.xml');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $xml_raw = curl_exec($ch);
    curl_close($ch);

    $trips = simplexml_load_string($xml_raw);

    //uncomment next 2 lines, to view the array $trips
    // echo "<pre>";
    // print_r($trips);

    //pick one id, for example the first one. 
    $ch = curl_init('http://www.expeditiontrips.com/xml/trips/' . $trips->trip[0] . '.xml');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $xml_raw = curl_exec($ch);
    curl_close($ch);

    $info = simplexml_load_string($xml_raw);

    //uncomment next 2 lines, to view the array $info
    // echo "<pre>";
    // print_r($info);

    //single value
    echo '<a href="' . $info->url . '">' . $info->url . '</a><br />';

    //multiple valeus in an array
    foreach($info->images->image as $images){
        echo '<img src="' . $images->url . '">';
    }

?>
Sign up to request clarification or add additional context in comments.

3 Comments

thank you soo much!! i'll give it a try today and let u know the out come.
Hi @Jason if this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this.
Sure you gave me the correct direction to to work on. no problem to accept your answer since it is anyway solving part of my question though it is not exactly what i was looking for. But yeah u gave me the correct path. So here im accepting u r answer :)
0

Finally Ended up with the following code. :) Hope it helps someone

    <?php 
        $ch = curl_init('http://www.expeditiontrips.com/xml/triplist.xml');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $xml_raw = curl_exec($ch);
        curl_close($ch);

        $trips = simplexml_load_string($xml_raw);

        $totalTrips = count($trips);
        $perPage = 10;
        $page = isset($_GET['trip']) && ($page = intval($_GET['trip'])) > 0 ?  $page : 1;

        $start = ($page - 1) * $perPage;
        $end = $start + $perPage;

        for ($a=$start; $a<$end; ++$a) { 
            if (isset($trips->trip[$a])) { 
                   $ch = curl_init('http://www.expeditiontrips.com/xml/trips/' . $trips->trip[$a] . '.xml');
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    $xml_raw = curl_exec($ch);
                    curl_close($ch);

                    $info = simplexml_load_string($xml_raw);

                    $ran = array(1,2,3,4,5,6);
                    $randomElement = $ran[array_rand($ran, 1)];

                    echo '<div id="trip-item">';
                    echo '<div class="trip-title">'.$info->name.'</div>';
                    echo '<div class="trip-body">';
                    echo '<div class="col span_3">';
                    echo '<img src="'.$info->images->image[$randomElement]->url.'" />';     
                    echo '</div>';
                    echo '<div class="col span_9 col_last">';
                    echo '<span class="mini-des">'.$info->description.'</span>';
                    echo '<table>';
                    echo '<tr>';
                    echo '<td>Prices: '.$info->prices->price->value.'</td>';
                    echo '<td>Days:</td>';
                    echo '</tr>';
                    echo '<tr>';
                    echo '<td>Ship: '.$info->additions->addition[0]->body.'</td>';
                    echo '<td><a href="">Click here for Departure Dates</a></td>';
                    echo '</tr></table></div></div></div>';

            }
        }

        $pages = ceil($totalTrips / $perPage);

        $low = $page - 3;
        $high = $page + 3;

        echo '<div class="paginator">';

        if ($page > 3) {
            echo '<a href="?trip=1">&laquo;</a>';
        }

        for ($a=$low; $a<=$high; ++$a) {
            if($a > $pages) {
                break;
            }
            if($a > 0 && $a != $page) {
                echo '<a href="?trip=' . $a . '">' . $a . '</a>';
            } else if($a > 0) {
                echo '<span>' . $a . '</span>';
            }
        }

        if ($page != $pages) {
            echo '<a href="?t='.$pages.'">&raquo;</a>';
        }
        echo '</div>';  
    ?>

Comments

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.