We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38740
    • 45 Posts
    Hi, i am a ASP-guy converting to MODx/Php trying to figure things out smiley

    Im used to build db-solutions for customers on IIS/ASP.

    In ASP i used a Recordset-object to execute Queries, Update records and use the result in the Recordset to present it on webbpages.


    How is this done in "Best Practice" in php against custom tables in Modx?

    Thx
    /R Sweden
    [ed. note: johnjohn22 last edited this post 14 years ago.]
      ____________________________________________________
      • 3548
      • 102 Posts
        Antonio Zdilar
        linearvector.com
        • 38740
        • 45 Posts
        Hi BC, thanks for link, will try it out.

        I managed to used plain php to INSERT and also SELECT data from db. I put the code in two snippets, one to insert and one to read.

        Q1: Why should i use xPDO instead of plain php to access my db ?

        Q2: In ASP i usualy mix server script with html to presernt the final result. How do i do this in MODx, in a chunk or a snippet?

        READ DATA:
        mysql_connect('x.x.x.x',$username,$password);
        @mysql_select_db($database) or die( "Unable to select database");
        $query = "INSERT INTO custom_products VALUES ('','Saab','3')";
        mysql_query($query);

        READ DATA:
        mysql_connect('x.x.x.x',$username,$password);
        @mysql_select_db($database) or die( "Unable to select database");
        $query="SELECT * FROM custom_products";
        $result = mysql_query($query);
          ____________________________________________________
          • 3548
          • 102 Posts
          Q1: xPDO is an ORM, so google for ORM and you will find answer to Q1

          Q2: You should not mix script with html. Script (php) goes to snippets (retrieve data, do some logic..) and presents data through chunk (html + placeholders for data passed from snippet)

            Antonio Zdilar
            linearvector.com
            • 38740
            • 45 Posts
            Thanks BC. i will follow your lead... smiley
              ____________________________________________________
              • 38740
              • 45 Posts
              Ok, so i got some xPDO database-operations to work.

              Question: Now, i would love a way to write plain SQL-querys?

              Have tried this but it returns 0 rows:

              $query = "SELECT * FROM Products WHERE iStatus=1";
              $criteria = new xPDOCriteria($modx, $query);
              $products = $modx->getCollection('Products',$criteria);
              $output .= '<p>Totalt: '. count($products) . '</p>';
                ____________________________________________________
                • 22303 MODX Staff
                • 10,725 Posts
                It doesn't work because your WHERE clause is empty.
                  • 38740
                  • 45 Posts
                  Thx opengeek, it should be:

                  $query = "SELECT * FROM Products WHERE field1=1 OR field1=2";
                  $criteria = new xPDOCriteria($modx, $query);
                  $products = $modx->getCollection('Products',$criteria);
                  $output .= '<p>Totalt: '. count($products) . '</p>';
                    ____________________________________________________
                    • 38740
                    • 45 Posts
                    And a getChunk question:

                    I pass a database-row to a chunk like this:

                    foreach($products as $product) {
                    $fields = $product->toArray();
                    $output .= $modx->getChunk('ListProduct', $fields);
                    }

                    Question: How can i pass an additional value besides the database-values ?

                    I have tryed with $output .= $modx->getChunk('ListProduct ? &name=´george´', $fields); but it didnt work...
                      ____________________________________________________
                      • 3548
                      • 102 Posts
                      foreach($products as $product) {
                        $fields = $product->toArray();
                        $fields['besidesDBValue']='someValue';
                        $output .= $modx->getChunk('ListProduct', $fields);
                      }
                      
                        Antonio Zdilar
                        linearvector.com