We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4018
    • 1,131 Posts
    Got a little problem I’ve runned into while developing the Xinha plugin. I’m currently working on the configuration settings and I’m trying to use an array of checkboxes that will allow for the user to select which Xinha plugins they wish to appear in the editor. The checkboxes themselves are assigned a value of the name of the plugin and the name of the checkbox is the same for all checkboxes: xinha_plugin_sel[]. Obviously with the empty brackets, you can see I’m trying to create and store an array. However, it’s been my experience that before storing away the data for a variable in MySQL, you first have to either serialize it or turn the array into a string value first...which with MODx, there doesn’t appear to be a way to do this. So...my question is, is there a way around this within the plugin I’m working on? I’ve reached brainfry at the moment! LOL! I really need this to be an array since the choice of plugins will likely change from user to user so...need a checkbox array! AHHH!! wink

    L8R!

      Jeff Whitfield

      "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
      • 32963
      • 1,732 Posts
      Hi Jeff,

      Great idea smiley

      I think you can can try using the following:

      $ar = $_POST[’xinha_plugin_sel’];

      $v = implode(’,’,$ar);

      or

      $v = serialize($ar);
        xWisdom
        www.xwisdomhtml.com
        The fear of the Lord is the beginning of wisdom:
        MODx Co-Founder - Create and do more with less.
        • 4018
        • 1,131 Posts
        Ahh..speak of the devil! Funny...I just thought of this myself while looking at the save_settings.processor.php file. Question though...where in the plugin code do I need to put the implode line?
          Jeff Whitfield

          "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
          • 4018
          • 1,131 Posts
          Oh yeah...and I already tested this out and it works great. I went into /manager/processors/save_settings.processor.php and changed line 7 from:

          foreach ($_POST as $k => $v) {
          	$sql = "REPLACE INTO ".$modx->getFullTableName("system_settings")." (setting_name, setting_value) VALUES('".mysql_escape_string($k)."', '".mysql_escape_string($v)."')";
          	
          	if(!@$rs = mysql_query($sql)) {
          		echo "Failed to update setting value!";
          		exit;
          	}
          }
          


          to the following:

          foreach ($_POST as $k => $v) {
          	$v = is_array($v) ? implode(",", $v) : $v;
          	$sql = "REPLACE INTO ".$modx->getFullTableName("system_settings")." (setting_name, setting_value) VALUES('".mysql_escape_string($k)."', '".mysql_escape_string($v)."')";
          	
          	if(!@$rs = mysql_query($sql)) {
          		echo "Failed to update setting value!";
          		exit;
          	}
          }
          


          This works great an insures that any array is stored as a string. However, without it, I’m not sure how the plugin could pass off the $v variable since this is a loop. Perhaps the change I made could be worked into the core? This certainly would help alot with saving arrays into the site settings, not to mention giving us another way to handle site setting in various plugins. smiley However, I’m not sure if we want to use a simple comma in the implode...or would we prefer a different character? Then there’s the question of whether we want to allow for multi-dimensional arrays. Hmm...brings up some thoughts huh! LOL!
            Jeff Whitfield

            "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
            • 32963
            • 1,732 Posts
            Hmmm,

            Lets keep it real simple. We can use the comma and allow the develope the do the necessary escaping where necessary.
              xWisdom
              www.xwisdomhtml.com
              The fear of the Lord is the beginning of wisdom:
              MODx Co-Founder - Create and do more with less.
              • 4018
              • 1,131 Posts
              Cool...I’m all for keeping it simple. Single level array is good for now. We could always change it later if the need arises for multi-dimensional arrays. smiley So...would it be safe to assume that the new code will make it into the core? Cause in order for the Xinha plugin to continue, I need to be sure. Otherwise...I’ll still be stuck! smiley
                Jeff Whitfield

                "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
                • 32963
                • 1,732 Posts
                Sure it will. It’s a great idea to allow such settings.


                  xWisdom
                  www.xwisdomhtml.com
                  The fear of the Lord is the beginning of wisdom:
                  MODx Co-Founder - Create and do more with less.