We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Hi there, I have been very successfully following Bob's guide to creating Custom DB Tables at http://bobsguides.com/custom-db-tables.html, however, I seem to have hit a stumbling block when working with the edit snippet.

    I created the snippet EditQuote as code below and called

    [[EditQuote? &quoteId=`3`]] 


    Output was
    Could not find Quote with ID: 3


    This is the EditQuote snippet in its original form which gave the results above:

    <?php
    /* Get the existing Quote */
    $quoteId = $scriptProperties['quoteId'];
    $quote = $modx->getObject('Quotation', array('id'=>$quoteId));
    
    /* Show error message if quote is not found */
    if (empty($quote)) {
       return ('Could not find Quote with ID: ' . $quoteId);
    }
    
    if (isset($_POST['submit']) && $_POST['submit'] == 'MySubmitVar') {
       /* Form has been submitted */
    			
    
       $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;


    I then changed this to:

    <?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'); 
    /* Get the existing Quote */
    $quoteId = $scriptProperties['quoteId'];
    $quote = $modx->getObject('Quotation', array('id'=>$quoteId));
    
    /* Show error message if quote is not found */
    if (empty($quote)) {
       return ('Could not find Quote with ID: ' . $quoteId);
    }
    
    
    if (isset($_POST['submit']) && $_POST['submit'] == 'MySubmitVar') {
       /* Form has been submitted */
    
    
       $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;


    Which successfully outputs the quote which I can edit but it won't submit and save the change - where is this going wrong? And how would I potentially show all the quotes too?

    Great tutorial, just need this last bit so that I can start building my own custom tables.

    Many thanks in advance!
      Helen Warner
      Number one pixel!
      Proud to be linked with MODX at Crimson Pixel
      • 37686
      • 94 Posts
      Does quote id 3 exists? and, when you edit from original EditQuote, do you see any changes in the bobs_quotation table?
      • Yes, quote id 3 does exist and no the changes don't appear in the table.
          Helen Warner
          Number one pixel!
          Proud to be linked with MODX at Crimson Pixel
          • 37686
          • 94 Posts
          try changin MySubmitVar to submit, or whatever other value you gave your submit button's name
            • 37686
            • 94 Posts
            and use this template
            <form method="post" action="[[~[[*id]]]]">
            <input type="hidden" name="quoteId" value="[[+id]]" />
            <label for="topic">topic</label>
            <input type="text" name="topic" id="topic"  value="[[+topic]]" />
            <label for="quote">quote</label>
            <input type="text" name="quote" id="quote" value="[[+quote]]" />
            <label for="author">author</label>
            <input type="text" name="author" id="author"  value="[[+author]]" />
            <input type="submit" name="submit" id="submit" value="submit" />
            </form>
            
            • That works! The form chunk was the almost the same except with the addition in the label tag with the for ref. Have also changed the words in the EditQuote snippet from 'Quote created' to 'Quote amended' - if anyone else picks up this thread smiley

              <?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'); 
              /* Get the existing Quote */
              $quoteId = $scriptProperties['quoteId'];
              $quote = $modx->getObject('Quotation', array('id'=>$quoteId));
              
              /* Show error message if quote is not found */
              if (empty($quote)) {
                 return ('Could not find Quote with ID: ' . $quoteId);
              }
              
              
              if (isset($_POST['submit']) && $_POST['submit'] == 'submit') {
                 /* Form has been submitted */
              
              
                 $quote->set('topic', $_POST['topic']);
                 $quote->set('quote', $_POST['quote']);
                 $quote->set('author', $_POST['author']);
              
                 if ( $quote->save()) {
                     $output = "Quote Amended";
                 } 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;
                Helen Warner
                Number one pixel!
                Proud to be linked with MODX at Crimson Pixel