We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6561
    • 139 Posts
    I want to read the values from a database, then display the result of that query to an overview page. Is there some kind of method in Modx for querying database rows/fields and looping through them?

    I know how to do this in asp, but I only know a tiny little bit about php... I tried to modify the code from the following post http://modxcms.com/forums/index.php/topic,33267.0.html but no success... nothing displays :-(

    My database is called ’test’, my table is called ’contest’ and has 4 values (an ID as primary key and 3 string values).

    Also, I tried making a chunk with a simple ’show current date’ function, but it only works if I call it like [[ShowTheDate]] , does not work when I use [[!ShowTheDate!]] , why is that?


    Edit: I found this tutorial instead ( http://wiki.modxcms.com/index.php/DB_query_and_Placeholders ) and got it working!
      • 6561
      • 139 Posts
      Okay... next thing I want to do is create a form which takes some values and then it uses those values to update the data in the table.

      E.g. (values from form ’name’ attribute tags) :


      • id
      • namevalue
      • familynamevalue
      • streetvalue

      On submitting the form, the table row should be updated... e.g. with a construct like this:

      UPDATE Contest
      SET Name='namevalue', Familyname='familynamevalue', Streetvalue='Streetvalue' WHERE id='id'


      I have some idea on how to do it, the main problem I have is that I do not really have an idea how to ’transfer’ the values from the form to a variable in the ’update’ script/snippet.

        • 22303 MODX Staff
        • 10,725 Posts
        Quote from: Darkelve at Feb 26, 2009, 05:31 AM

        I have some idea on how to do it, the main problem I have is that I do not really have an idea how to ’transfer’ the values from the form to a variable in the ’update’ script/snippet.
        All of the variables POSTed from your form would be available in the PHP superglobal $_POST array. So you could access individual form fields with $_POST[’nameOfField’], though please make sure to validate the user supplied values to prevent creating potential security vulnerabilities to your entire site.
          • 6561
          • 139 Posts
          Quote from: OpenGeek at Feb 26, 2009, 10:38 PM

          Quote from: Darkelve at Feb 26, 2009, 05:31 AM

          I have some idea on how to do it, the main problem I have is that I do not really have an idea how to ’transfer’ the values from the form to a variable in the ’update’ script/snippet.
          All of the variables POSTed from your form would be available in the PHP superglobal $_POST array.  So you could access individual form fields with $_POST[’nameOfField’], though please make sure to validate the user supplied values to prevent creating potential security vulnerabilities to your entire site.

          Thanks for that!

          Right now I’m only testing some things, but I’ll keep your advice in mind for later. You are talking about people pasting weird code (programming/script code, ’crash’ code) into forms to try and hack the site, right?
            • 33997
            • 150 Posts
            Quote from: Darkelve at Feb 26, 2009, 03:06 AM


            Also, I tried making a chunk with a simple ’show current date’ function, but it only works if I call it like [[ShowTheDate]] , does not work when I use [[!ShowTheDate!]] , why is that?


            Edit: I found this tutorial instead ( http://wiki.modxcms.com/index.php/DB_query_and_Placeholders ) and got it working!


            I don’t know if you figured this out yet; by the way you are showing your call, you must be using a snippet, not a chunk. Snippets are used for php, and chunks are only raw html. It looks like it should work to call it uncached, but you have too many brackets in there i.e. when calling uncached you would call it as [!ShowTheDate!] not [[!ShowTheDate!]] (the second one looks like it is passing the snippet name as !ShowTheDate!)
              "Great spirits have always encountered violent opposition from mediocre minds." -Albert Einstein