We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30399
    • 23 Posts
    I have a snippet that generates a list of the States of the US from a mySQL database with PHP. The users sees something like this:

    Alabama
    Alaska
    Arizona
    Arkansas
    California
    Etc ...

    The State names are hyperlinks, i.e. the name is the anchor text. I want the user to be able to click a State name and get a new page with info about that State. This data is also held in mySQL. The PHP / mySQL aspect is trivial but I don’t see how to do this ’within’ MODx.

    For example my snippet could create a list of hyperlinks in this format:

    www.domain.com/myscript.php?state=anchor text

    But a user clicking that link would be taken ’outside’ of MODx to a PHP page so I don’t think this is the right approach.

    Is there any way I could generate the page content from ’within’ MODx using a snippet?

    Sorry if this is a dumb question. I don’t want a total answer, just need to be pointed in the right general direction. Thanks!

      • 3749
      • 24,544 Posts
      If you send the user to the same page each time, but with the state name in the $_POST data (or the URL), you could use a snippet on the landing page to produce the display of the data based on the $_POST variable.

      I’m assuming that you only want to show one state’s data on the landing page. If you want to show all states and use anchors, I think forwarding the user to

      [~nn~]#statename


      (where nn is the ID of the landing page) would do it.
        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
        • 30399
        • 23 Posts
        Thank you very much. I used the first method.

        Just one small question.

        Page1 lists the States.

        Page2 lists the info about the state.

        On page1 my snippet link is formed like this:

        while ( $row = mysql_fetch_array($result) ) {

        echo ("<a href=’http://www.domain.com/parent/page12.php?usastate=$row[1]’>$row[1]</a>
        ");

        }


        When I hover over the links on Page1 the url for page2 looks like this:

        page2.php.html

        Any idea why this is? In Tools -> Configuration I have Friendly URL Suffix set to .html. Is that the reason?


        Irrespective of this, it works.
          • 3749
          • 24,544 Posts
          I think so.

          Try expressing the link with a MODx link tag like this in your code (then MODx will make correct link for you):

          echo ("<a href='[~12~]?usastate=$row[1]'>$row[1]</a><br />");


          I’m not sure if it will be parsed after the snippet call, but it’s worth a try. If that doesn’t work, add a parameter to the snippet call:

          &MyUrl=`[~12~]`


          Then in the snippet:

          echo ("<a href='" . $myUrl . "?usastate=$row[1]'>$row[1]</a><br />");


          smiley
            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
            • 30399
            • 23 Posts
            Quote from: BobRay at Feb 14, 2010, 12:07 AM

            I think so.

            Try expressing the link with a MODx link tag like this in your code (then MODx will make correct link for you):

            [b]
            echo ("<a href='[~12~]?usastate=$row[1]'>$row[1]</a><br />");

            [/b]
            I’m not sure if it will be parsed after the snippet call, but it’s worth a try. If that doesn’t work, add a parameter to the snippet call:

            &MyUrl=`[~12~]`


            Then in the snippet:

            echo ("<a href='" . $myUrl . "?usastate=$row[1]'>$row[1]</a><br />");


            smiley


            Thanks BobRay.

            I tried the [~id~] method of referencing a page, rather than hardcoding the url, but it doesn’t work correctly. For some reason it duplicates the parent name:

            www.domain.com/parent/parent/page.php

            It should be:

            www.domain.com/parent/page.php


            I also tried using the second approach

            [b]
            &MyUrl=`[~12~]`


            Then in the snippet:

            echo ("<a href='" . $myUrl . "?usastate=$row[1]'>$row[1]</a><br />");
            [/b]


            But it creates the same format of link: page.php.html?etc

            Strange eh?
              • 22303 MODX Staff
              • 10,725 Posts
              All MODx templates, especially those using Friendly URL Aliases with the Alias Path option enabled, need this in the HEAD tag:
              <base href="[(site_url)]"></base>

                • 30399
                • 23 Posts
                Quote from: OpenGeek at Feb 14, 2010, 12:46 PM

                All MODx templates, especially those using Friendly URL Aliases with the Alias Path option enabled, need this in the HEAD tag:
                <base href="[(site_url)]"></base>



                Thanks! That works.

                I didn’t know how to progress this at the beginning because I didn’t realise you could give MODx pages a .php extension (thus be able to pass variables to them). rolleyes
                  • 22303 MODX Staff
                  • 10,725 Posts
                  Quote from: tonymodx at Feb 14, 2010, 02:38 PM

                  Thanks! That works.

                  I didn’t know how to progress this at the beginning because I didn’t realise you could give MODx pages a .php extension (thus be able to pass variables to them). rolleyes
                  You don’t have to give MODx pages a php extension to be able to pass variables to them. All MODx pages can use PHP variables via Snippets and Plugins.
                    • 30399
                    • 23 Posts
                    Hello

                    thanks OpenGeek for your comment. If you can point me in the direction of documentation that would enable me to not have to declare pages as .php in order to pass variables (in the form of ?name=value pairs) I would be truly grateful. Please bear in mind that I am not a programmer but just someone who sees the potential of MODx.

                    As I wrote above, I have a snippet that displays a list of US States and when the user clicks a State (i.e. the anchor text) I want him to be taken to another page which contains all the info for that one State. Actually, for me, this is a model for a lot of other things I would like to try with MODx, so if I could figure out a solution in the way you said (and believe me I have tried and read a lot of things) I would be very happy.

                    Looking again at my site yesterday I was wrong - the pages are still showing the strange ending page1.php.html I have included this:

                    <base href="[(site_url)]"></base>
                    in the head section of my template

                    Tony
                      • 27708 MODX Staff
                      • 2,502 Posts
                      You can pass variables to a page so long as the php script you have is either in a snippet or using the API. Don’t get hung up on file extensions as they are purely abstract for the purposes of friendly URLs when you have fURLs enabled. It uses Apache mod_rewrite to give a file an extension so even though your file has the extension .html or php they are both php files rendered via /index.php. Giving a page a php extension doesn’t do any thing but change the friendly url. You could call your files .pizza or .joe or whatever you want it is still parsed by the MODx parser.

                      This is a user contributed document on how to Create a Snippet. Snippets are php scripts that perform any logic you want done on a page and you access them by calling them using the following Syntax: [[SnippetName? &parameter=`` &parameter2=``]] etc. Parameters are any value you wish to pass from the individual to the snippet. There need not be any parameters if the snippet doesn’t need values from the document.
                        Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub