Display random elements from XML in PHP

2019-06-07 19:16发布

I think my problem is simple but I could not find anything that can match me. I have a PHP file that enables me to display information from an XML file. I would like to display from there 3 random elements from this file.

Here my XML

<?xml version="1.0" encoding="UTF-8"?>
<items>
    <item>
        <id>1</id>
        <thumb>images/portfolio/thumb/website1.jpg</thumb>
        <titre>website1</titre>
        <description>Lorem ipsum.</description>
        <image>images/portfolio/website1.jpg</image>
        <category>Cat 1</category>
        <link>portfolio.html</link>
    </item>
    <item>
        <id>2</id>
        <thumb>images/portfolio/thumb/website2.jpg</thumb>
        <titre>website2</titre>
        <description>Lorem ipsum.</description>
        <image>images/portfolio/website2.jpg</image>
        <category>Cat 2</category>
        <link>portfolio.html</link>
    </item>
    <item>
        <id>3</id>
        <thumb>images/portfolio/thumb/website3.jpg</thumb>
        <titre>website3</titre>
        <description>Lorem ipsum.</description>
        <image>images/portfolio/website3.jpg</image>
        <category>Cat 3</category>
        <link>portfolio.html</link>
    </item>
    <item>
        <id>4</id>
        <thumb>images/portfolio/thumb/website4.jpg</thumb>
        <titre>website4</titre>
        <description>Lorem ipsum.</description>
        <image>images/portfolio/website4.jpg</image>
        <category>Cat 4</category>
        <link>portfolio.html</link>
    </item>
    <item>
        <id>5</id>
        <thumb>images/portfolio/thumb/website5.jpg</thumb>
        <titre>website5</titre>
        <description>Lorem ipsum.</description>
        <image>images/portfolio/website5.jpg</image>
        <category>Cat 5</category>
        <link>portfolio.html</link>
    </item>
    <item>
        <id>6</id>
        <thumb>images/portfolio/thumb/website6.jpg</thumb>
        <titre>website6</titre>
        <description>Lorem ipsum.</description>
        <image>images/portfolio/website6.jpg</image>
        <category>Cat 6</category>
        <link>portfolio.html</link>
    </item>
</items>

There, my PHP for display my XML elements.

<?php
    $xmldata = simplexml_load_file('portfolio.xml');
    foreach($xmldata->item as $item) {
            echo '<li class="col4 item ' . $item->category . '">';
            echo '<img src="' . $item->thumb . '" alt="">';
            echo '<div class="col4 item-info">';
            echo '<h3 class="title"><a href="' . $item->image . '" data-rel="prettyPhoto['. $item->category .']">' . $item->titre . '</a></h3>';
            echo '</div>';
            echo '<div class="item-info-overlay">';
            echo '<div>';
            echo '<h4>' . $item->category . '</h4>';
            echo '<p>' . $item->description . '</p>';
            echo '<a href="' . $item->image . '" class="preview" data-rel="prettyPhoto['. $item->category .']">preview</a>';
            echo '</div>';
            echo '</div>';
            echo '</li>';
            }

    ?>

Thanks for your help.

标签: php xml random
2条回答
The star\"
2楼-- · 2019-06-07 19:31

Here we go:

$xml = simplexml_load_string($x); // assume XML in $x
$random = array_rand($xml->xpath("item"),3);
foreach ($random as $n) echo $xml->item[$n]->titre. "<br />";

see it working: http://codepad.viper-7.com/WAL1EB

查看更多
走好不送
3楼-- · 2019-06-07 19:33

array_rand comes in useful.

Try something like this:

$xmldata = simplexml_load_file('portfolio.xml');
$random = array_rand($xmldata->xpath('item'), 3);
foreach ($random as $key) {
    $item = $xmldata->item[$key];
    echo '<li class="col4 item ' . $item->category . '">';
    echo '<img src="' . $item->thumb . '" alt="">';
    echo '<div class="col4 item-info">';
    echo '<h3 class="title"><a href="' . $item->image . '" data-rel="prettyPhoto['. $item->category .']">' . $item->titre . '</a></h3>';
    echo '</div>';
    echo '<div class="item-info-overlay">';
    echo '<div>';
    echo '<h4>' . $item->category . '</h4>';
    echo '<p>' . $item->description . '</p>';
    echo '<a href="' . $item->image . '" class="preview" data-rel="prettyPhoto['. $item->category .']">preview</a>';
    echo '</div>';
    echo '</div>';
    echo '</li>';
}

Untested

Make sure you read up on the warnings array_rand can produce and use count() to make sure there are enough items in your xml.

查看更多
登录 后发表回答