We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26182
    • 164 Posts
    Hi,

    I am in the process of designing a snippet that makes a newsletter from articles that have been posted to my modx site.

    I am not too sure how to handle the form processing aspect of it.

    This is the code I have so far:

    <?php
    /*
     ****************************************
     *
     * Snippet Name: MakeNewsletter
     * Short Desc: Makes a newsletter from articles stored in the database
     * Created By: Sarah Vardy
     *
     * Version: 1.0 beta
     * Modified: November 9th, 2007
     *
     * Changelog: 
     *   
     *
     ****************************************
     *
     * Description: 	
     *   Display a list of articles that have been posted to the site
     *   Shows a drop-down list next to each article so the user can select and article and a relevant weight for inclusion in the IT newsletter
     *
     ****************************************
     *  
     * Example: [[MakeNewsletter?]]
    */
    $output = '';
     
    //connect to table that stores site content
    $table_content = $modx->getFullTableName("site_content");
    
    //connect to the table that stores web user data
    $table_users= $modx->getFullTableName("web_users");
    
    //fields to get from query
    $fields = 'id,pagetitle,createdby,createdon';
    
    //$sql= "SELECT {$fields} FROM {$table_content} table_content JOIN {$table_users} table_users ON table_users.id = table_content.createdby  WHERE table_content.parent= '48' AND table_content.published = '1' ORDER BY table_content.createdon DESC LIMIT 3";
    $sql= "SELECT {$fields} FROM {$table_content} table_content  WHERE table_content.parent= '48' AND table_content.published = '1' ORDER BY table_content.createdon DESC LIMIT 3";
    $results= $modx->db->query($sql);
    
    $output .='<form>';
    $output .='<table>';
    
    while ($row=$modx->db->getRow($results)) {
    
      $output .='<tr><td><label><input type="checkbox" name="checkbox" value="' . $row[id] . '" /> Include this</label></td>';
      $output .='<td width="100px"><label>Weight <select name="select"><option>-10</option><option>-9</option><option>-8</option><option>-7</option><option>-6</option><option>-5</option>
        <option>-4</option><option>-3</option><option>-2</option><option>-1</option><option>0</option><option>1</option><option>2</option><option>3</option><option>4</option>
        <option>5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option></select>';
      $output .= '<td><a href="index.php?id=' . $row[id] . '">' . $row[pagetitle] . '</a>' . "<br>" . '</td></tr>';
    
    }
    $output .='</table>';
    
    $output .='<label><input type="submit" name="Submit" value="Make newsletter" /></label>';
    $output .='</form>';
    return $output;
    
    ?>


    Any help or suggestions would be great.

    Thanks in advance,

    Sarah
      • 29076
      • 615 Posts
      Could this help mabye: http://modxcms.com/Newsletter-574.html?
        I think, thererfor I am! But what I am, and why...?
        • 26182
        • 164 Posts
        thanks for that but unfortunately it is lacking the functionality out of the box. i might be able to modify it in some way but i want to be able to just get documents from the current month - compare the timestamp in the createdby col to the server time. i also want to extend it so the parameter for the parent folder can be set by the user in the snippet call.

        i am just a little bit tied up about the way i can send a form using dynamic elements but i’m sure it’s nothing too hard to figure out.

        thanks again!

        sarah
          • 26182
          • 164 Posts
          Does anyone know if this is the right way to be going about it?

          I can’t seem to get the form behaving exactly like I want it to. Ive made a new table in the database for the sake of testing called modx_rcs_it_newsletters I’ve commented out the final function to process the form as it stops the entire snippet from working... does anyone know how to get this working?

          <?php
          /*
           ****************************************
           *
           * Snippet Name: MakeNewsletter
           * Short Desc: Makes a newsletter from articles stored in the database
           * Created By: Sarah Vardy
           *
           * Version: 1.0 beta
           * Modified: November 9th, 2007
           *
           * Changelog: 
           *   
           *
           ****************************************
           *
           * Description: 	
           *   Display a list of articles that have been posted to the site
           *   Shows a drop-down list next to each article so the user can select and article and a relevant weight for inclusion in the IT newsletter
           *
           ****************************************
           *  
           * Example: [[MakeNewsletter?]]
          */
          
          $months = array(January,February,March,April,May,June,July,August,September,October,November,December);
          
          // getting current timestamp
          if(!isset($format)) {
              $format="%m";
          }
          if(!isset($timestamp)) {
              $timestamp=time();
          }
          
          // getting current timestamp
          if(!isset($format_two)) {
              $format_two="%Y";
          }
          
          $currentMonth = strftime($format, $timestamp+$modx->config['server_offset_time']);
          $currentYear = strftime($format_two, $timestamp+$modx->config['server_offset_time']);
          
          $output = '';
           
          //connect to table that stores site content
          $table_content = $modx->getFullTableName("site_content");
          
          //connect to the table that stores web user data
          $table_users= $modx->getFullTableName("web_users");
          
          //fields to get from query
          $fields = 'id,pagetitle,createdby,createdon';
          
          //$sql= "SELECT {$fields} FROM {$table_content} table_content JOIN {$table_users} table_users ON table_users.id = table_content.createdby  WHERE table_content.parent= '48' AND table_content.published = '1' ORDER BY table_content.createdon DESC LIMIT 3";
          $sql= "SELECT {$fields} FROM {$table_content} table_content  WHERE table_content.parent= '48' AND table_content.published = '1' ORDER BY table_content.createdon DESC LIMIT 3";
          $results= $modx->db->query($sql);
          
          $output .='<form>';
          $output .='<table>';
          
          while ($row=$modx->db->getRow($results)) {
          
          $tempMonth = strftime($format, $row[createdon]);
          $tempYear = strftime($format_two, $row[createdon]);
          
          //if the current entry is the current month display it
          if ($tempMonth == $currentMonth && $tempYear == $currentYear)
          {
            $output .='<tr><td><label><input type="checkbox" name="checkbox" value="' . $row[id] . '" /> Include this</label></td>';
            $output .='<td width="100px"><label>Weight <select name="select"><option>-10</option><option>-9</option><option>-8</option><option>-7</option><option>-6</option><option>-5</option>
              <option>-4</option><option>-3</option><option>-2</option><option>-1</option><option>0</option><option>1</option><option>2</option><option>3</option><option>4</option>
              <option>5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option></select>';
            $output .= '<td><a href="index.php?id=' . $row[id] . '">' . $row[pagetitle] . '</a>' . "<br>" . '</td></tr>';
              $tempMonth ='';
          
          //else do not display it
          }else
          {
             $tempMonth ='';
          }
          
          }
          $output .='</table>';
          
          $output .='<label><input type="submit" name="make" value="Make newsletter for ' . $months[$currentMonth -1] . ',' . $currentYear . '" /></label>';
          $output .='</form>';
          
          /*
          if(isset($_POST['make']))
            {
                $checkbox = $_POST['checkbox'];
                $select = $_POST['select'];
          
                $sql = "INSERT INTO ".$modx->getFullTableName('rcs_it_newsletters')." (doc_id, doc_weight) VALUES ('".$checkbox."' , '"$select"')";
                $rs = $modx->db->query($sql);
                $id = $modx->db->getInsertId();
          
                 //include 'library/closedb.php';
          
                 echo "Article '$checkbox' added with weight of '$select'";
          
              }
          */
          
          return $output;
          ?>
            • 26182
            • 164 Posts
            I’ve decided I’m going to write a form processing script that I’ll upload to the server and then I can have the action of the form to be directed to this document.

            Oh well, it’s worth a try. I’ll see how I go.

            :)

            Please don’t hesitate to contact me if you know a better way of doing this...
              • 26182
              • 164 Posts
              I’ve made 2 snippets. This is just something I’ve whipped up pretty quickly. But I’m having a problem - only one entry is being sent to the database. How do I fix this?

              I’ve made a table in the database to store the info. This is just for testing at the moment. The snippet will hopefully eventually create a table for the user automatically.


              This is my first snippet to display the form stuff to select the articles.

              <?php
              /*
               ****************************************
               *
               * Snippet Name: MakeNewsletter
               * Short Desc: Makes a newsletter from articles stored in the database
               * Created By: Sarah Vardy
               *
               * Version: 0.1 beta
               * Modified: November 13th, 2007
               *
               * Changelog: 
               *   
               *
               ****************************************
               *
               * Description: 	
               *   Display a list of articles that have been posted to the site
               *   Shows a drop-down list next to each article so the user can select and article and a relevant weight for inclusion in the IT newsletter
               *
               ****************************************
               *  
               * Example: [[MakeNewsletter?]]
              */
              
              $months = array(January,February,March,April,May,June,July,August,September,October,November,December);
              
              // getting current timestamp
              if(!isset($format)) {
                  $format="%m";
              }
              if(!isset($timestamp)) {
                  $timestamp=time();
              }
              
              // getting current timestamp
              if(!isset($format_two)) {
                  $format_two="%Y";
              }
              
              $currentMonth = strftime($format, $timestamp+$modx->config['server_offset_time']);
              $currentYear = strftime($format_two, $timestamp+$modx->config['server_offset_time']);
              
              $output = '';
               
              //connect to table that stores site content
              $table_content = $modx->getFullTableName("site_content");
              
              //connect to the table that stores web user data
              $table_users= $modx->getFullTableName("web_users");
              
              //fields to get from query
              $fields = 'id,pagetitle,createdby,createdon';
              
              //query mySQL database
              $sql= "SELECT {$fields} FROM {$table_content} table_content  WHERE table_content.parent= '48' AND table_content.published = '1' ORDER BY table_content.createdon DESC LIMIT 3";
              $results= $modx->db->query($sql);
              
              while ($row=$modx->db->getRow($results)) {
              
              $tempMonth = strftime($format, $row[createdon]);
              $tempYear = strftime($format_two, $row[createdon]);
              
              $output .='<form action="index.php?id=99" method="post">';
              $output .='<table>';
              
                 //if the current entry is the current month display it
                 if ($tempMonth == $currentMonth && $tempYear == $currentYear)
                 {
              
                $output .='<tr><td><label><input type="checkbox" name="doc_id" value="' . $row[id] . '" /> Include this</label></td>';
                $output .='<td width="100px"><label>Weight <select name="doc_weight"><option>-10</option><option>-9</option><option>-8</option><option>-7</option><option>-6</option><option>-5</option>
                  <option>-4</option><option>-3</option><option>-2</option><option>-1</option><option>0</option><option>1</option><option>2</option><option>3</option><option>4</option>
                  <option>5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option></select>';
                $output .= '<td><a href="index.php?id=' . $row[id] . '">' . $row[pagetitle] . '</a>' . "<br>" . '</td>';
              
                  $tempMonth ='';
              
                 //else do not display it
                 }
                 else
                 {
                 $tempMonth ='';
                 }
              
               
              
              }
              
              $output .='</table>';
              $output .='<input type="hidden" name="doc_month" value="' . $currentMonth . '">';
              $output .='<input type="hidden" name="doc_year" value="' . $currentYear . '">';
              $output .='<label><input type="submit" name="make" value="Make newsletter for ' . $months[$currentMonth -1] . ',' . $currentYear . '" /></label>';
              
              $output .='</form>';
              
              return $output;
              ?>



              This is my second snippet that process the form entries:

              <?php
              #
              # Snippet MakeNewsletter v 0.1 beta
              # By sarah Vardy 
              # This release: 13-Nov-2007.
              #
              # Processes form entry fields from MakeNewsletter snippet
              #
              # Warning! This add-on might not be ready for public use
              #
              
              //global variables
              //global $modx, $table_prefix;
              $table = $modx->getFullTableName("rcs_it_newsletters");
              
              //get values from post and insert them into table in database
              
              $doc_id=$_POST['doc_id'];
              $doc_weight=$_POST['doc_weight'];
              $doc_month=$_POST['doc_month'];
              $doc_year=$_POST['doc_year'];
              
              
              mysql_query("INSERT INTO $table VALUES ('$doc_id', '$doc_weight', '$doc_month', '$doc_year')");
              
              print "Your information has been successfully added to the database.";
              
              print $doc_id;
              print $doc_weight;
              print $doc_month;
              print $doc_year;
              ?>


              It’s only getting the last entry and storing that in the database table.

              Please help!


              Sarah
                • 26182
                • 164 Posts
                Hi Ganesh,

                I managed to get it working, but now I’m having problems with working out where I should check if the entry exists in the custom table I’ve created to store newsletter entries.

                This is the code as it stands:

                <?php
                /*
                 ****************************************
                 *
                 * Snippet Name: MakeNewsletter
                 * Short Desc: Makes a newsletter from articles stored in the database
                 * Created By: Sarah Vardy
                 *
                 * Version: 0.1 beta
                 * Modified: November 13th, 2007
                 *
                 * Changelog: 
                 *   
                 *
                 ****************************************
                 *
                 * Description: 	
                 *   Display a list of articles that have been posted to the site
                 *   Shows a drop-down list next to each article so the user can select and article and a relevant weight for inclusion in the IT newsletter
                 *
                 ****************************************
                 *TO-DO
                 * Check the custom newsletter table to see if an article is already being used in an existing newsletter before displaying it to be included in a newsletter
                 ****************************************
                 *  
                 * Example: [[MakeNewsletter?]]
                */
                global $currentCounter;
                $counter =1;
                
                $months = array(January,February,March,April,May,June,July,August,September,October,November,December);
                
                // getting current timestamp
                if(!isset($format)) {
                    $format="%m";
                }
                if(!isset($timestamp)) {
                    $timestamp=time();
                }
                
                // getting current timestamp
                if(!isset($format_two)) {
                    $format_two="%Y";
                }
                
                $currentMonth = strftime($format, $timestamp+$modx->config['server_offset_time']);
                $currentYear = strftime($format_two, $timestamp+$modx->config['server_offset_time']);
                
                $output = '';
                 
                //connect to table that stores site content
                $table_content = $modx->getFullTableName("site_content");
                
                //connect to the table that stores web user data
                $table_users= $modx->getFullTableName("web_users");
                
                //connect to custom table that stores newsletter documents
                $table_newsletters = $modx->getFullTableName("rcs_it_newsletters");
                
                //fields to get from query
                $fields = 'id,pagetitle,createdby,createdon';
                
                //query mySQL database
                $sql= "SELECT {$fields} FROM {$table_content} table_content  WHERE table_content.parent= '48' AND table_content.published = '1' ORDER BY table_content.createdon DESC LIMIT 3";
                $results= $modx->db->query($sql);
                
                while ($row=$modx->db->getRow($results)) {
                
                $tempMonth = strftime($format, $row[createdon]);
                $tempYear = strftime($format_two, $row[createdon]);
                
                $output .='<form action="index.php?id=99" method="post">';
                $output .='<table>';
                
                   //if the current entry is the current month display it
                   if ($tempMonth == $currentMonth && $tempYear == $currentYear)
                   {
                   //------------------------------------------DO I DO THIS HERE?????
                   //if the existing entry does not already belong in the rcs_it_newsletters table display it
                
                  $docIdTag = 'doc_id_';
                  $docWeightTag = 'doc_weight_';
                
                  $output .='<tr><td><label><input type="checkbox" name="' . $docIdTag . $counter . '" value="' . $row[id] . '" /> Include this</label></td>';
                  $output .='<td width="100px"><label>Weight <select name="' . $docWeightTag . $counter . '"><option>-10</option><option>-9</option><option>-8</option><option>-7</option><option>-6</option><option>-5</option>
                    <option>-4</option><option>-3</option><option>-2</option><option>-1</option><option>0</option><option>1</option><option>2</option><option>3</option><option>4</option>
                    <option>5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option></select>';
                  $output .= '<td><a href="index.php?id=' . $row[id] . '">' . $row[pagetitle] . '</a>' . "<br>" . '</td>';
                  $counter++;
                  $tempMonth ='';
                
                   //else do not display it
                   }
                   else
                   {
                   $tempMonth ='';
                   }
                
                 //return $counter;
                
                }
                
                $output .='</table>';
                
                //$output .='<input type="hidden" name="doc_counter" value="' . $currentCounter . '">';
                $output .='<input type="hidden" name="doc_month" value="' . $currentMonth . '">';
                $output .='<input type="hidden" name="doc_year" value="' . $currentYear . '">';
                $output .='<label><input type="submit" name="make" value="Make newsletter for ' . $months[$currentMonth -1] . ',' . $currentYear . '" /></label>';
                $output .='</form>';
                
                if ($counter >= '20')
                {
                   return $output;
                }
                else if ($counter < '20')
                {
                   print '<strong><font size="3">THERE ARE NOT ENOUGH ARTICLES TO MAKE A FULL NEWSLETTER YET!</font></strong>' . "<br>" . 'You have a total of <strong><font size="3">' . $counter . ' articles</font></strong>';
                }
                ?>


                I need to work out either an amendment to the sql query to check if the entry already exists in the custom made table for storing newsletters or if I scour through this custom table and check to see if a doc with the same id exists. I’m not sure which is the more economical.

                And then I have another snippet to process the form entries:

                <?php
                #
                # Snippet MakeNewsletter v 0.1 beta
                # By Sarah Vardy 
                # This release: 13-Nov-2007.
                #
                # Processes form entry fields from MakeNewsletter snippet
                #
                # Warning! This add-on might not be ready for public use
                #
                
                //global variables
                global $modx, $table_prefix;
                $table = $modx->getFullTableName("rcs_it_newsletters");
                
                //while (false)
                for ($counter =0; $counter < 20; $counter ++)
                {
                    if (isset($_POST['doc_id_'.$counter])) 
                    {
                     print 'Got here!';
                     print $val_count;
                     $val_id = $_POST['doc_id_'.$counter];
                     $val_weight = $_POST['doc_weight_'.$counter];
                     $val_month=$_POST['doc_month'];
                     $val_year=$_POST['doc_year'];
                     // do something with val
                     print $val_id;
                     print $val_weight;
                     mysql_query("INSERT INTO $table VALUES ('$val_id', '$val_weight', '$val_month', '$val_year')");
                     }
                     //$counter++;
                 }
                
                
                print "Your information has been successfully added to the database.";
                ?>


                Any help would be great.

                :) sarah

                  • 26182
                  • 164 Posts
                  Ok there’s something screwy about my logic. It’s only printing out the first entry that matches.

                  Can anyone please please help?

                  <?php
                  /*
                   ****************************************
                   *
                   * Snippet Name: MakeNewsletter
                   * Short Desc: Makes a newsletter from articles stored in the database
                   * Created By: Sarah Vardy
                   *
                   * Version: 0.1 beta
                   * Modified: November 13th, 2007
                   *
                   * Changelog: 
                   *   
                   *
                   ****************************************
                   *
                   * Description: 	
                   *   Display a list of articles that have been posted to the site
                   *   Shows a drop-down list next to each article so the user can select and article and a relevant weight for inclusion in the IT newsletter
                   *
                   ****************************************
                   *TO-DO
                   * Check the custom newsletter table to see if an article is already being used in an existing newsletter before displaying it to be included in a newsletter
                   ****************************************
                   *  
                   * Example: [[MakeNewsletter?]]
                  */
                  global $currentCounter;
                  $counter =1;
                  $docIdArray = array();
                  $counter_two =0;
                  
                  $months = array(January,February,March,April,May,June,July,August,September,October,November,December);
                  
                  // getting current timestamp
                  if(!isset($format)) {
                      $format="%m";
                  }
                  if(!isset($timestamp)) {
                      $timestamp=time();
                  }
                  
                  // getting current timestamp
                  if(!isset($format_two)) {
                      $format_two="%Y";
                  }
                  
                  $currentMonth = strftime($format, $timestamp+$modx->config['server_offset_time']);
                  $currentYear = strftime($format_two, $timestamp+$modx->config['server_offset_time']);
                  
                  $output = '';
                   
                  //connect to table that stores site content
                  $table_content = $modx->getFullTableName("site_content");
                  
                  //connect to the table that stores web user data
                  $table_users= $modx->getFullTableName("web_users");
                  
                  //connect to custom table that stores newsletter documents
                  $table_newsletters = $modx->getFullTableName("rcs_it_newsletters");
                  
                  //fields to get from query
                  $fields = 'id,pagetitle,createdby,createdon';
                  $fields_two = 'doc_id';
                  
                  //query mySQL database
                  $sql= "SELECT {$fields} FROM {$table_content} table_content  WHERE table_content.parent= '48' AND table_content.published = '1' ORDER BY table_content.createdon DESC LIMIT 3";
                  $results= $modx->db->query($sql);
                  
                  $sql_two= "SELECT {$fields_two} FROM {$table_newsletters} table_newsletters";
                  $results_two= $modx->db->query($sql_two);
                  
                  //store the results of the doc_id in an array 
                  while ($row_two=$modx->db->getRow($results_two))
                  {
                     //store the value in the array
                     $docIdArray[$counter_two] = $row_two[doc_id];
                     //print $docIdArray[$counter_two];
                     $counter_two++;
                  }
                  
                  
                  while ($row=$modx->db->getRow($results)) {
                  
                     $tempMonth = strftime($format, $row[createdon]);
                     $tempYear = strftime($format_two, $row[createdon]);
                     $tempId = $row[id];
                  
                     $output .='<form action="index.php?id=99" method="post">';
                     $output .='<table>';
                    
                  
                        //if the current entry is the current month display it
                        if ($tempMonth == $currentMonth && $tempYear == $currentYear)
                        {
                  
                        //iterate through the $docIdArray
                        for($i = 0; $i < count($docIdArray); $i++)
                        {  
                             //If the id of the current document is found 
                            $tempArrayIDStore = $docIdArray[$i];
                             
                            if($tempArrayIDStore == $tempId)
                            { 
                                //Do not display the document and go back to the statement that called this one
                                $output .= 'Resource with id '. $tempId . ' has already been used!';
                                $output .= "<br>";
                                $tempId = '';
                                break;
                  
                             }
                             else if($tempId != $tempArrayIDStore)
                             { 
                                //else display the document 
                                $docIdTag = 'doc_id_';
                                $docWeightTag = 'doc_weight_';
                  
                                $output .='<tr><td><label><input type="checkbox" name="' . $docIdTag . $counter . '" value="' . $row[id] . '" /> Include this</label></td>';
                  
                                $output .='<td width="100px"><label>Weight <select name="' . $docWeightTag . $counter . '"><option>-10</option><option>-9</option><option>-8</option><option>-7</option><option>-6</option><option>-5</option>
                                <option>-4</option><option>-3</option><option>-2</option><option>-1</option><option>0</option><option>1</option><option>2</option><option>3</option><option>4</option>
                                <option>5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option></select>';
                                $output .= '<td><a href="index.php?id=' . $row[id] . '">' . $row[pagetitle] . '</a>' . "<br>" . '</td>';
                  
                               
                                $counter++;
                                $tempMonth ='';
                                break;
                             }
                  
                  
                        }
                  
                              
                  
                     }//end if statement
                     //else do not display it
                     else
                     {
                  
                  
                     }//end else statement
                  
                  
                  }//end while statement
                  
                  $output .='</table>';
                  
                  $output .='<input type="hidden" name="doc_month" value="' . $currentMonth . '">';
                  $output .='<input type="hidden" name="doc_year" value="' . $currentYear . '">';
                  $output .='<label><input type="submit" name="make" value="Make newsletter for ' . $months[$currentMonth -1] . ',' . $currentYear . '" /></label>';
                  $output .='</form>';
                  
                  //if ($counter >= '20')
                  //{
                     return $output;
                  //}
                  //else if ($counter < '20')
                  //{
                     //print '<strong><font size="3">THERE ARE NOT ENOUGH ARTICLES TO MAKE A FULL NEWSLETTER YET!</font></strong>' . "<br>" . 'You have a total of <strong><font size="3">' . $counter . ' articles</font></strong>';
                  //}
                  ?>
                    • 26182
                    • 164 Posts
                    Ok so I’ve slightly changed my plan.

                    I’m now storing the doc_ids and doc_weights as arrays in the database, except I can’t get them to write to the database using the serialise() function.

                    I still haven’t successfully changed the first snippet to check for entries in the database that are already existing but I’ll look at this later.

                    Here’s the first snippet:

                    <?php
                    /*
                     ****************************************
                     *
                     * Snippet Name: MakeNewsletter
                     * Short Desc: Makes a newsletter from articles stored in the database
                     * Created By: Sarah Vardy
                     *
                     * Version: 0.1 beta
                     * Modified: November 14th, 2007
                     *
                     * Changelog: 
                     *   
                     *
                     ****************************************
                     *
                     * Description: 	
                     *   Display a list of articles that have been posted to the site
                     *   Shows a drop-down list next to each article so the user can select and article and a relevant weight for inclusion in the IT newsletter
                     *
                     ****************************************
                     *TO-DO
                     * Check the custom newsletter table to see if an article is already being used in an existing newsletter before displaying it to be included in a newsletter
                     ****************************************
                     *  
                     * Example: [[MakeNewsletter?]]
                    */
                    global $currentCounter;
                    $counter =1;
                    $docIdArray = array();
                    $counter_two =0;
                    
                    $months = array(January,February,March,April,May,June,July,August,September,October,November,December);
                    
                    // getting current timestamp
                    if(!isset($format)) {
                        $format="%m";
                    }
                    if(!isset($timestamp)) {
                        $timestamp=time();
                    }
                    
                    // getting current timestamp
                    if(!isset($format_two)) {
                        $format_two="%Y";
                    }
                    
                    $currentMonth = strftime($format, $timestamp+$modx->config['server_offset_time']);
                    $currentYear = strftime($format_two, $timestamp+$modx->config['server_offset_time']);
                    
                    $output = '';
                     
                    //connect to table that stores site content
                    $table_content = $modx->getFullTableName("site_content");
                    
                    //fields to get from query
                    $fields = 'id,pagetitle,createdby,createdon';
                    
                    //query mySQL database
                    $sql= "SELECT {$fields} FROM {$table_content} table_content  WHERE table_content.parent= '48' AND table_content.published = '1' ORDER BY table_content.createdon DESC LIMIT 3";
                    $results= $modx->db->query($sql);
                    
                    while ($row=$modx->db->getRow($results)) {
                    
                       $tempMonth = strftime($format, $row[createdon]);
                       $tempYear = strftime($format_two, $row[createdon]);
                       $tempId = $row[id];
                    
                       $output .='<form action="index.php?id=99" method="post">';
                       $output .='<table>';
                    
                          //if the current entry is the current month display it
                         while ($tempMonth == $currentMonth && $tempYear == $currentYear)
                          {
                                  //display the document 
                                  $docIdTag = 'doc_id_';
                                  $docWeightTag = 'doc_weight_';
                    
                                  $output .='<tr><td><label><input type="checkbox" name="' . $docIdTag . $counter . '" value="' . $row[id] . '" /> Include this</label></td>';
                    
                                  $output .='<td width="100px"><label>Weight <select name="' . $docWeightTag . $counter . '"><option>-10</option><option>-9</option><option>-8</option><option>-7</option><option>-6</option><option>-5</option>
                                  <option>-4</option><option>-3</option><option>-2</option><option>-1</option><option>0</option><option>1</option><option>2</option><option>3</option><option>4</option>
                                  <option>5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option></select>';
                                  $output .= '<td><a href="index.php?id=' . $row[id] . '">' . $row[pagetitle] . '</a>' . "<br>" . '</td>';
                    
                                 
                                  $counter++;
                                  $tempMonth ='';
                          }
                    
                    }//end while statement
                    
                    $output .='</table>';
                    
                    
                    $output .='<label><input type="submit" name="make" value="Make newsletter for ' . $months[$currentMonth -1] . ',' . $currentYear . '" /></label>';
                    $output .='</form>';
                    
                    return $output;
                    ?>


                    Here’s the second with the serialise function:

                    <?php
                    #
                    # Snippet MakeNewsletter v 0.1 beta
                    # By Sarah Vardy 
                    # This release: 13-Nov-2007.
                    #
                    # Processes form entry fields from MakeNewsletter snippet
                    #
                    # Warning! This add-on might not be ready for public use
                    #
                    
                    //global variables
                    global $modx, $table_prefix;
                    $table = $modx->getFullTableName("rcs_it_newsletters");
                    
                    //while (false)
                    /*for ($counter =0; $counter < 20; $counter ++)
                    {
                    */
                    
                        if (isset($_POST['doc_id'])) 
                        {
                    
                         //$val_weight = $_POST['doc_weight_'.$counter];
                    
                         $val_id=serialize($_POST['doc_id']);
                         $val_weight=serialize($_POST['doc_weight']);
                    
                         //$val_month=$_POST['doc_month'];
                         //$val_year=$_POST['doc_year'];
                    
                         //$val_id = $_POST['doc_id_'.$counter];
                    
                         // do something with val
                         print $val_id;
                         print $val_weight;
                         mysql_query("INSERT INTO $table VALUES ('$val_id', '$val_weight')");
                         }
                    
                         //$counter++;
                     //}
                    
                    
                    print "Your information has been successfully added to the database.";
                    ?>
                      • 26182
                      • 164 Posts
                      I have modified the snippet and have it writing to the database as an array using serialize and getting the array back using unserialize. What a cool function! Have to make some mods so that documents that are already in the database are not displayed on the initial form or so that they are somehow marked to indicate that they have been used and so their weighting can be adjusted.

                      Also looking at another snippet that will compile all the entries in the table newsletters and display each newsletter.