We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30176
    • 99 Posts
    Hi, I am new here. I am looking at Modx for a larger project for myself. But My forte is not web development. I am challenged,though, to continue learning; An older dog here--who qualifies for AARP--trying to learn new tricks. I have basic PHP skills.

    As a test, I want to create a snippet from code generated from Dreamweaver. I am trying a simple example here of attaching to Modx’s database, putting this Dreamweaver code in a Snippet and calling this Snippet in a resource. The snippet returns one record from the modx_site_content table.

    Can this be done and if so how do I modify the code? I have setup Evo 1.2

    Here is the code in my Snippet named ModxTest:

    <?php
    $hostname_MTest = "localhost";
    $database_MTest = "modx";
    $username_MTest = "ttt";
    $password_MTest = "ttttttt";
    $MTest = mysql_pconnect($hostname_MTest, $username_MTest, $password_MTest) or die(mysql_error());
    ?>
    <?php
    mysql_select_db($database_MTest, $MTest);
    $query_RS_Test = "SELECT * FROM modx_site_content";
    $RS_Test = mysql_query($query_RS_Test, $MTest) or die(mysql_error());
    $row_RS_Test = mysql_fetch_assoc($RS_Test);
    $totalRows_RS_Test = mysql_num_rows($RS_Test);
    ?>

    <?php echo $row_RS_Test[’id’]; ?>
    <?php echo $row_RS_Test[’pagetitle’]; ?>
    <?php echo $row_RS_Test[’description’]; ?>

    <?php
    mysql_free_result($RS_Test);
    ?>

    If I could create my code in this way I would feel much more comfortable. And I have a feeling that there are many people out there with Dreamweaver backgrounds who would readily adapt to Modx with a few examples like this.

    Thanks,
    Thomas

      "Remember what Bilbo used to say: It's a dangerous business, Frodo, going out your door. You step onto the road, and if you don't keep your feet, there's no knowing where you might be swept off to."
      -J.R.R. Tolkien from The Lord of the Rings
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      MODx has an extensive API for accessing the database like that. For what you’ve done, use the $modx->getDocument(), $modx->getDocuments(), or $modx->getPageInfo() functions.

      http://wiki.modxcms.com/index.php/API:getPageInfo
      http://wiki.modxcms.com/index.php/Category:API

      There is no reason you can’t either paste your Dreamweaver code into a snippet, or simply include the code from an external .php file in the snippet.

      http://www.sottwell.com/how-snippets-work.html
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 30176
        • 99 Posts
        Hi Susan,

        Thank you for your reply. I am a whole lot more visual and so if I see something I can work it out and later construct it on my own. I have looked at the materials that you sent to me.

        If I understand your answer I have two directions to go here. I can use Modx API and use the functions you have listed. If I do this then I assume I am not using my Dreamweaver code. For simple constructions that is fine but more complexed and I am getting over my head.

        Your second option is what interested me in using Dreamweaver’s code. With the code I listed initially what do I put in my snippet? As it stands now, my snippet is exactly the code I had given before and it is called on the page with [[ModxTest? &id=`1` ]]

        As it is now, I get my template returned but content on the page is blank.

        Thanks again for your help.
          "Remember what Bilbo used to say: It's a dangerous business, Frodo, going out your door. You step onto the road, and if you don't keep your feet, there's no knowing where you might be swept off to."
          -J.R.R. Tolkien from The Lord of the Rings
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          I’m sorry, that code’s just ugly. In the first place, it’s generated to be embedded in an HTML file, which is not how MODx works. In the second place, MODx already has the database connection made; it has to in order to get the document and its template from the database to parse. Snippet code is passed to the PHP eval() function and any output is inserted into the document’s HTML. And finally, using the API you can get exactly what you want in three or four lines of code. It just takes doing it a couple of times to get the hang of it. I’m a California mountain hillbilly grandmother with barely a high school diploma; if I can do it you certainly can. But trust me; buying frozen slice-and-bake cookie rolls isn’t the way to learn how to bake.

          Oh, and if you are determined to use that code, get rid of all those opening and closing php tags. Try this:
          $query_RS_Test = "SELECT * FROM modx_site_content";
          $RS_Test = mysql_query($query_RS_Test); // MODx will produce error messages
          $row_RS_Test = mysql_fetch_assoc($RS_Test);
          $totalRows_RS_Test = mysql_num_rows($RS_Test);
          // treat a snippet like a function - always return, and preferably return its output
          $output = $row_RS_Test['id'];
          $output .= $row_RS_Test['pagetitle'];
          $output .= $row_RS_Test['description'];
          
          return $output;
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 30176
            • 99 Posts
            Hi Susan, You are lovely!! Thank you! I learned more about Modx from that simple comparison than you can imagine. Sorry the code was ugly. But again, it was not mine but the folks at Dreamweaver.

            I knew that the database connection was already open, but without showing that bit of code the references later would not have made sense.

            Trust me, I have been beaten by grandfolks before...and as far a learning to bake, I have no misconceptions that my grandmother could out cook or bake me any day of the week. So why can’t you run circles around me in coding. I am old enough that it doesn’t bother me anymore...well maybe not too much. smiley

            Thank you again!
              "Remember what Bilbo used to say: It's a dangerous business, Frodo, going out your door. You step onto the road, and if you don't keep your feet, there's no knowing where you might be swept off to."
              -J.R.R. Tolkien from The Lord of the Rings