2014-10-22 9 views
0

나는 아래의 예와 같은 항목의 수와 XML 파일을 만든 :랜드() 셔플 XML 출력

<?xml version="1.0" encoding="UTF-8"?> 
<feed xmlns="http://www.w3.org/2005/Atom"> 
    <id>http://www.example.com/index.php?pagina=video-categories</id> 
    <title><![CDATA[Example.com Categories RSS]]></title> 
    <author> 
    <name>Example.com</name> 
    <email>[email protected]</email> 
    </author> 
    <updated></updated> 
    <link rel="alternate" href="http://www.example.com/categories"/> 
    <subtitle><![CDATA[HQ example videos in any category]]></subtitle> 
    <rights>Copyrights reserved. Feel free to use the embed function.</rights> 
    <item> 
    <categoryname><![CDATA[100 Latest Videos]]></categoryname> 
    <shortcategoryname><![CDATA[100 New Sex Clips]]></shortcategoryname> 
    <categoryurl>http://www.example.com/1.html</categoryurl> 
    <categoryimage>http://www.example.com/12347.jpg</categoryimage> 
    </item> 
    <item> 
    <categoryname><![CDATA[100 Latest Videos]]></categoryname> 
    <shortcategoryname><![CDATA[100 New Sex Clips]]></shortcategoryname> 
    <categoryurl>http://www.example.com/2.html</categoryurl> 
    <categoryimage>http://www.example.com/12346.jpg</categoryimage> 
    </item> 
    <item> 
    <categoryname><![CDATA[100 Latest Videos]]></categoryname> 
    <shortcategoryname><![CDATA[100 New Sex Clips]]></shortcategoryname> 
    <categoryurl>http://www.example.com/3.html</categoryurl> 
    <categoryimage>http://www.example.com/12345.jpg</categoryimage> 
    </item> 
    ... and more items ... 
    </feed> 

I XML 파일에서 출력으로 모든 링크에 다음 코드를 가지고

 <?php 
    $html = ""; // var full of emptyness 
    $url = "http://www.example.com/categories.xml"; 
    $xml = simplexml_load_file($url); 
    for($i = 0; $i < 35; $i++){ // Number of category here, I use a lower number at this moment... 
    $categoryname = $xml->item[$i]->categoryname; 
    $shortcategoryname = $xml->item[$i]->shortcategoryname; 
    $categoryurl = $xml->item[$i]->categoryurl; 
    $html .= '<a class="purplewidebutton" href="' . $categoryurl . '" title="' . $categoryname . '">' . $shortcategoryname . '</a>'; 
    } 
    echo $html; 
    ?> 

6 개의 링크 만 표시하고 XML 피드에서 위치 정보를 표시하고 싶습니다. xml 항목에서 임의의 링크를 갖고 싶습니다. 어떤 항목을 추가하거나 변경해야합니까? rand() 또는 shuffle()을 6 개의 무작위 링크에 에코하려면 사용해야합니까?

PHP 코드가 좀 링크를 반향이 순간에 사용하고 무엇을하지만 그렇지 않은 임의의 ...

+0

보유하고있는 PHP는 무엇입니까? – AbraCadaver

+0

내 질문에 표시된 PHP 코드를 사용하지만 xml 파일 URL은 실제 URL이 아니라 예제 URL입니다. – jagb

+0

죄송합니다. 스크롤하지 않았습니다. – AbraCadaver

답변

0

$xml->item 때문에, shuffle 배열입니다 그 다음 첫 번째 6를 얻을, 뭔가 같은 :

shuffle($xml->item); 

foreach(array_slice($xml->item, 0, 6) as $item) { 
    $categoryname = $item->categoryname; 
    $shortcategoryname = $item->shortcategoryname; 
    $categoryurl = $item->categoryurl; 
    $html .= '<a class="purplewidebutton" href="' . $categoryurl . '" title="' . $categoryname . '">' . $shortcategoryname . '</a>'; 
} 
echo $html; 

또는 현재 코드와 함께 당신은 루프에서 배열에 HTML을 저장할 수 :

$html[] = '<a class="purplewidebutton" href="' . $categoryurl . '" title="' . $categoryname . '">' . $shortcategoryname . '</a>'; 

그리고 afte 루프 :

shuffle($html); 
echo implode("\n", array_slice($html, 0, 6)); 
+0

대단히 고맙습니다. 당신의 대답에 2 번째 옵션을 사용할 것이고, 당신의 대답에 다시 한번 감사드립니다! – jagb

+0

'foreach'는 XML에 항목을 추가하면 루프를 변경할 필요가 없듯이 쉽고 다양합니다. – AbraCadaver

+0

코드 사용 :' item [$ i] -> categoryname;에 대한 ($ i = 0; $ i <35; $ i ++) $ shortcategoryname = $ xml-> item [$ i] -> shortcategoryname; $ categoryurl = $ xml-> item [$ i] -> categoryurl; $ html [] = '' . $shortcategoryname . ''; } shuffle ($ html); echo implode ("\ n", array_slice ($ html, 0, 6)); ?>'xml 피드에 카테고리를 제거/추가하는 경우에만 35 숫자를 변경해야합니다. 캐시를 사용하는 페이지에서 페이지를 하루에 4 번만 실행합니다. – jagb