We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3046
    • 48 Posts
    Good day to everybody!

    I have snippet:

    <?php
    if (!isset($page)) $page=0;
    $data=$modx->getChunk("config");
    $max=count($data);
    if ($page >$max) $page=0;
    if (ereg("[[:alpha:]]",$page)) $page=0;
    echo "";
    include("$data");
    echo "";
    ?>



    and chunk:

    http://www.mysite.com/index.html
    http://www.mysite.com/contacts.html
    http://www.mysite.com/price.html
    http://www.mysite.com/order.html



    And the questions is how i can make this function ($data=$modx->getChunk("config");) to get addresses line by line from chunk. Cause now it gets it as one line. Thanks everybody for any advise!
      • 16034
      • 107 Posts
      I think something like this could do the job:
      <?php
      $data = $modx->getChunk("config");
      $data_array = explode("\n",$data);
      foreach($data_array as $address) {
        // Do something with $address
      }
      
      // Or just access one specific line:
      echo "Line 2 contains: ".$data_array[1]."\n";
      // Remember that arrays start at index 0
      

      (Not tested)
        MODx snippet-glossary 101:
        Ditto = Content Lister -- Wayfinder == Menu Builder -- Jot = Comment Control
        • 3046
        • 48 Posts
        Yes, particully your version works, but only with manual changing of lines.

        I have different var to send to this script (like page=’1’, page=’2’ and so on)
        And it needs not to show the results, but start the page, from chosen line.

        <?php
        if (!isset($page)) $page=0;
        $data=$modx->getChunk("config");
        $max=count($data);
        if ($page >$max) $page=0;
        if (ereg("[[:alpha:]]",$page)) $page=0;
        echo "";
        include("$data");
        echo "";
        ?>
        


        i think it needs smth like $data=($data[$page]); to get necessary page from line.. comments? )) (i’m really noob in php ;-))
          • 29181
          • 480 Posts
          I’m a little lost as to your aims exactly, but if the code that Kleist posted works for the most part, why are you using a different piece of code?


          if (!isset($page)) $page=0;
          $data = $modx->getChunk("config");
          $data_array = explode("\n",$data);
          echo "Line 2 contains: ".$data_array[$page]."\n";
          


          I may well be on the wrong tracks though, I couldn’t quite understand

          only with manual changing of lines.

          and

          it needs not to show the results, but start the page, from chosen line.

          Hope this helps in some way!
          Taff
            Adrian Lawley: www.adrianlawley.com
            • 16034
            • 107 Posts
            When you have the array $data_array (after explode).
            include($data_array[$page]);
            

            includes the file on line $page+1 in the file. (+1 because arrays start at 0).
              MODx snippet-glossary 101:
              Ditto = Content Lister -- Wayfinder == Menu Builder -- Jot = Comment Control