Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 18, 2011 @ 16:32
    Fuji Kusaka
    0

    Counting number of media in media folder

    Can someone point out how to count the number of Items present in a folder in media section for a paging to work?

     

  • Tom Fulton 2030 posts 4996 karma points c-trib
    Oct 18, 2011 @ 16:47
    Tom Fulton
    0

    Hi Fuji,

    You can use the count() function to count the number of items, ex:

      <xsl:variable name="myFolder" select="umbraco.library:GetMedia(1117,true())" />
      Total items in folder:  <xsl:value-of select="count($myFolder/* [@id])"/>

    Note, I added [@id] to the xpath to make sure it only finds nodes that have an id attribute, otherwise I think it will pick up properties from your folder as well -- since there is no @isDoc in media.

    -Tom

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 18, 2011 @ 17:07
    Fuji Kusaka
    0

    Hi Tom,

    This is how i proceeded am using  a mediaCurrent here to pick the media folder from a template. Here the paging is showing only 1  instead.

     <xsl:variable name="mediaFolder" select="/macro/imgFiles"/>    
    <xsl:variable name="itemsToDisplay" select="10"/>  
        
     <xsl:variable name="numberOfItems" select="count($mediaFolder/*[@id])"/>
    <xsl:variable name="jump" select="$itemsToDisplay * $pageNumber" />
        

        
        
    <xsl:variable name="pageNumber">
      <xsl:choose>
        <xsl:when test="umbraco.library:RequestQueryString('page') = ''">
          <xsl:value-of select="1"/>
        xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="umbraco.library:RequestQueryString('page')"/>
        xsl:otherwise>
      xsl:choose>
    xsl:variable>    

    <xsl:template match="/">
      
      <ul>
       <xsl:if test="$mediaFolder">     
        
         <xsl:variable name="mediaItems" select="umbraco.library:GetMedia($mediaFolder/*/@id, 'true')" />   
         <xsl:for-each select="$mediaItems/Image">
            <xsl:if test="position() <= $jump and position() >= ($jump - $itemsToDisplay)">
             <li>
              <href="{concat(substring-before(umbracoFile,'.'),'_slideshow.jpg')}">
                 <img src="{concat(substring-before(umbracoFile,'.'),'_thumbnail.jpg')}" />           
           a>
         li>  
              xsl:if>  
        xsl:for-each>
      
       xsl:if>  
    ul  
      
        <ul id="paging">    
          <xsl:if test="$pageNumber > 1">
          <li>
              <href="?page={$pageNumber -1}#page">previousa>
         li>
        xsl:if>    
              <xsl:call-template name="for.loop">
              <xsl:with-param name="i">1xsl:with-param>
               <xsl:with-param name="count" select="ceiling($numberOfItems div $itemsToDisplay)">xsl:with-param>
            xsl:call-template>  
        
          <xsl:if test="(($pageNumber) * $itemsToDisplay) < ($numberOfItems)">
               <li>
                      <href="?page={$pageNumber+1}#
    page">nexta>
            li>
          xsl:if
          ul>
      
      
    xsl:template>   
        
    <xsl:template name="for.loop">     
                    <xsl:param name="i"/>
                    <xsl:param name="count"/>              
                    <xsl:if test="$i <=$count">                     
                            <xsl:if test="$pageNumber!= $i">
                              <li>
                                    <href="?page={$i}#
    page"><xsl:value-of select="$i" />a>
                              li>
                            xsl:if>

                            <xsl:if test="$pageNumber = $i">
                              <li>
                                    <xsl:value-of select="$i"/>
                              li>
                            xsl:if>
                          
                            <xsl:call-template name="for.loop">
                                    <xsl:with-param name="i" select="$i + 1" />
                                    <xsl:with-param name="count" select="$count">
                                    xsl:with-param>
                            xsl:call-template>                  
                    xsl:if>
       xsl:template>
  • Tom Fulton 2030 posts 4996 karma points c-trib
    Oct 18, 2011 @ 17:21
    Tom Fulton
    0

    The problem is mediaCurrent doesn't include the child items, only the current selected media.

    To fix you'll need to use GetMedia to get a reference to that media item and include its children:

    <xsl:if test="$mediaFolder">
     <xsl:variable name="mediaItems" select="umbraco.library:GetMedia($mediaFolder/*/@id, true())" /> 
     <xsl:variable name="numberOfItems" select="count($mediaItems/*[@id])"/>
      ...
    </xsl:if>

    Note I moved your mediaItems variable up so you can re-use it for both calls.  Also you should change 'true' to true()

    -Tom

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 18, 2011 @ 17:38
    Fuji Kusaka
    0

    Still cant get the paging to work though. I must have missed something here...

    <xsl:variable name="mediaFolder" select="/macro/imgFiles"/>    
    <xsl:variable name="itemsToDisplay" select="2"/>  
        
    <xsl:variable name="numberOfItems" select="count($mediaFolder/*[@id])"/>
    <xsl:variable name="jump" select="$itemsToDisplay * $pageNumber" />
        
        
    <xsl:variable name="pageNumber">
      <xsl:choose>
        <xsl:when test="umbraco.library:RequestQueryString('page') = ''">
          <xsl:value-of select="1"/>
        xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="umbraco.library:RequestQueryString('page')"/>
        xsl:otherwise>
      xsl:choose>
    xsl:variable>    

    <xsl:template match="/">
      
      <ul>
       <xsl:if test="$mediaFolder">     
        
         <xsl:variable name="mediaItems" select="umbraco.library:GetMedia($mediaFolder/*/@id, true())" />   
         <xsl:variable name="numImg" select="count($mediaItems/*[@id])"/>
         <xsl:for-each select="$mediaItems/Image">      
            <xsl:if test="position() = ($jump - $itemsToDisplay)">
             <li>
              <href="{concat(substring-before(umbracoFile,'.'),'_slideshow.jpg')}">  
                 <img src="{concat(substring-before(umbracoFile,'.'),'_thumbnail.jpg')}" />
           a>
        li>  
              xsl:if>  
        xsl:for-each>  
       xsl:if>  
    ul
      
      
        <ul id="paging">    
          <xsl:if test="$pageNumber > 1">
          <li>
              <href="?page={$pageNumber -1}#page">previousa>
         li>
        xsl:if>    
              <xsl:call-template name="for.loop">
              <xsl:with-param name="i">1xsl:with-param>
               <xsl:with-param name="count" select="ceiling($numberOfItems div $itemsToDisplay)">xsl:with-param>
            xsl:call-template>  
        
          <xsl:if test="(($pageNumber) * $itemsToDisplay) < ($numberOfItems)">
               <li>
                      <href="?page={$pageNumber+1}#page">nexta>
            li>
          xsl:if
          ul>
      
      
    xsl:template>   
        
    <xsl:template name="for.loop">     
                    <xsl:param name="i"/>
                    <xsl:param name="count"/>              
                    <xsl:if test="$i >                     
                            <xsl:if test="$pageNumber!= $i">
                              <li>
                                    <href="?page={$i}#
    page"><xsl:value-of select="$i" />a>
                              li>
                            xsl:if>

                            <xsl:if test="$pageNumber = $i">
                              <li>
                                    <xsl:value-of select="$i"/>
                              li>
                            xsl:if>
                          
                            <xsl:call-template name="for.loop">
                                    <xsl:with-param name="i" select="$i + 1" />
                                    <xsl:with-param name="count" select="$count">
                                    xsl:with-param>
                            xsl:call-template>                  
                    xsl:if>
       xsl:template>

     

  • Tom Fulton 2030 posts 4996 karma points c-trib
    Oct 18, 2011 @ 17:44
    Tom Fulton
    0

    I see you added $numImg but I think your code is still referencing $numberOfItems in the for.loop template call.

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 18, 2011 @ 17:49
    Fuji Kusaka
    0

    Yup but still not doing it...

  • Tom Fulton 2030 posts 4996 karma points c-trib
    Oct 18, 2011 @ 18:06
    Tom Fulton
    0

    Paste your updated code, I'll see if I can take a look

  • Tom Fulton 2030 posts 4996 karma points c-trib
    Oct 18, 2011 @ 18:20
    Tom Fulton
    1

    Try this instead.  numImg was being declared outside the scope it was referenced in:

    <xsl:variable name="mediaFolder" select="/macro/imgFiles"/>
    <xsl:variable name="itemsToDisplay" select="10"/>  
    <xsl:variable name="jump" select="$itemsToDisplay * $pageNumber" />
     
    <!-- Paging -->
    <xsl:variable name="pageNumber">
      <xsl:choose>
        <xsl:when test="umbraco.library:RequestQueryString('page') = ''">
          <xsl:value-of select="1"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="umbraco.library:RequestQueryString('page')"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>    

    <xsl:template match="/">
      <xsl:if test="$mediaFolder">   
        <xsl:variable name="mediaItems" select="umbraco.library:GetMedia($mediaFolder/*/@id, 'true')" />    
        <xsl:variable name="numImg" select="count($mediaItems/*[@id])"/>
         
        <ul id="luxGallery">
          <xsl:for-each select="$mediaItems/Image">
            <xsl:if test="position() &lt;= $jump and position() &gt;= ($jump - $itemsToDisplay)">
              <li>
                <href="{concat(substring-before(umbracoFile,'.'),'_slideshow.jpg')}">
                  <img src="{concat(substring-before(umbracoFile,'.'),'_thumbnail.jpg')}" />           
                </a>
              </li>  
            </xsl:if>  
          </xsl:for-each>
        </ul>   
      
        <ul id="paging">    
          <xsl:if test="$pageNumber &gt; 1">
            <li>
              <href="?page={$pageNumber -1}#page">previous</a>
            </li>
          </xsl:if>    
          <xsl:call-template name="for.loop">
            <xsl:with-param name="i">1</xsl:with-param>
            <xsl:with-param name="count" select="ceiling($numImg div $itemsToDisplay)"></xsl:with-param>
          </xsl:call-template>  
        
          <xsl:if test="(($pageNumber) * $itemsToDisplay) &lt; ($numImg)">
            <li>
              <a href="?page={$pageNumber+1}#page">next</a>
            </li>
          </xsl:if
        </ul>
        
      </xsl:if>  
      </xsl:template>   
        
    <xsl:template name="for.loop">     
      <xsl:param name="i"/>
      <xsl:param name="count"/>              
      <xsl:if test="$i &lt;=$count">                     
        <xsl:if test="$pageNumber!= $i">
          <li>
            <href="?page={$i}#page"><xsl:value-of select="$i" /></a>
          </li>
        </xsl:if>

        <xsl:if test="$pageNumber = $i">
          <li>
            <xsl:value-of select="$i"/>
          </li>
        </xsl:if>
              
        <xsl:call-template name="for.loop">
          <xsl:with-param name="i" select="$i + 1" />
          <xsl:with-param name="count" select="$count">
          </xsl:with-param>
        </xsl:call-template>                  
      </xsl:if>
    </xsl:template>
  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 18, 2011 @ 19:04
    Fuji Kusaka
    0

    Hey Tom, Yes got it working, i had a miss of everything everywhere by trying to get it working.

    Thanks for your Help.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies