We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11927
    • 105 Posts
    I’m not sure if my subject really explains what I am trying to do.

    I’m trying to create something like a directory, but dynamically from a database. I have a page called: regions. What I’m trying to do is ’create’ pages under regions that show the different areas our clients work in, but according to the database I have. I don’t want to have to create a resource/document for each area because that would defeat the purpose of the benefits of having the information in the database.

    So the page regions is not a folder and doesn’t have any child documents. The url to regions is mysite.com/regions.html .

    The database has different regions in it.
    City A, CA
    City B, CA
    City Q, NY

    Although there is no page for CA or NY, I want to dynamically ’create’ a page for CA and NY that will then list the areas for those states. The url would look something like mysite.com/regions/ca.html and mysite.com/regions/ny.html .

    Then for the cities, these would be dynamically ’created’ pages as well that would talk about that city. For example the url for City A, CA would look like mysite.com/regions/ca/city-a.html .

    So would I use a snippet or a plugin or both? How would I prevent the Page Not Found from showing and instead show my pages?

    I think I would use a plugin and snippet combination, but I can’t figure out how to create the plugin. I need it to
    1. recognize the url in the address bar (I think I got that figured out)
    2. prevent the Page Not Found page from showing up and call the snippet instead
      You may or may not want to use the code I write. It's probably all against the syntax rules of php and MODx. smiley

      Carpet Cleaning
      • 6316
      • 92 Posts
      Just a few days ago I’ve finished a very similar system (based on Evolution) for the website of a model agency. It allowed me to make a multilingual website while keeping a single (therefore easily maintained) database of models.

      The only difference is that I still used MODx to run the database, because I love how easily MODx allows me to make fields (TVs), customize the look of a form (ManagerManager plugin) and set permissions for the employees. So in my case each entry is still a MODx resource, but I call them through the database in various ways for every language.

      So how would I do it:

      1. Create a snippet, which would accept URL parameters and output placeholders

      if (isset($_GET['region']))
      {
      	// Don't forget to add some regexps and stuff to filter unwanted crap from the request
      
      	// Some SQL queries should be here, perhaps it can be done with the MODx DB class
      	// Read more http://wiki.modxcms.com/index.php/API:DBAPI
      	$modx->db->select(fields to select, tables, query);
      
      	// Process results into placeholders to show them on the page
      	$myPlaceholders['someField1'] = some_processed_result_from_db;
      	$myPlaceholders['someField2'] = some_processed_result_from_db; 
      
      	foreach ($myPlaceholders as $key => $value)
      	{
      		$modx->setPlaceholder($key, $value);
      	}
      }


      2. Make a new template specifically for your "Regions" page. Place the snippet call in the very beginning of it, then you can place the placeholders around.
      [!mySnippetCall!]
      <table>
      	<tr>
      		<td>City</td>
      		<td>[+someField1+]</td>
      	</tr>
      
      	<tr>
      		<td>State</td>
      		<td>[+someField2+]</td>
      	</tr>
      </table>


      3. Don’t forget to set your "Regions" page to use the new template. By the way you may leave "cacheable" ticked, it won’t affect functionality of the snippet.

      4. If your "Regions" has an alias "regions.php" (it will work without .php too), you may see test your viewer at your-site.com/regions.php?region=some_region_to_query_from_the_db

      5. The second major step would be to make another snippet (or modify existing) which would generate those links somewhere, so user could follow them on the website.

      I hope you get the idea.

      In the end you may even modify .htaccess to enable friendly URLs for those new pages. I.e. your-site.com/regions/some_region_name

      To do it, add something like this before the standard friendly URLs part:
      # Stuff for mySnippet
      RewriteRule regions/(.*) regions.php?region=$1 [L]
      RewriteRule regions/(.*) regions.php?region=$1 [L]
      


      (assuming regions.php is the alias of your MODx page which uses the new template; again ".php" extension is not really needed)
        • 11927
        • 105 Posts
        Wow, thank you so much. You explained it really well. I didn’t even know where to begin.

        I think with code you provided for the .htaccess, I finally understand how friendly urls are made. That .htaccess code is hard to understand.

        I got it working and am just now trying to make it look pretty.

        Thanks again!
          You may or may not want to use the code I write. It&#39;s probably all against the syntax rules of php and MODx. smiley

          Carpet Cleaning