We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25287
    • 81 Posts
    I am stumbling through the process of creating a very simple payment system as every one I've looked at is way to complex for my needs. So far I've manged to build a custom table and the component requirements for it using Bob's Custom Table guide. That seems to be working. I've spent hours searching the forums, reading Bob's Snippet FAQ's and trying numerous combinations.

    I have a page that uses the following chunk and snippet to read from the db table and write values to a table on the page with a submit button.
    <table id="prices">
    <td class="item_name" width="160">[[+Size]]   [[+Format]] Print</td>
    <td  class="item_price" width="40"><strong>$[[+Price]]</strong></td>
    <td class="buy_button" width="120">
    <form action="[[!getMYpid? &IDprint=`[[+PriceID]]`]]" method="post" >
    <input type="image" src="assets/gfx/Add2Cart.png" alt="Click to submit"/>
    </form>
    </table>
    


    The button calls the snippet getMYpid to open a new page (id 11) and pass the PrintID for another snippet to use. The idea is to read the data from the custom table to verify that the customer has chosen the correct image and size etc, and that the price has not been manipulated.
    $PrintID = $scriptProperties['IDprint'];
    $_SESSION['IDprint'] = $PrintID;
    
    $webLink = "http://localhost/modx/index.php?id=11";
    # $webLink = "http://localhost/modx/index.php?id=11" .$PrintID;
    
    return $webLink;
    


    I know the getMYpid snippet correctly transfers the value because of what the commented line produces when used. However, the next snippet either does not read the $_SESSION['IDprint'] value, or there are other errors in the snippet.

    <?php
    $path = MODX_CORE_PATH . 'components/Prints/';
    $result = $modx->addPackage('Prints',$path .
        'model/','rg_');
    
    # get Price etc from 'rg_Prints' table
    #
    $PrintID = "0"; // clear the value
    # $PrintID = $_POST['IDprint'];
    $PrintID = $_SESSION['IDprint'];
    
    return $PrintID;
    
    $sortingcriteria = 'Size';
    $filter1 = $PrintID;
    $s = $modx->newQuery('Prints');
    $s->sortby($sortingcriteria,'ASC');
    $s->where(array(
       'PriceID' => $filter1
    ));
    $prints  = $modx->getCollection('Prints',$s);
    foreach ($prints as $photoprint) {
        $fields = $photoprint->toArray();
        $output = $output .$modx->getChunk('ShopItems', $fields);
    }
    return $output;
    


    Finally the chunk ShopItems is called on the processing page (id 11)
    <table>
    <td>[[+Size]] [[+Format]] width print on [[+Medium]]</td>
    <td><h3>Price: <strong>AU$[[+Price]].00</strong></h3></td>
    </tr></table>


    As you can probably tell from my code, I'm very inexperienced with php and mysql. Any help would be much appreciated. Hopefully it's something simple.

    Thanks

    Rob
      • 45533
      • 23 Posts
      <form action="[[~11]]" method="post">
      <input type="hidden" name="IDPrint" value="[[+PriceID]]"/>
      
      </form>
      


      then in your snippet

      $PrintID = $_POST['IDPrint'];
      


      use this again instead of the session variable.

      and it should work [ed. note: aquabug last edited this post 11 years, 10 months ago.]
        • 3749
        • 24,544 Posts
        I would add some debugging code.

        Test the return value of addPackage:

        if (empty($result)) {
           return 'addPackage failed';
        }


        You can take a look at the actual query with this:
        $s -> prepare();
        return 'SQL: ' .  $s->toSQL();
        

        Also,

        $prints  = $modx->getCollection('Prints',$s);
        if (empty($prints)) {
           return 'getCollection returned no objects';
        }


        In theory, you don't need it, but you might also try adding a loadClass() statement and checking its return value (false on failure).

        This is the kind of problem where it's nice to have a code editor with a debugger. I use PhpStorm. You can instantiate MODX easily in a test snippet and step through your code line-by-line in the debugger to find out exactly where and why things are falling over.
          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
          • 25287
          • 81 Posts
          Thank you aquabug and BobRay!

          Adding the debugging code means I can see that it is working up until the getCollection point. The return 'SQL" output shows it is getting the result I'm after, but at this stage the output chunk is not outputting anything.

          I need to get to my paid job now, but will look at how to use PhpStorm tonight.

          Once it's sorted I'll post the working code for future reference.

          Blessings

          Rob
            • 45533
            • 23 Posts
            you might have a closer look at this line :

            $output = $output .$modx->getChunk('ShopItems', $fields);
            


            just if you're interessted - Bob wrote an awesome article about addPackage, loadClass etc. - it helped me a lot to understand how that works :

            http://bobsguides.com/blog.html/2013/05/25/understanding-addpackage%28%29,-loadclass%28%29,-and-getservice%28%29/
              • 3749
              • 24,544 Posts
              That certainly narrows down the possibilities.

              It shouldn't make any difference, but this:

              $output = $output .$modx->getChunk('ShopItems', $fields);


              is usually done this way:

              $output .= $modx->getChunk('ShopItems', $fields);


              Make sure that the chunk name is correct and that the placeholders in the chunk match the actual object field names exactly.
                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
                • 25287
                • 81 Posts
                Thanks for the suggestions. I changed the $output line to your suggestion BobRay. However, no joy yet.

                I've made sure the chunk name is correct by cutting & pasting the chunk name to the snippet.

                The code suggested by BobRay
                $s -> prepare();
                return 'SQL: ' .  $s->toSQL();

                returns this result:
                SQL: SELECT `Prints`.`id` AS `Prints_id`, `Prints`.`Edition` AS `Prints_Edition`, `Prints`.`Size` AS `Prints_Size`, `Prints`.`Price` AS `Prints_Price`, `Prints`.`Format` AS `Prints_Format`, `Prints`.`Medium` AS `Prints_Medium`, `Prints`.`PriceID` AS `Prints_PriceID` FROM `rg_prints` AS `Prints` WHERE `Prints`.`PriceID` = 104 ORDER BY Size ASC

                It suggests to me that it is reading the right row from the Prints table ... Is there a way to return the values of those fields to make sure they are being retreived?

                The placeholders the ShopItems chunk uses are the same as the SQL query returns
                <table>
                <td>[[+Size]] [[+Format]] width print on [[+Medium]]</td>
                <td><h3>Price: <strong>AU$[[+Price]].00</strong></h3></td>
                </tr></table>


                The chunk outputs to the page, but the placeholder values are blank. The issue seems to be with getting values to the chunk, but it's beyond me.
                  • 3749
                  • 24,544 Posts
                  If you look at "AS" parts of the query, you can see that the placeholders need to be: Prints_Size, Prints_Format, etc.
                    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
                    • 25287
                    • 81 Posts
                    I have had no success as yet, despite lots of experimenting, searching, and more experimenting.

                    The snippet at the moment is:
                    $path = MODX_CORE_PATH . 'components/Prints/';
                    $result = $modx->addPackage('Prints',$path .
                        'model/','rg_');
                    
                    # get Price etc from 'rg_Prints' table
                    #
                    $PrintID = "0"; // clear the value
                    $PrintID = $_POST['IDprint'];
                    
                    $sortingcriteria = 'Size';
                    $filter1 = $PrintID;
                    $s = $modx->newQuery('Prints');
                    $s->sortby($sortingcriteria,'ASC');
                    $s->where(array(
                       'PriceID' => $filter1
                    ));
                    
                    #$s -> prepare();
                    #return 'SQL: ' .  $s->toSQL();
                    # commented out otherwise the code seems to abort at the previous line
                    
                    $prints  = $modx->getCollection('Prints',$s);
                    if (empty($prints)) {
                       return 'getCollection returned no objects';
                    }
                    
                    $output = '';
                    foreach ($prints as $photoprint) {
                        $fields = $photoprint->toArray();
                        $output = $output .$modx->getChunk('ShopItems', $fields);
                    }
                    
                    $output = print_r(array_keys($fields));
                    $output = $output .print_r(array_values($fields));
                    return $output;


                    Adding the final two lines enabled me to see that the correct record is being found and the fields and values loaded into the $fields array.
                    The resulting output is (with a line-break put in for easier reading)

                    Array ( [0] => id [1] => Edition [2] => Size [3] => Price [4] => Format [5] => Medium [6] => PriceID )
                    Array ( [0] => 31 [1] => Open [2] => 45 cm [3] => 55 [4] => Panoramic [5] => FujiFlex [6] => 103 ) 11

                    The chunk ShopItems is still not outputting values, even using [[+Prints_Size]] or [[+Prints]] as placeholder names.

                    I'm learning a bit about php, but not getting any success with this so far.
                      • 4172
                      • 5,888 Posts
                      to make your life easier,
                      first make your package-name lowercase

                      then install
                      MIGX

                      and install

                      getReqParam

                      Then try:

                      [[!migxLoopCollection?
                      &packageName=`prints`
                      &classname=`Prints`
                      &where=`{"PriceID":"[[!getReqParam? &name=`IDprint` &type=`POST`]]"}`
                      &tpl=`ShopItems`
                      ]]


                      use placeholders like

                      [[+Size]]
                      [[+Price]]
                      [[+Format]]


                      you can check the result, by removing the &tpl-property.
                        -------------------------------

                        you can buy me a beer, if you like MIGX

                        http://webcmsolutions.de/migx.html

                        Thanks!