We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    Quote from: robb at Feb 24, 2011, 09:52 AM

    What I mean is the top portion of the code works for posting data to database but following the resto of the tutorial here:

    http://bobsguides.com/custom-db-tables.html

    is giving errors. This is the portion I cant seem to figure out.
     if ( $quote->save()) {
           $output = "Quote Created";
       } else {
           $output = "Error";
       }
        
     else {
        /* Not a repost, just display the form */
        $output = $modx->getChunk('quotationTpl');
    }
    return $output;
    
    


    The second "else" is missing its preceding curly brace. You’ve changed the indentation so it looks like that else goes with if ($quote->save()), but it doesn’t, it goes with the first "if" above that tests for a repost.

    Take another look at the example in my message and look at the how things are lined up and indented. If you always do it the same way, you’ll spot errors like this faster.

    A good code editor will help you a lot by putting the cursor in the right place when you press Enter, and, optionally, by creating and placing the matching braces. It will also reformat the entire snippet for you, or just parts of it. Beginners need a good code editor as much, if not more, than advanced users. NetBeans is a good free editor.
      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
      • 25226
      • 29 Posts
      Man, I was using Notepad++ and NetBeans is much better for noobies. I got the Posting to DB working perfectly(thanks) but now cant get the Edit code working. Using NetBeans I was able to fix the sytax issues to where it didnt error but now just need to select a record to edit.

      How would I create a list of database id labels to click on which would navigate to the form to edit the selected record?

      Edit Quote snippet
      /* EditQuote Snippet */
      
      /* Get the existing Quote */
      $path = MODX_CORE_PATH . 'components/quotes/';
      $result = $modx->addPackage('quotes', $path . 'model/', 'bobs_');
      $quote = $modx->getObject('Quotation', array('id' => $quoteId));
      
      if (isset($_POST['submit']) && ($_POST['submit'] == 'submit')) {
          /* Form has been submitted */
      
          $fields = array(
              'topic' => $_POST['topic'],
              'quote' => $_POST['quote'],
              'author' =>$_POST['author'],
          );
      
          if ($quote->save()) {
              $output = "Quote Created";
          } else {
              $output = "Error";
          }
      } else {
          /* Not a repost, just display the form */
      
          /* The second argument to getChunk() tells MODx to replace
           * the placeholders with the existing quote's values
           */
      
          $output = $modx->getChunk('quotationTpl', $quote->toArray());
      }
      return $output;




        • 3749
        • 24,544 Posts
        Something like this:

        <?php
        /* ShowQuotes snippet */
        $quotes = $modx->getCollection('Quotation');
        
        foreach($quotes as $quote) {
          
           $output .= $modx->getChunk('quoteTpl', $quote->toArray());
        
        }


        quoteTpl:
        Author: [[+author]]<br />
        Quote: [[+quote]]<br />
        Topic: [[+topic]]<br />
        <a href="[[~##]]?quoteId=[[+id]]">Edit</a><br />


        Where ## is the ID of the edit page. In the edit page snippet, the id will be available as $_GET[’quoteId’].
          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
          • 25226
          • 29 Posts
          Passing the id is working perfectly but the data is not being posted to the db on submit. The page just flashes and removes the changes I made to the fields. Im sure im just missing something stupid. I tried to be as detailed as possible to finally put this thread to rest. I believe this thread is going to help a lot of people as there nearly 600 views in less than 4 days. Thank you to everyone!

          The "EditQuote" snippet
          <?php
          /* EditQuote Snippet */
          
          /* Get the existing Quote */
          $path = MODX_CORE_PATH . 'components/quotes/';
          $result = $modx->addPackage('quotes', $path . 'model/', 'bobs_');
          $quote = $modx->getObject('Quotation', array('id'=>$_GET['quoteId']));
          
          if (isset($_POST['submit']) && ($_POST['submit'] == 'submit')) {
             /* Form has been submitted */
          
             $fields = array(
                'topic' => $_POST['topic'],
                'quote' => $_POST['quote'],
                'author' => $_POST['author'],
             );
             if ( $quote->save()) {
                 $output = "Quote Created";
             } else {
                 $output = "Error";
             }
             } else {
              /* Not a repost, just display the form */
          
              /* The second argument to getChunk() tells MODx to replace
               * the placeholders with the existing quote's values
               */
          
              $output = $modx->getChunk('quotationTpl', $quote->toArray());
             }
          return $output;
          
          


          the "quotationTpl" chunk

          <form method="post" action="[[~[[*id]]]]">  
          
                          <input type="text" name='topic' id='topic' value="[[+topic]]" />                
                          <input type="text" name='quote' id='quote' value="[[+quote]]" />
                          <input type="text" name='author' id='author' value="[[+author]]" />
                          <input type='submit' name='submit' id='submit' value='submit'/>
          
          </form>
          


          and the edit form resource page

          [[EditQuote? quoteId=`[[+id]]`]]


          The modifications made to "ShowQuotes" snippet

          <?php
          $path = MODX_CORE_PATH . 'components/quotes/';
          $result = $modx->addPackage('quotes', $path . 'model/', 'bobs_');
          if (!$result) {
              return 'failed to add package';
          } else {
              $output = '<p>added Package</p>';
          }
          
          $quotes = $modx->getCollection('Quotation');
          
          $output .= '<p>Total: ' . count($quotes) . '</p>';
          
          foreach ($quotes as $quote) {
          
             $output .= $modx->getChunk('quoteTpl', $quote->toArray());
            
          }
          
          return $output;


          and finally the "quoteTpl" chunk. The ~41 refers to my edit form resource page.

          Author: [[+author]]<br />
          Quote: [[+quote]]<br />
          Topic: [[+topic]]<br />
          <a href="[[~41]]?quoteId=[[+id]]">Edit</a><br />
            • 3749
            • 24,544 Posts
            When the editing form is submitted, it’s a new request and the $_GET value for the ID is not there any more. So the getObject() call fails and snippet has no object to save.

            A hidden field in the Tpl chunk for the edit page like this should help:

            <input type="hidden" name="quoteId" value="[[+id]]"  />


              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
              • 25226
              • 29 Posts
              Thank you, but no change. Still dont receive any errors, it just doesnt post to db on submit

              "quotationTpl" chunk
              
              <form method="post" action="[[~[[*id]]]]">  
                              <input type="hidden" name="quoteId" value="[[+id]]" />
                              <input type="text" name='topic' id='topic' value="[[+topic]]" />                
                              <input type="text" name='quote' id='quote' value="[[+quote]]" />
                              <input type="text" name='author' id='author' value="[[+author]]" />
                              <input type='submit' name='submit' id='submit' value='submit'/>
              
              </form>


              also want to be sure I have the snippet call in the edit form RESOURCE correct

              [[EditQuote? quoteId=`[[+id]]`]]
              
                • 3749
                • 24,544 Posts
                I wish I had time to just debug this myself and post the final version, but I’m dealing with the crash of my primary disk drive and can’t really do any work right now.

                I think what’s wrong is that on the second pass, the value is in the $_POST array rather than the $_GET array. The solution is to change it from $_GET to $_REQUEST.

                Try changing this:

                $quote = $modx->getObject('Quotation', array('id'=>$_GET['quoteId']));


                to this:

                $quote = $modx->getObject('Quotation', array('id'=>$_REQUEST['quoteId']));
                
                if (! $quote) {
                  return "Could not retrieve quotation. Request[quoteId] = " .  $_REQUEST['quoteId'];
                }
                
                
                  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
                  • 25226
                  • 29 Posts
                  Sorry, still no go. One observation is that when I select "edit" on a particlular record on "ShowQuotes". The editform that it navigates to has a url of

                  http://www.mywebsite.com/formedit.html?quoteId=1223


                  and then when I make my changes and it submit the url changes to

                  http://www.mywebsite.com/formedit.html
                  


                    • 11055 ☆ A M B ☆
                    • 3,112 Posts
                    <?php
                    /* EditQuote Snippet */
                    
                    // You might want to use this instead
                    $scriptProperties['quoteId'] = !empty((int) $_GET['quoteId']) ? (int) $_GET['quoteId'] : '';
                    if (empty($scriptProperties['quoteId'])) {
                        // or you want to echo out any text
                        return '';        
                    }
                    
                    /* Get the existing Quote */
                    $path = MODX_CORE_PATH . 'components/quotes/';
                    $result = $modx->addPackage('quotes', $path . 'model/', 'bobs_');
                    $quote = $modx->getObject('Quotation', array('id'=>$scriptProperties['quoteId']));
                    
                    if (isset($_POST['submit']) && ($_POST['submit'] == 'submit')) {
                        /**
                        * Try to see whether the field's values are empty or not, using:
                        * die(print_r($_POST));
                        */
                        
                        /* Form has been submitted */
                        // @todo sanitize the input values, this is IMPORTANT!
                        $quote->set('topic', $_POST['topic']);
                        $quote->set('quote', $_POST['quote']);
                        $quote->set('author', $_POST['author']);
                    
                        if ( $quote->save()) {
                          $output = "Quote Created";
                        } else {
                          $output = "Error";
                        }
                        
                    } else {
                        /* Not a repost, just display the form */
                    
                        /* The second argument to getChunk() tells MODx to replace
                         * the placeholders with the existing quote's values
                         */
                    
                        $output = $modx->getChunk('quotationTpl', $quote->toArray());
                     }
                     
                    return $output;
                    
                    


                    Look, please understand this:
                    - getObject is about getting the existing data.
                    you use set() because you want to UPDATE the existing content.
                    - newObject is about to add a new data
                    you use set() because you want to INSERT the new row

                    If you use set(), using save() is to finalize the action.
                      Rico
                      Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
                      MODx is great, but knowing how to use it well makes it perfect!

                      www.virtudraft.com

                      Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

                      Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

                      Maintainter/contributor of Babel

                      Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
                      • 25226
                      • 29 Posts
                      When I try your code(paste complete and replace existing) I get the following error.

                      Parse error: syntax error, unexpected T_INT_CAST in C:\wamp\www\modx\core\cache\elements\modsnippet\39.include.cache.php on line 10
                      


                      Please clarify if I was only supposed to use a portion of the code you provided. Am I supposed to remove anything from my snippet like
                      $quote = $modx->getObject('Quotation', array('id'=>$scriptProperties['quoteId']));
                      


                      thanks - Rob