1

I am creating an accessory that will access a table that I have created to update the value of a field. I have it set up so that if the value is 1 the page will be rendered and if the value is 0 the page will not be rendered. I have gotten everything down except getting the value to change via the access class.

What I currently have is a link that will execute an onclick to call another function within the accessory that evaluates the value of the field and then decides to change the value based on that.

Here is a sample of the code:

/**
 * The Blog Switch Accessory
 *
 * @package ' '
 * @author Jeremy Herb
 */
class Blog_switch_acc {
var $name         = 'Blog Switch';
var $id           = 'blog_acc';
var $version      = '1.0';
var $description  = 'This accessory will allow you to turn your blog page on and off';
var $sections     = array();
/**
* Constructor
*/
public function Blog_switch_acc()
{
$this->EE =& get_instance();
}

//onclick execute function to change value
public function set_sections()
{
$this->sections["On/Off Switch"]  = "
  <a  href='#' onclick='switcher()'>On/Off</a> 
  <p>To turn the blog on or off click the button above</p>";
}


public function switcher()
{
$query = $this->EE->db->query("SELECT blog_on_off FROM db_options");      
$set_to = $query == false;
$this->EE->db->query("UPDATE db_options SET blog_on_off = $set_to");
}
   }

/* End of file acc.blog_switch.php */       
/*Location:./system/expressionengine/third_party/blog_switch/acc.blog_switch.php*/ 

My apologies if it looks bad on the forum, this is my first post here.

Thanks, Jeremy Herb

1

Welcome Jeremy! You appear to be confusing PHP functions and javascript functions. In your "On/Off Switch" accessory code, you're calling a javascript function with the onclick event. For that to call your switcher() PHP function, you'd need to add more javascript that will make that click an AJAX call to your accessory method which runs via PHP on the server.

You can do this with accessories by using the process_ method. So, you'd rename switcher() to process_switcher(), then make an AJAX call to C=addons_accessories&M=process_request&accessory=blog_switch&method=process_switcher.

Also, your code inside of the switcher() function does not follow EE code guidelines. It should be more like this:

$query = $this->EE->db->query("SELECT blog_on_off FROM db_options LIMIT 1");      
if($query->num_rows() == 1)
{
    $set_to = ($query->row('blog_on_off') == false) ? 1 : 0;
    $this->EE->db->query("UPDATE db_options SET blog_on_off = $set_to");
}

(Review the EE database class documentation here.)

3
  • Awesome, I will try this as soon as I can. Thank you! – Jeremy Herb Feb 7 '13 at 12:15
  • I have one problem. How do I return to the control panel after the function has executed. Everything is working properly. The function updates the database but leaves the control panel. – Jeremy Herb Feb 12 '13 at 13:50
  • This should be executing via AJAX, so you shouldn't be leaving the control panel at all. However, you can always use $this->EE->functions->redirect(). (Docs.) – Derek Hogue Feb 12 '13 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.