kostenloser Webspace werbefrei: lima-city


Suche auf lima-city

  • in: Mehrfach-Elemente in RSS item ?

    geschrieben von keyrank

    Danke
    naja, wenn ich in der foreach aber kein Array habe, sondern alles als einzelne Objekte,
    dann bringt mir das auch wenig, eine Rekursion zu machen wenn is_array()
    ... das Array muss ich dann also doch selber anlegen wenn nötig (mehrfach Sub-Elemente in item)

    ich finde die Lösung von incdesing schn ganz gut.

    Wenn item-Element schon existiert, dann als Array of Strings, sonst direkt als String.

    Hier nochmal als Funktion: RSS Items aus simplexml Object in Array ablegen
    <?php 
    // -------------------------------------------------------
    
    function get_rss_items($xmlobject) { 
    	
    	$resdata = array(); 
    	
    	$i=0; 
    	foreach($xmlobject->channel->item as $item) { 
    	
    	foreach($item as $key => $val) { 
    		if (isset($resdata[$i][$key]) && !is_array($resdata[$i][$key])) { 
    			$resdata[$i][$key] = array(); 
    			foreach($item->$key as $subval) { 
    				$resdata[$i][$key][] = (string)$subval; 
    			}
    		}
    		else { 
    			$resdata[$i][$key] = (string)$val; 
    		}
    	
    	}
    	$i++; 
    	}
    	
    	return $resdata; 
    }
    
    // -------------------------------------------------------
    
    
    $sourceurl = 'test1.rss'; 
    
    $xmlobject = simplexml_load_file($sourceurl, 'SimpleXMLElement', LIBXML_NOCDATA); 
    
    $rssitems = get_rss_items($xmlobject); 
    
    print "<pre>\n"; 
    print_r($rssitems); 
    print "</pre>\n"; 
    
    // -------------------------------------------------------
    ?>


    das funktioniert ganz gut. Danke.
  • in: PHP-Function regelmäßig ausführen

    geschrieben von keyrank

    Beispiel mit jQuery

    <html>
    <head>
    <title>TEST</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    
    <script type="text/javascript">
    
    var seconds = 5;    // --- Gesamt Reload-Dauer in Sekunden
    var mdelay = 1000;  // --- Dauer der fadeIn/Out Animation 
    
    var milisec = ((seconds*1000) - (mdelay*2)); 
    
    var auto_refresh = setInterval( function() { 
      $('#loaddiv').fadeIn(mdelay).load('reload1.php').delay(milisec).fadeOut(mdelay).empty(); 
    }, 5000);
    
    
    $(document).ready(function(){ 
      $('#loaddiv').fadeIn(mdelay).load('reload1.php').delay(milisec).fadeOut(mdelay).empty(); 
    }); 
    
    </script>
    
    
    <style type="text/css">
    <!--
    
    body { 
      background: #FFFFFF; 
      color: #000000;
    }
    
    #loaddiv { 
      font-family: "Times New Roman", Times, serif; 
      font-size: 22px; 
      color: #003366;
      text-align: center; 
      margin: 0 auto; 
      padding: 8px; 
      width: 60px; 
      height: 30px; 
      background: #AACCFF; 
      border: 1px solid #33AA00; 
    }
    -->
    </style>
    
    </head>
    
    <body>
    <br>
    
    <div style="text-align: center;">
    <div style="margin: 0 auto; padding:6px; width:90px; height:50px; background:#AAFF99;">
      <div id="loaddiv">&nbsp;</div>
    </div>
    </div>
    
    <br>
    </body>
    </html>


    Datei: reload1.php

    hier die PHP-Funktion rein ... im BSP wird eine Zufalls-Zahl zwischen 0 und 1000 ausgegeben)


    <?php 
    
    $randid = rand(0,1000); 
    
    echo $randid; 
    
    ?>


    nur so als Idee ... mit jQuery
  • in: Mehrfach-Elemente in RSS item ?

    geschrieben von keyrank

    Hallo

    Beim Auslesen einer RSS hatte ich Probleme bei mehrfachen Elementen innerhalb <item>

    vereinfachtes Beispiel:
    ... 
    	<item>
    		<title>Irgend ein Artikel</title>
    		<link>http://example.org/index.php?page=116</link>
    		<pubDate>Fri, 05 Apr 2013 11:51:37 +0000</pubDate>
    		<category><![CDATA[Artikel]]></category>
    		<category><![CDATA[Internet]]></category>
    		<category><![CDATA[Sonstiges]]></category>
    		<category><![CDATA[Lesestoff]]></category>
    		<guid>116</guid>
    		<description><![CDATA[Bla blabla blablabla bla blabla blablabla]]></description>
    	</item>
    ...


    ich hole die RSS Daten per simplexml in ein PHP Object
    und gehe dann mit einer foreach() Schleife durch die Daten

    $sourceurl = 'feed.rss'; 
    
    $xmlobject = simplexml_load_file($sourceurl, 'SimpleXMLElement', LIBXML_NOCDATA);


    DEBUG mit
    print_r($xmlobject);
    egibt folgende Ausgabe:

    SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [version] => 2.0
            )
    
        [channel] => SimpleXMLElement Object
            (
                [title] => Name der Seite
                [link] => http://example.org
                [description] => Berichte und Artikel
                [lastBuildDate] => Fri, 05 Apr 2013 11:51:37 +0000
                [language] => de-DE
                [item] => Array
                    (
                        [0] => SimpleXMLElement Object
                            (
                                [title] => Irgend ein Artikel
                                [link] => http://example.org/index.php?page=116
                                [pubDate] => Fri, 05 Apr 2013 11:51:37 +0000
                                [category] => Array
                                    (
                                        [0] => Artikel
                                        [1] => Internet
                                        [2] => Sonstiges
                                        [3] => Lesestoff
                                    )
    
                                [guid] => 116
                                [description] => Bla blabla blablabla bla blabla blablabla
                            )
    
                        [1] => SimpleXMLElement Object
                            (
                                [title] => Noch so ein Bericht
                                [link] => http://example.org/index.php?page=115
                                [pubDate] => Fri, 04 Apr 2013 16:21:47 +0000
                                [category] => Array
                                    (
                                        [0] => Berichte
                                        [1] => Real-Life
                                        [2] => Allgemein
                                        [3] => Laberzeug
                                    )
    
                                [guid] => 115
                                [description] => Blubber blubb blubbblubb blubb blubber blubb
                            )
    
                    )
    
            )
    
    )


    also ist jeweils das Element [category] ein Array innerhalb $xmlobject->channel->item

    ABER ... wenn ich mit foreach() durchgehe,
    dann kommt jedes Elemet von [category] einzeln als SimpleXMLElement Object
    und nicht (wie erwartet) als ein Array

    $nr = 1; 
    foreach ($xmlobject->channel->item as $item) { 
    print "<br />--------- Item: ".$nr." --------- \n"; 
    	foreach ($item as $key => $val) { 
    		print "<br />".$key." --- ".print_r($val,true)."\n"; 
    	} 
    
    print "<br />\n"; 
    $nr++; 
    }


    macht als Ausgabe:
    --------- Item: 1 ---------
    title --- SimpleXMLElement Object ( [0] => Irgend ein Artikel )
    link --- SimpleXMLElement Object ( [0] => http://example.org/index.php?page=116 )
    pubDate --- SimpleXMLElement Object ( [0] => Fri, 05 Apr 2013 11:51:37 +0000 )
    category --- SimpleXMLElement Object ( [0] => Artikel )
    category --- SimpleXMLElement Object ( [0] => Internet )
    category --- SimpleXMLElement Object ( [0] => Sonstiges )
    category --- SimpleXMLElement Object ( [0] => Lesestoff )
    guid --- SimpleXMLElement Object ( [0] => 116 )
    description --- SimpleXMLElement Object ( [0] => Bla blabla blablabla bla blabla blablabla )
    
    --------- Item: 2 ---------
    title --- SimpleXMLElement Object ( [0] => Noch so ein Bericht )
    link --- SimpleXMLElement Object ( [0] => http://example.org/index.php?page=115 )
    pubDate --- SimpleXMLElement Object ( [0] => Fri, 04 Apr 2013 16:21:47 +0000 )
    category --- SimpleXMLElement Object ( [0] => Berichte )
    category --- SimpleXMLElement Object ( [0] => Real-Life )
    category --- SimpleXMLElement Object ( [0] => Allgemein )
    category --- SimpleXMLElement Object ( [0] => Laberzeug )
    guid --- SimpleXMLElement Object ( [0] => 115 )
    description --- SimpleXMLElement Object ( [0] => Blubber blubb blubbblubb blubb blubber blubb )


    aber im DEBUG mit print_r ist category doch als Array drin ?!?
    also hätte ich auch in der foreach für category ein Array erwartet, ... aber ... Pustekuchen !


    dann habe ich eine if-Bediungng eingeführt, die wenn $key == 'category' daraus ein Array macht
    $i=0; 
    foreach ($xmlobject->channel->item as $item) { 
    
    $nr = ($i+1); 
    print "<br />--------- Item: ".$nr." --------- \n"; 
    
    	foreach ($item as $key => $val) { 
    	
    		if ($key == 'category') { 		
    			$catarr = array(); 
    			foreach($item->category as $catval) {
    				$catarr[] = (string)$catval;
    			}
    			$valdata = $catarr; 
    		}
    		else { 
    			$valdata = (string)$val; 
    		}
    
    		$articles[$i][$key] = $valdata; 
    	}
    
    print "<br />\n"; 
    print "<pre>".print_r($articles[$i],true)."</pre>\n"; 
    print "<br />\n"; 
    
    $i++; 
    }


    das ist schonmal besser, damit bekomme ich die Ausgabe:
    --------- Item: 1 ---------
    
    Array
    (
        [title] => Irgend ein Artikel
        [link] => http://example.org/index.php?page=116
        [pubDate] => Fri, 05 Apr 2013 11:51:37 +0000
        [category] => Array
            (
                [0] => Artikel
                [1] => Internet
                [2] => Sonstiges
                [3] => Lesestoff
            )
    
        [guid] => 116
        [description] => Bla blabla blablabla bla blabla blablabla
    )
    
    
    
    --------- Item: 2 ---------
    
    Array
    (
        [title] => Noch so ein Bericht
        [link] => http://example.org/index.php?page=115
        [pubDate] => Fri, 04 Apr 2013 16:21:47 +0000
        [category] => Array
            (
                [0] => Berichte
                [1] => Real-Life
                [2] => Allgemein
                [3] => Laberzeug
            )
    
        [guid] => 115
        [description] => Blubber blubb blubbblubb blubb blubber blubb
    )


    gut, so funtioniert das für das "bekannte" Sub-Element = [category]
    Aber es könnte ja auch ein anderes Sub-Element mehrfach vorkommen
    dessen $key Wert (Element-Name) man nicht vorher kennt ...

    .... damit jetzt zur eigentlichen Frage:

    Wie kann ich mehrfach vorkommende (gleichnamige) Sub-Elemente von <item> im $xmlobject
    immer als Array bekommen ... gibt es da eine bessere Lösung als meine if-Bedingung

    Warum bekomme ich mit print_r bei [category] die Daten als Array
    aber bei Durchlauf mit foreach() dann wieder einzelne Objekte ???

    ... spinnt simplexml ?
    ... spinnt print_r ?
    ... spinnt foreach ?

    ... oder spinn ich ?!?
    :)
  • in: Verschieben von Div innerhalb einer Tabelle

    geschrieben von keyrank

    nimm nicht 2x die gleiche DIV id ... jedes DIV sollte eine eigene (eindeutige) id haben
    eine CSS id "darf" man im HTML nur einmal anwenden, sonst für mehrfach numm Klasse


    BSP ... ganz grob als Test
    <html>
    <head>
    <title>Test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    <style type="text/css">
    <!--
    td { 
      margin: 0;
      padding:0; 
    }
    
    #div1 { 
      margin: 0;
      padding:0; 
      float:left;
      width:296px; 
      background:#CCFFAA; 
      border: 2px solid #CC33FF; 
    }
    
    #div2 { 
      margin: 0;
      padding:0; 
      float:left;
      width:296px; 
      background:#AAFFCC; 
      border: 2px solid #CC33FF; 
    }
    -->
    </style>
    
    </head>
    
    <body bgcolor="#FFFFFF" text="#000000">
    
    <br>
    <br>
    
    <table border="0" cellspacing="0" cellpadding="0" bgcolor="#CCCCCC" width="700">
      <tr>
        <td width="100">x</td>
        <td width="600">y</td>
      </tr>
      <tr>
        <td width="100">z</td>
        <td width="600">
    	
    	<div id="div1">
    	  <p>Erste Box</p>
    	  <p>Bla bla bla bla</p>
    	</div>
    	
    	<div id="div1">
    	  <p>Erste Box</p>
    	  <p>Bla bla bla bla</p>
    	</div>
    	
    	</td>
      </tr>
    </table>
    
    <br>
    <br>
    
    </body>
    </html>



    oder mit Klasse anstatt ID

    CSS:
    .divbox { 
      margin: 0;
      padding:0; 
      float:left;
      width:296px; 
      background:#CCFFAA; 
      border: 2px solid #CC33FF; 
    }


    und dann im HTML
    ...
    	<div class="divbox">
    	  <p>Erste Box</p>
    	  <p>Bla bla bla bla</p>
    	</div>
    	
    	<div class="divbox">
    	  <p>Erste Box</p>
    	  <p>Bla bla bla bla</p>
    	</div>
    ...


    zum Abstand ... wenn Du je DIV einen border:2px machst,
    dann musst Du diese 2 mal 2px (links und rechts) von der Breite abziegen

    also das td Element hat z.B. eine Breite von: 600px
    dann die zwei DIVs jeweils (300-4) = 296 Pixel Breite
    weil ja die Rahmen-Breite noch dazu kommt ...

    zumindest ist das in meinem Browser (FF) so
    kann je Browser auch anders sein ...
    ... ich glaub mit Außen-/ Innen- Abstand (margin / padding) ist im IE irgendwas anders ... :(

    und Nebeneinander geht mit CSS-Befehl float: left;

    PS: anstatt Tabelle ist es evtl. besser auch das "drum-herum" mit DIV zu machen

Login zum Webhosting ohne Werbung!