PHP Ordination

Asked

Viewed 108 times

5

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

That’s possible?

Code 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>

  • How to run in random order?

  • 1

    shuffle($xml->links) before the for.

  • But in this he will randomize only the link and not divide it whole, correct? And I didn’t understand how you apply.

  • sorry... So this whole block generates a div with a product inside, name, image, link, etc... this is a product and in xml there are many others and I want these products to be in random order and not just use the sequence that is in xml. ?

  • 1

    I could understand that. And what I told you you tested ?

  • I did but the only thing he did was leave random links, I need the content inside the div to be from the same product. I will add XML upstairs with only 2 products.

  • Great. Add that to see what it’s all about.

Show 2 more comments

2 answers

2


Use a Count to count the number of arrays that contain the $xml variable, use this number to mount one for and mount another array with random numbers with limit number of arrays in your $xml, mount it this way:

$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 it If not right, just return here ;)

  • Gave this error on all products. Warning: main(): Cannot add element offer number 3 when only 0 such Elements exist in

  • It shows the line?

  • No, it just shows up repeating it over and over again, just changing number 3 by another number.

  • 1

    Do the following, every XML needs to have a root, I changed the answer, see above. Let’s assume that your xml has a Rais <products></products>

  • It worked now. For some reason the style got a little strange but this gives to tidy up. Thanks!

  • It already stays with you rs.

Show 1 more comment

0

shuffle($xml->offer) before the for.

This function will randomize the object offerwithin the XML.

See if it works like this.

  • It didn’t work either way. shuffle ($offer->Offer); foreach ($xml the $offer ){

Browser other questions tagged

You are not signed in. Login or sign up in order to post.