• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Corrupted Array

#1
[eluser]Ondrej[/eluser]
Edit: Nevermind, I found a ugly, but working workaround through if/else - I've $this->input->post('categories', array()), but it didn't work. However, If I manually assign array(), it works just fine.

Hey guys,

I'm not sure what's causing it, but it seems my array gets corrupted for no apparent reason.
Basically, I've got two functions in my controller, which display the same form.

Code:
public function news_add_post()
{
    $this->checkLogin();                    
        
    $headerData = array();
    $headerData['title'] = "Novinky - Pridať";
    $headerData['js_files'] = array('jquery.markitup.js','news_form.js',
                                         'sets/bbcode/set.js');
        
        
    $miscData = array();
        
    // Get languages        
    $miscData['languages'] = $this->admin_model->getLanguages();
    $miscData['categories'] = $this->admin_model->getNewsCategories();
        
    $miscData['form_header'] = "Pridať príspevok";
    $miscData['form_action'] = "admin/news/add";
    $miscData['date'] = date("Y-m-d H-i-s");
        
    $data = array();
    $data['headerData'] = $headerData;
    $data['miscData'] = $miscData;

    if(/* Checks whether there is POST data */)
    {
            // Validation rules
            // . . . . . . . .

            // Form re-population
            
            $this->form_validation->
                    set_rules("categories",
                              "Kategória",
                              "xss_clean");            
            
            if($this->form_validation->run() == false)
            {                
                
                // Invalid data -> form re-population              
                $this->load->view("admin/admin_news_edit_view", $data);                
            }
            else
            {
                // Prepare data for database
                
                $post = array();

                // Required
                $editedPost['news_id'] = $postID;
                $editedPost['news_title'] = ucfirst($this->input->post("title"));                
                $editedPost['news_description'] = $this->input->post("description");          
                
                // Sorting criteria
                $post['lang'] = $this->input->post('lang');              
                $post['categories'] = $this->input->post("categories");                
                $post['tags'] = explode(" ", $this->input->post("tags"));                
                                
                $post['news_content'] = $this->input->post("content");                      
                
                if($this->admin_model->submitNewPost($post) == false)
                {
                    // @todo Remove mysql_error(), add better error warning
                    
                    $data['miscData']['error_message'] =
                        mysql_error();
                    
                    $this->load->view("admin/admin_news_edit_view", $data);
                }
                else
                {
                    // @todo Style entryStatus message
                    
                    $this->session->set_flashdata("entryStatus", "Príspevok úspešne uložený.");
                    redirect("admin/news");
                }
                              
            }
        }
        else
        {
            // No data submitted            
            $this->load->view("admin/admin_news_edit_view", $data);  
        }            
        
    }
}

Relevant code from form fragment:

Code:
<div id="news_form">
<h1 class="triform_green">&lt;?php echo $form_header ?&gt;</h1>
    <br />
    <div class="error_validation">&lt;?php if(!empty($error_message)): echo $error_message; endif; ?&gt;</div>
    &lt;?php echo form_open($form_action); ?&gt;

    <label>Kategórie:</label>
    <select id="form_categories" name="categories[]" multiple="multiple">
        &lt;?php foreach($categories as $category): ?&gt;
        <option value="&lt;?php echo $category['cat_id']; ?&gt;">&lt;?php echo $category['cat_title']; ?&gt;</option>
        &lt;?php endforeach; ?&gt;
    </select>
    <div class="spacer"></div>

<button type="submit" id="form_submit_button">Uložiť</button>
    <button id="form_preview_button">Náhľad</button>
    &lt;?php echo form_close(); ?&gt;
    <div class="spacer"></div>
</div>

As you can see, categories get passed on as an array, so the $_POST array looks like this:

Code:
Array
(
    [news_id] => 149
    [news_title] => Title X
    [news_description] => Dummy description
    [lang] => 2
    [categories] => Array
        (
            [0] => 1
            [1] => 2
        )

    [tags] => Array
        (
            [0] => TagX
            [1] => TagY
            [2] => TagZ
        )

    [news_content] => Dummy content
)

Everything works fine; the problem is this: When I use the same form in another function, called news_edit_post(), I populate individual fields with data from database, no problem with that. Same validation rules are applied but instead of adding a new post, an existing post is updated/edited. However, if no categories are chosen, $_POST array gets corrupted:

Code:
Array
(
    [news_id] => 149
    [news_title] => Title X
    [news_description] => Dummy description
    [lang] => 2
    [categories] =>      // Corrupted array
    [tags] => Array
        (
            [0] => TagX
            [1] => TagY
            [2] => TagZ
        )

    [news_content] => Dummy content
)

It's impossible to do anything with category field, I can't use it in loops; it says "Invalid argument supplied for foreach".

I've got no idea what's wrong, since it is almost the exact same code.

Any idea? Thanks in advance


Messages In This Thread
Corrupted Array - by El Forum - 07-11-2011, 05:14 AM
Corrupted Array - by El Forum - 07-13-2011, 11:26 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.