Im am trying to count the nodes in my XML document so I can specify in the loop where I dont want <hr>'s i.e. at the end of the page.

Please tell me why this is not counting the nodes under blog.

foreach(file_counter("blog/$current_year/$months") as $key=>$file)
							{
								$doc = new DOMDocument(); 
								$doc->load($file); 								  
$dataset = $doc->getElementsByTagName("blog"); $node_count = $dataset->length; $i = 0; foreach($dataset as $row) { $title = $row->getElementsByTagName("title")->item(0)->nodeValue; $date = $row->getElementsByTagName("date")->item(0)->nodeValue; $body = $row->getElementsByTagName("body")->item(0)->nodeValue; $i++; echo "$title<br>"; echo "$date<br>"; echo "$body<br><br>"; echo $node_count; } }

    Well, so much is missing that this can only be a guess; but it might have something to do with the fact you never use $i, and you repeatedly echo the same value of $node_count.

      Surely I only need this but with the right element?

      $dataset = $doc->getElementsByTagName("blog"); 
      $node_count = $dataset->length; 
      

      If not could you point me to how to do it or tell me.

      Cheers

        S3NTIN3L;10981757 wrote:
        $dataset = $doc->getElementsByTagName("blog"); 
        $node_count = $dataset->length; 
        

        This counts the number of "blog" elements in the document. Since I don't know what structure your document has, I can't really say what you need to do.

        But if you for example have

        <blog>
        	<post>
        		<date />
        		<title />
        		<body />
        	</post>
        	...
        	<post>
        		<date />
        		<title />
        		<body />
        	</post>
        </blog>
        

        You could do

        # assuming there is just one blog element
        $blogs = $doc->getElementsByTagName("blog");
        $blog = $blogs->item(0);
        
        $posts = $blog->getElementsByTagName("post");
        $count = $posts->length;
        

        With the above, you might also getElementsByTagName('title') since there is one title element per post element, so the count will be the same. But if not all posts have one title (some have 0, others have many) you can't rely on this.

        However, you should not use HR in my opinion since this is far easier to control with css. If you change your output to

        echo '<div class="post">';
        echo "$title<br>";
        echo "$date<br>";
        echo "$body<br><br>";
        echo '</div>';
        

        And add the proper CSS styling, you will get the same effect

        div.post + div.post
        {
        	border-top: 1px solid black;
        }
        

          Thanks for the reply.

          Ive decided to structure the XML your way and I use this (just echoing first before I add spacers):

          $count = $tags->length; 
          
          for($i = 0; $i < $count; $i++) 
          {
          	$tag_echo = $tags->item(0)->nodeValue;
          	echo "<a href=''>$tag_echo</a>,&nbsp;";
          }
          
          
          

          Problem at the moment is their not individual links, their different tags but one big link. How would I remedy this?

          I use <hr> with custom CSS values - an image to space elements output.

            item(0) always refers to item(0). You probably want item($i)

              I did actually try that before you suggested it, to no avail though.

              The code that gets the XML has to go deeper i.e. down another node as its only counting one therefore only making one loop iteration.

              The XML grabbing code in the loop just gets the whole lot of nested nodes in one therefore $i wont work.

              $count = $tags->length; 
              
              for($i = 0; $i < $count; $i++) 
              { 
                  $tag_echo = $tags->item(0)->nodeValue; 
                  echo "<a href=''>$tag_echo</a>,&nbsp;"; 
              }
              

              The good news is I got it working:

              $get_tag = $row->getElementsByTagName("tag");								
              $count = $get_tag->length; 
              //echo $count;
              
              for($i = 0; $i < $count; $i++) 
              {								
              	$tag_echo = $get_tag->item($i)->nodeValue;;
              
              echo "<a href=''>$tag_echo</a>";
              
              if($i != ($count - 1))
              {
              	echo ",&nbsp;";
              }
              }
              

              Cheers for the help people

                S3NTIN3L wrote:

                I did actually try that before you suggested it, to no avail though.

                The good news is I got it working:

                Ironically, by doing what was suggested.

                  Write a Reply...