We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12584
    • 208 Posts
    I have an existing site which has a very simple php script which pulls some info out of one table in a database on just one page, what I would like to do is convert this site to MODx but I am not sure how to intergrate this with MODx.

    1. Should I copy the table into the modx database so that the content will become searchable.

    2. I guess I need to put the php into snippet, but is there anything for the connection to the database I need to know about or do I leave it the same as on the non MODx site like this $dbcnx = !@mysql_connect(’localhost’, ’root’, ’password);

    3. Am I so out of my depth should I not bother.

    Cheers

    Ian
      • 3749
      • 24,544 Posts
      Quote from: ian_m at Sep 04, 2007, 04:46 PM

      I have an existing site which has a very simple php script which pulls some info out of one table in a database on just one page, what I would like to do is convert this site to MODx but I am not sure how to intergrate this with MODx.

      1. Should I copy the table into the modx database so that the content will become searchable.

      2. I guess I need to put the php into snippet, but is there anything for the connection to the database I need to know about or do I leave it the same as on the non MODx site like this $dbcnx = !@mysql_connect(’localhost’, ’root’, ’password);

      3. Am I so out of my depth should I not bother.

      Cheers

      Ian


      If you wrote the original php code, you’re almost certainly up to doing this.

      You should make a snippet and put it a call to it on your display page, e.g.
      [!yourSnippetName!]

      It could contain your original code (and maybe you should to start with that) but you might also want to consider putting the data into a MODx table and using the MODX Database API (DBAPI) to access it. See http://www.modxcms.com/dbapi.html

      Down the road, you might want to modify the snippet to take additional parameters. That way you can have different displays of the data on different pages, let the user select how things should look, or show a debugging version. All these will involve calling the same snippet but with different parameters, like so:

      [!yourSnippetName? &fullDisplay=`yes` &debug=`no`!]



      Look at some of the existing snippets in MODx to see how to do that.

      Bob


        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
        • 10487 MODX Staff
        • 1,535 Posts
        Well, I’m in agreement with Bob on this one - with a bit of reading up on the DBAPI and some refactoring of your original code you’ll have no problems getting your code into a snippet.

        If you transfer your table into the MODx database, then you can eliminate having to create your own connection to the database because the DBAPI handles the connection. Just refactor any mysql function calls to use the MODx DBAPI and you’ll be on your way.

        Note that snippets behave very much like PHP functions so store your snippet output in a variable and then use a ’return’ statement at the end of your snippet to push your snippet output to the page as opposed to using ’echo’, ’print’ or inline PHP statements. (Hope that makes sense wink It’s getting late and I’m pass my bedtime, lol)
          Garry Nutting
          Senior Developer
          MODX, LLC

          Email: [email protected]
          Twitter: @garryn
          Web: modx.com
          • 3749
          • 24,544 Posts
          Quote from: garryn at Sep 04, 2007, 06:35 PM

          Note that snippets behave very much like PHP functions so store your snippet output in a variable and then use a ’return’ statement at the end of your snippet to push your snippet output to the page as opposed to using ’echo’, ’print’ or inline PHP statements. (Hope that makes sense wink It’s getting late and I’m pass my bedtime, lol)

          Garry,

          Could you give a quick example of how to use the return statement to pass information. I’ve created some snippets that I think would be improved by using this technique but I’ve never quite understood it in the snippet context. It sounds like a way to avoid using relatively inelegant SESSION variables.

          Bob
            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
            • 12584
            • 208 Posts
            Thanks very much for the replies,

            Well I put my php in a snippet added my tables to the MODx database updated my database log in and wow it worked like a dream, one of the tables included a list of names and these dont get pulled out by the search function but for now I am just happy that I can include my own php bits no problem, had a scan of the dbapi and it is just a little beyond me, for instance if I am querying a table I have added to the database why does it say $table = $modx-> values.

            Not quite understandwhy I need the $modx-> but, like I said I only know very basic php but am trying to learn more.

            Thanks again

            Ian