We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18953
    • 5 Posts
    I was hoping someone could give me some direction as to how I can achieve this in Modx Revo.

    I am trying to create a page that shows links for all 50 US states and then on each of the states pages, links to all county pages within in each state. There are around 3600 counties in the US.

    I’m thinking that there is a right way to achieve this instead of manually creating 3000+ documents in Modx.

    Perhaps dynamic links like http://www.mydomain.com/state.php?state=Alabama&county=Jackson

    Any suggestions on the right way to do this (before I start manually creating documents) is appreciated. Thanks!
      • 3749
      • 24,544 Posts
      What you want is a file (or a MODx chunk) containing the state names and respective county names in a consistent format. Then, a very simple snippet would parse that and create all the pages for you in no time at all.

      Do you have a list of the states and their counties that could be parsed or re-formatted and then parsed to do the job?
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 26016
        • 561 Posts
        Wow Bob,
        That sounds great. I wish I had thought of that. So we’re parsing that CSV file or what-have-you, and the snippet issues a command to create a doc from each data item when it runs? Or have I lost it, and just need feeding?

        I’m peering quizically at the API, and can’t quite find something for creating a doc programatically. Any places you can point me to a clue for that?

        Thanks! Dave

        btw, when you’re taking a quiz, you could be said to be quizzical. When you’re taking a test, you could be said to be....?????
          MODx and Wordpress development
          Linux, PHP 5.2, MySQL 5.0, Evo 1.05, Revo 2.08-pl, Firefox 4
          • 3749
          • 24,544 Posts
          Quote from: samba at Nov 17, 2010, 06:33 PM

          Wow Bob,
          That sounds great. I wish I had thought of that. So we’re parsing that CSV file or what-have-you, and the snippet issues a command to create a doc from each data item when it runs? Or have I lost it, and just need feeding?

          I’m peering quizically at the API, and can’t quite find something for creating a doc programatically. Any places you can point me to a clue for that?

          Thanks! Dave

          btw, when you’re taking a quiz, you could be said to be quizzical. When you’re taking a test, you could be said to be....?????
          You’ve got it. Post an example from your state/county file and I’ll get you started on the snippet.
          If each state is in a separate file, named for the state, all in the same directory, with a comma-separated list of counties, that would make it a little easier -- but we can probably make anything work as long as it’s consistent.

          Think about what you want for the Page Title, Long Title, and Alias of each resource.

          BTW, that’s a crapload of resources. I hope you’re using Revolution. wink


            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 18953
            • 5 Posts
            Thanks Bob...

            If I understand correct, the snippet will generate the documents based on the csv file. So there will be 50 .csv files in the same folder each named for a state. I have attached an example of a state csv file set up the way I think you are suggesting but I’m not sure how to set up the snippets/chunks to parse the data.

            Thanks for the help!
              • 3749
              • 24,544 Posts
              Quote from: mdninter at Nov 18, 2010, 02:42 PM

              Thanks Bob...

              If I understand correct, the snippet will generate the documents based on the csv file. So there will be 50 .csv files in the same folder each named for a state. I have attached an example of a state csv file set up the way I think you are suggesting but I’m not sure how to set up the snippets/chunks to parse the data.

              Thanks for the help!


              OK, but that’s not a CSV file. That’s a screenshot from a spreadsheet and most of the fields are unnecessary since they can be generated automatically.

              A much better format would be:

              Filename: Alabama

              Good formats for the file content:

              "Austen","Broward","Finley,"Howard", etc.

              Austen,Broward,Finley,Howard, etc.

              Austin
              Broward
              Finley
              Howard
              etc.

              Austin,
              Broward,
              Finley,
              Howard,
              etc.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 18953
                • 5 Posts
                Ok... sorry about that. I attached a .txt file with all the counties in Alabama. .csv is not an accepted file format to upload in the forums.
                  • 3749
                  • 24,544 Posts
                  OK, your snippet will look something like this (untested):

                  [[!MakeStates? &stateList=`Alabama,Arkansas,Arizona`]]


                  <?php
                  /* Make States Snippet */
                  
                  $delimiter = "\n\r";   // (could be a comma instead)
                  
                  /* get list of state files from parameter */
                  $states = explode(',',$stateList);
                  $path = "path/to/files/";
                  
                  /* This will hold the menu for all states */
                  $stateLinks = '<ul>';
                  
                  foreach($states as $state) {
                        $content = '<ul>';  // beginning of county menu 
                        $filepath = $path . $state;
                        $text = file_get_contents($filename);
                  
                      if ($text === false) {
                         return "failed to get file contents";
                      }
                  
                      /* create state resource */
                  
                      $fields = array(
                          'pagetitle'=>$state,
                          'longtitle'=>$state,
                          'alias'=>$state,
                          'published'=>'1',
                      );
                      $stateObj = $modx->newObject('modResource',$fields);
                      $stateObj->save();
                  
                      $stateId = $stateObj->get('id'); // get state id to be county page parent
                  
                      $stateLinks .= "\n" .  '<li><a href = "[[~' .  $stateId . '"</a>' . $state . '</a></li>';
                  
                      /* create county pages */
                  
                      $counties = explode($delimiter,$text);
                  
                      if (count($counties) < 5) {
                        return 'Something is wrong -- too few counties';
                      }
                  
                      foreach($counties as $county) {
                         $county = trim($county);  // trim extraneous characters from county name
                         $pagetitle = $county;
                         $longtitle = $pagetitle;
                         $alias = tolower($county);
                         $alias = str_replace(' ','-',$alias);
                         $parent = $stateId;
                  
                         $fields = array(
                             'pagetitle'=>$pagetitle,
                             'longtitle'=>$longtitle,
                             'alias'=>$alias,
                             'parent'=>$parent,
                             'published'=>$parent;
                         );
                         //create county resource 
                         $countyObj = $modx->newObject('modResource',$fields);
                         $countyObj->save();
                         $countyId = $countyObj->get('id');
                  
                         // add link for county to state page menu 
                         $content .= "\n" . '<li><a href="[[~' . $countyId . ']]>' . $county . '</a></li>';
                      } /* end foreach county */
                  
                      // end county menu
                      $content .= '</ul>';
                      /* might have to re-get the state object with $modx->getObject here */
                  
                      /* add county menu to state page */
                     /* might have to re-get the state object with $modx->getObject here */
                      $stateObject->setContent($content);
                      $stateObject->save();
                  
                  }  /* end foreach state */
                  
                  /* Make all state page */
                  $stateLinks = '</ul>';
                  
                  $fields = array(
                     'pagetitle'=>'All States',
                     'longtitle'=>'All States',
                     'alias'=>'all-states';
                     'published'=>'1',
                  );
                  $allStateObj = $modx->newObject('modResource',$fields);
                  $allStateObj->setContent($stateLinks);
                  $allStateObj->save();
                  
                  return "Pages Created";
                  
                  
                  
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 18953
                    • 5 Posts
                    Ok… I’m pretty sure I understand most of the code but I’m having trouble getting it to work. I have been just trying with one state to start but I just get a blank page when I place the snippet call in a document.

                    Should the state files with all the counties be in a .txt or a .csv file or have no file extension?

                    Do I just place the snippet call in a document and load that document to generate all the new documents in Modx? Not 100% sure I am doing this correctly.

                    I made some slight changes to the code but still no luck.

                    Thanks for the help Bob…

                    <?php
                    /* Make States Snippet */
                    
                    $delimiter = "\n\r";   // (could be a comma instead)
                    
                    /* get list of state files from parameter */
                    $states = explode(',',$stateList);
                    $path = "assets/states/";
                    
                    /* This will hold the menu for all states */
                    $stateLinks = '<ul>';
                    
                    foreach($states as $state) {
                          $content = '<ul>';  // beginning of county menu 
                          $filepath = $path . $state;
                          $text = file_get_contents($filename);
                    
                        if ($text == false) {
                           return "failed to get file contents";
                        }
                    
                        /* create state resource */
                    
                        $fields = array(
                            'pagetitle'=>$state,
                            'longtitle'=>$state,
                            'alias'=>$state,
                            'published'=>'1',
                        );
                        $stateObj = $modx->newObject('modResource',$fields);
                        $stateObj->save();
                    
                        $stateId = $stateObj->get('id'); // get state id to be county page parent
                    
                        $stateLinks .= "\n" .  '<li><a href="[[~' .  $stateId . ']]">' . $state . '</a></li>';
                    
                        /* create county pages */
                    
                        $counties = explode($delimiter,$text);
                    
                        if (count($counties) < 5) {
                          return 'Something is wrong -- too few counties';
                        }
                    
                        foreach($counties as $county) {
                           $county = trim($county);  // trim extraneous characters from county name
                           $pagetitle = $county;
                           $longtitle = $pagetitle;
                           $alias = tolower($county);
                           $alias = str_replace(' ','-',$alias);
                           $parent = $stateId;
                    
                           $fields = array(
                               'pagetitle'=>$pagetitle,
                               'longtitle'=>$longtitle,
                               'alias'=>$alias,
                               'parent'=>$parent,
                               'published'=>$parent;
                           );
                           //create county resource 
                           $countyObj = $modx->newObject('modResource',$fields);
                           $countyObj->save();
                           $countyId = $countyObj->get('id');
                    
                           // add link for county to state page menu 
                           $content .= "\n" . '<li><a href="[[~' . $countyId . ']]">' . $county . '</a></li>';
                        } /* end foreach county */
                    
                        // end county menu
                        $content .= '</ul>';
                        /* might have to re-get the state object with $modx->getObject here */
                    
                        /* add county menu to state page */
                       /* might have to re-get the state object with $modx->getObject here */
                        $stateObject->setContent($content);
                        $stateObject->save();
                    
                    }  /* end foreach state */
                    
                    /* Make all state page */
                    $stateLinks = '</ul>';
                    
                    $fields = array(
                       'pagetitle'=>'All States',
                       'longtitle'=>'All States',
                       'alias'=>'all-states';
                       'published'=>'1',
                    );
                    $allStateObj = $modx->newObject('modResource',$fields);
                    $allStateObj->setContent($stateLinks);
                    $allStateObj->save();
                    
                    return "Pages Created";
                    
                    

                      • 3749
                      • 24,544 Posts
                      The state files should have no extension. They need to match the names in the &stateList snippet parameter exactly (case-sensitive).

                      I do see a mistake in the code (I did say it was untested wink ):

                       $text = file_get_contents($filename);


                      Should be:

                       $text = file_get_contents($filepath);
                      


                      You should probably add this just above that line:

                      if (! file_exists($filepath)) {
                        return 'No file at: ' . $filepath;
                      }



                      Put this in your resource:

                      <p>Creating States and Counties . . . </p>
                      [[!MakeStates? &stateList=`Alabama,Arkansas,Arizona`]]
                      


                      When you preview the resource, the docs should be created.

                      Double check the spelling of the snippet name and the snippet parameter name.
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting