We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12372
    • 44 Posts
    I know a bit of PHP, I can piece together some simple if/else stuff etc. But ultimately, I don’t know much!

    I’m wanting to loop through a number of template variables, churning out their output for each number.

    So for example I have TVs named News 1 to News 15 - and I want to ouput each of them in turn as long as it’s not empty (as there’s not always 15 news item).

    So I wanted to basically do this:

    $newsitem = "[[*News $i]]";

    The looping is working if I manually put in a number say [[*News 1]] - but I can’t seem to use $i in this context.

    Is there a way that it can be achieved?

    Thanks
      • 4172
      • 5,888 Posts
      instaed of using 15 TVs for news-items you could create subresources and use getResources to fetch them.
      Or you can use MIGX, where you can add many records into one TV.
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 12372
        • 44 Posts
        Thanks for the fast reply.

        Subresources? I’m not aware of them, and can’t sem to find anything about them, do you have a link?

        MIGX I’m looking at now. I’m slightly wary of installing a separate package because it creates a reliance on someone supporting it etc. If I can do it ’home grown’ then that would be my ideal route. But I will read into MIGX further.

        Thanks
          • 12236
          • 48 Posts
          Digital Butter - MODX Premiere Partners Reply #4, 15 years, 3 months ago
          Definitely recommend MIGX, perfect for this kind of thing and it’s solid.
            • 12372
            • 44 Posts
            Actually what I was doing was actually working, I was focusing int he wrong spot.

            My problem is something else entirely, and that’s:

            if (empty($newsdate))

            Is not returning empty, even if it is ’empty’. And it seems to work on another MODX of mine, yet not on this... so not sure what I’m missing.
              • 32316
              • 387 Posts
              totally off topic - Is there something special about 17 ?

              if(empty($foo) is just php - so it really has little to do with modx

              and if $foo is empty this will return true - not sure if that is what you meant, and if $foo is not empty it will return False (which is considered empty) so only if $foo is not empty will this return the equivilant of empty.

              Also you do not mention what $newsdate is and whether it is possible that is not set

              So you said:
              if (empty($newsdate))
              Is not returning empty, even if it is ’empty’.
              if it ($newsdate) is empty it should NOT return, but true! Maybe that is what you meant - but it is not what you said

              edit - I missed a word: It should have read... if it ($newsdate) is empty it should NOT return empty, but true!

              more info and we could help more.

              I would like to also point out that by using a CMS you are depending on others to support your use of it (unless you write your own personal CMS)
              Using a well tried extra is only a small step further.
                • 12372
                • 44 Posts
                Quote from: whistlemaker at Jul 23, 2011, 11:30 PM

                Maybe that is what you meant - but it is not what you said

                Well as I said, I don’t know much.

                Quote from: whistlemaker at Jul 23, 2011, 11:30 PM

                I would like to also point out that by using a CMS you are depending on others to support your use of it (unless you write your own person CMS)
                Using a well tried extra is only a small step further.

                Well the odd question/PHP issue is hardly "depending". I have 5 MODX sites and for the most part get on fine. But when you have a question, or something is niggling you, that’s what support forums are for surely? To sound people out and see if anyone has any ideas. Sometimes I even solve issues just in the process of writing a post, as I’m laying out logically in my head.

                For this an add-on seemed overkill as it was working before in Evo but I couldn’t seem to work out why I wasn’t getting the same result in Evo. And I am about four add-ons that no longer work, I’ll try to avoid an add-on if I can. But of course I appreciate add-ons and do use them.

                I solved it in the end though... to be honest, not even sure how. I’m sure I have what I had before. End result, it works.

                Thanks.
                  • 32316
                  • 387 Posts
                  Perhaps my words were taken wrongly - I certainly made a couple other errors in my post, now edited.
                  Your last post
                  Well the odd question/PHP issue is hardly "depending"
                  when I said depending I was referring to a previous post of yours
                  I’m slightly wary of installing a separate package because it creates a reliance on someone supporting it etc.
                  reliance - depending - kinda similar I think, I was not being critical of your stance, just pointing out that when we use a cms we are relying on others code and if use a separate package it is just a little bit more code of others.
                  As you say:
                  But when you have a question, or something is niggling you, that’s what support forums are for surely?
                  Asking questions IS what the forum is all about so I am definitely not trying to be critical about that. So you just misunderstood me when you said
                  Well the odd question/PHP issue is hardly "depending". I have 5 MODX sites and for the most part get on fine. But when you have a question, or something is niggling you, that’s what support forums are for surely?

                  I still am pondering why two people both have 17 at the end of their nicknames - not really important though and do not expect or need an answer!

                  And ... too bad you don’t know what fixed the problem - it is always interesting and informative to find out what did not work and what fixed it.
                    • 4172
                    • 5,888 Posts
                    Quote from: hostile17 at Jun 27, 2011, 04:47 AM

                    I know a bit of PHP, I can piece together some simple if/else stuff etc. But ultimately, I don’t know much!

                    I’m wanting to loop through a number of template variables, churning out their output for each number.

                    So for example I have TVs named News 1 to News 15 - and I want to ouput each of them in turn as long as it’s not empty (as there’s not always 15 news item).

                    So I wanted to basically do this:

                    $newsitem = "[[*News $i]]";

                    The looping is working if I manually put in a number say [[*News 1]] - but I can’t seem to use $i in this context.

                    Is there a way that it can be achieved?

                    Thanks


                    OK,
                    since you didn’t post any of your code, I will do so.
                    This is untested, but I think something like that could do what you need.

                    <?php
                    // call example:      
                    // [[loopTVs? &tvname=`imgsrc` &tpl=`mychunk`]]
                    // chunk example:
                    // <img src="[[+imgsrc_[[+suffix]]]]" title="[[+imgtitle_[[+suffix]]]]" />
                    // <img src="[[+value]]" title="[[+imgtitle_[[+suffix]]]]" />
                    
                    if ($tvs = $modx->getCollection('modTemplateVar', array('name:LIKE' =>$tvname.'_%'))){
                        foreach ($tvs as $tv){
                            $value = $tv->getValue($modx->resource->get('id'));
                            if (!empty($value)){
                                $ph['name'] = $tv->get('name');
                                $nameparts = explode('_',$ph['name']);
                                $ph['suffix'] = $nameparts[1];
                                $ph['value'] = $value;
                                $output .= $modx->getChunk($tpl,$ph);            
                            }
                    
                        }
                    }
                    return $output;


                    name your TVs mytv_1 , mytv_2 , mytv_3 - you can use additional TVs with othertv_1 , othertv_2 , othertv_3 in your chunk, see my in code included examples
                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!
                      • 4172
                      • 5,888 Posts
                      Quote from: whistlemaker at Jul 28, 2011, 12:53 AM

                      I still am pondering why two people both have 17 at the end of their nicknames - not really important though and do not expect or need an answer!

                      well, at the time I registered here my Name ’Bruno’ was allready taken. So I just added the first numbers (dayOfMonth) of my birthday.
                      That’s all about the ’17’ in my name.
                        -------------------------------

                        you can buy me a beer, if you like MIGX

                        http://webcmsolutions.de/migx.html

                        Thanks!