1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How do I approach this data writer?

Discussion in 'XenForo Development Discussions' started by TheBigK, Sep 1, 2015.

  1. TheBigK

    TheBigK Well-Known Member

    I need help in making this data writer work. Here's what I have.

    $department_name $this->_input->filterSingle('department_name'XenForo_Input::STRING, array('array' => true));
    $department_email $this->_input->filterSingle('department_email'XenForo_Input::STRING, array('array' => true));
    $dw XenForo_DataWriter::create('ContactUs_DataWriter_Contact');

    //The following approach, I think is wrong. 
    for ($i 0$i <= sizeof($department_name); $i++)
    The idea is to set both $department_name and corresponding $department_email at a time. I'm however constantly getting the following error -

    Undefined offset: 1
    1. XenForo_Application::handlePhpError() in ContactUs/ControllerAdmin/Index.php at line 34
    2. ContactUs_ControllerAdmin_Index->actionAdd() in XenForo/FrontController.php at line 347
    3. XenForo_FrontController->dispatch() in XenForo/FrontController.php at line 134
    4. XenForo_FrontController->run() in /Users/CE-Mac-Two/Sites/xf/admin.php at line 13
  2. katsulynx

    katsulynx Well-Known Member

    $department_name $this->_input->filterSingle('department_name'XenForo_Input::ARRAY_SIMPLE);
    $department_email $this->_input->filterSingle('department_email'XenForo_Input::ARRAY_SIMPLE);

    $department_name as $key => $name) {
    $dw XenForo_DataWriter::create('ContactUs_DataWriter_Contact');
    'department_name' => $name,
    'department_email' => $department_email[$key]
    Or you might merge those two arrays together first.
  3. TheBigK

    TheBigK Well-Known Member

    Thanks @katsulynx . I've a question however: Wouldn't it be better if the datawriter instance is created before the foreach loop starts?
  4. Mr. Goodie2Shoes

    Mr. Goodie2Shoes Well-Known Member

    @TheBigK your approach wasn't entirely wrong since the index of the inputs are numerical. The for loop you used, changing '<=' to '<' in the condition will fix the error you are getting...
    But yes! Merging the arrays first is the way to go. :D

    The reason for change is that for an array with 'sizeof' 6, the last index is 5 (n - 1, since index starts from 0)
  5. TheBigK

    TheBigK Well-Known Member

    Thanks. I haven't seen anyone using a for loop; so I wasn't very sure. Why is merging the arrays a recommended approach?

Share This Page