PHP Ordering

5

I have a PHP code that pulls an XML and plays on the screen however I would like it to run in random order and not the order that is in XML.

Is this possible?

Code that I'm using.

PHP

$xml = simplexml_load_file('produtos.xml');
foreach ($xml as $oferta){
$a = "<div class='produto'><a href=" . $oferta->links->link["url"]. ">". $oferta->offerName. "</a><br>";
$a .= "" . $oferta->offerShortName. "<br>";
$a .= "<img src=" . $oferta->thumbnail["url"]. " width='80%;'><br>";
$a .= "R$" . $oferta->price->value.  "<br>";
$a .= "<a href=" . $oferta->links->link["url"]. ">comprar</a><br>"; 
$a .= "<a href=" . $oferta->links->link["url"]. "><img src=" . $oferta->seller->thumbnail["url"]. "></a><br></div>";
echo $a;

XML

<offer categoryId="5" id="149417086" productId="599409">
    <offerName>DVD Portátil Tectoy Bob Esponja DVT - P4000 ( 117700377 )</offerName>
    <offerShortName>Dvd Portátil </offerShortName>
    <links>
        <link url="http://links.lomadee.com/ls/of/Y249Z1F3RjttZHNyYz0zMzU4MzI4NTtlbXBfaWQ9ODE7bWRhcHA9NTc5ODtwcm9kX2lkPTE0OTQxNzA4Njt4bGs9aHR0cDovL3d3dy5hbWVyaWNhbmFzLmNvbS5ici9wcm9kdXRvLzExNzcwMDM3Ny9kdmQtcG9ydGF0aWwtdGVjdG95LWJvYi1lc3BvbmphLWR2dC1wNDAwMD87Y291bnRyeT1CUjtodD02YzRlZTg1ZjcyNjhmYzVjODdmOWE3NTJjZDZhNDE2NDttZGR0bj0w.html" type="offer"/>
    </links>
    <thumbnail url="http://iacom.s8.com.br/produtos/01/00/item/117700/3/117700377_1GG.jpg"/>
    <price>
        <currency abbreviation="BRL"/>
        <value>296.88</value>
    </price>
    <seller id="81" isTrustedStore="true" pagamentoDigital="false" advertiserId="0" oneClickBuy="false" oneClickBuyValue="0" cpcDifferentiated="false">
        <sellerName>Americanas.com</sellerName>
        <thumbnail url="http://imagem.buscape.com.br/vitrine/logo81.gif"/>
        <links>
            <link url="http://www.americanas.com.br" type="seller"/>
        </links>
        <contacts>
            <contact value="4003-4848" label="Atendimento"/>
            <contact value="0300-7893200" label="Televendas"/>
            <contact value="4003-1000" label="Televendas"/>
        </contacts>
        <rating>
            <userAverageRating>
                <numComments>10773</numComments>
                <rating>0.0</rating>
            </userAverageRating>
            <eBitRating>
                <numComments>10773</numComments>
                <rating>Diamante</rating>
                <ratingNew>e-bit Excelente</ratingNew>
                <ratingId>40</ratingId>
                <ebitId>568</ebitId>
            </eBitRating>
        </rating>
    </seller>
</offer>
<offer categoryId="10" id="120912411">
    <offerName>Caixa de Som Portátil Azul SSMN 101 - HoMedics ( 110969373 )</offerName>
    <offerShortName>Caixa De Som </offerShortName>
    <links>
        <link url="http://links.lomadee.com/ls/of/Y249N01WSTttZHNyYz0zMzU4MzI4NTtlbXBfaWQ9ODE7bWRhcHA9NTc5ODtwcm9kX2lkPTEyMDkxMjQxMTt4bGs9aHR0cDovL3d3dy5hbWVyaWNhbmFzLmNvbS5ici9wcm9kdXRvLzExMDk2OTM3My9jYWl4YS1kZS1zb20tcG9ydGF0aWwtYXp1bC1zc21uLTEwMS1ob21lZGljcz9lcGFyPWJ1c2NhcGUmb3BuPVlZTktaQjtjb3VudHJ5PUJSO2h0PWQ1YzNlYzRkNjY2MTgzZTA1NmVlMzdhYmJmZDA4Mzc2O21kZHRuPTA-.html" type="offer"/>
    </links>
    <thumbnail url="http://thumbs.buscape.com.br/T100x100/__2.81-734fa1b.jpg"/>
    <price>
        <currency abbreviation="BRL"/>
        <value>44.91</value>
    </price>
    <seller id="81" isTrustedStore="true" pagamentoDigital="false" advertiserId="0" oneClickBuy="false" oneClickBuyValue="0" cpcDifferentiated="false">
        <sellerName>Americanas.com</sellerName>
        <thumbnail url="http://imagem.buscape.com.br/vitrine/logo81.gif"/>
        <links>
            <link url="http://www.americanas.com.br" type="seller"/>
        </links>
        <contacts>
            <contact value="4003-4848" label="Atendimento"/>
            <contact value="0300-7893200" label="Televendas"/>
            <contact value="4003-1000" label="Televendas"/>
        </contacts>
        <rating>
            <userAverageRating>
                <numComments>10773</numComments>
                <rating>0.0</rating>
            </userAverageRating>
            <eBitRating>
                <numComments>10773</numComments>
                <rating>Diamante</rating>
                <ratingNew>e-bit Excelente</ratingNew>
                <ratingId>40</ratingId>
                <ebitId>568</ebitId>
            </eBitRating>
        </rating>
    </seller>
</offer>

    
asked by anonymous 02.10.2015 / 16:06

2 answers

2

Use a count to count the amount of arrays that contain the $ xml variable, use that number to build a for, and mount another random numbered array with a limit to the number of arrays in your $ xml, / p>

$qtd_array = count($xml->offer) - 1;    
for($i= 0; $i<= $qtd_array; $i++){
    $num_alea = rand(0, $qtd_array);
    if($i >= 1){
        while(in_array($num_alea, $num_aleatorio)){
             $num_alea = rand(0, $qtd_array);
        }
    }
$num_aleatorio[$i] = $num_alea;

$a = "<div class='produto'><a href=" . $xml->offer[$num_aleatorio[$i]]->links->link["url"]. ">". 
$xml->offer[$num_aleatorio[$i]]->offerName. "</a><br>";
$a .= "" . $xml->offer[$num_aleatorio[$i]]->offerShortName. "<br>";
$a .= "<img src=" . $xml->offer[$num_aleatorio[$i]]->thumbnail["url"]. " width='80%;'><br>";
$a .= "R$" . $xml->offer[$num_aleatorio[$i]]->price->value.  "<br>";
$a .= "<a href=" .$xml->offer[$num_aleatorio[$i]]->links->link["url"]. ">comprar</a><br>"; 
$a .= "<a href=" .$xml->offer[$num_aleatorio[$i]]->links->link["url"]. "><img src=" . $xml->offer[$num_aleatorio[$i]]->seller->thumbnail["url"]. "></a><br></div>";
echo $a;

Try this If you are not sure, just return here;)

    
02.10.2015 / 16:56
0

shuffle($xml->offer) before for .

This function will randomize the object offer within XML .

See if it works like this.

    
02.10.2015 / 16:58