CodeIgniter Forums

Full Version: [Solved] Model Function Not Working With Array Post
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to update my $this->input->post('page'); array. But for some reason when It updates It updates blank.

I have var_dumped the post and is correct.

PHP Code:
array(3) { ["page"]=> array(3) { [1]=> array(1) { ["page_name"]=> string(20"Controller Example 1" } [2]=> array(1) { ["page_description"]=> string(4"Test" } [3]=> array(1) { ["page_status"]=> string(1"1" } } ["page_content"]=> array(1) { [0]=> array(2) { ["content_name"]=> string(9"Example 1" ["content_text"]=> string(18"Example 1 Textarea" } } ["popup_image"]=> array(1) { [0]=> array(2) { ["image"]=> string(10"family.png" ["popup_image_id"]=> string(4"2407" } } } 

I am not sure why is updating with blank data. Its something to do with model function I think

PHP Code:
<?php

class Page_edit extends MX_Controller {

    public function 
index() {
        
$page_id $this->uri->segment(4);

        
// Page Content

        
$page_data_post $this->input->post('page');

        if (isset(
$page_data_post)) {
            
$data['page'] = $page_data_post;
        } elseif (isset(
$page_id)) {
            
$data['page'] = $this->get_page_info($page_id);
        } else {
            
$data['page'] = array();
        }

        
// Page Content

        
$content_post $this->input->post('page_content');

        if (isset(
$content_post)) {
            
$data['page_content'] = $content_post;
        } elseif (isset(
$page_id)) {
            
$data['page_content'] = $this->get_page_contents($page_id);
        } else {
            
$data['page_content'] = array();
        }

        
$this->form_validation->set_rules('page_name''Page Name');
        
$this->form_validation->set_rules('page_description''Page Description');
        
$this->form_validation->set_rules('page_image''Page Image');

        if (
$this->form_validation->run($this) == FALSE) {

            
$this->load->view('template/page/page_edit_view'$data);
        
        } else {

            
$this->page_update($page_id$this->input->post());

            
redirect('admin/page/page_list');
            
        }
    }

    
// To do create a model when test complete

    
public function page_update($page_id$data) {
        foreach (
$data['page'] as $page) {
            
$this->db->query("UPDATE " $this->db->dbprefix "page SET  
                page_name = " 
 $this->db->escape($page['page_name']) . ", 
                page_description = " 
 $this->db->escape($page['page_description']) . "
                WHERE page_id = '" 
. (int)$page_id "'
            "
);
        }
        

        
$this->db->query("DELETE FROM " $this->db->dbprefix "page_content WHERE page_id = '" . (int)$page_id "'");

        if (isset(
$data['page_content'])) {
            foreach (
$data['page_content'] as $page_content) {
                
$this->db->query("INSERT INTO " $this->db->dbprefix "page_content SET 
                    page_id = '" 
. (int)$page_id "', 
                    content_name = " 
 $this->db->escape($page_content['content_name']) . ", 
                    content_text = " 
 $this->db->escape($page_content['content_text']) . "
                "
);
            }
        }
    }

    public function 
get_page_info($page_id) {
        
$this->db->where('page_id'$page_id);
        
$query $this->db->get($this->db->dbprefix 'page');

        if (
$query->num_rows() > 0) {
            return 
$query->result_array();
        } else {
            return 
false;
        }
    }

    public function 
get_page_contents($page_id) {
        
$this->db->where('page_id'$page_id);
        
$query $this->db->get($this->db->dbprefix 'page_content');

        if (
$query->num_rows() > 0) {
            return 
$query->result_array();
        } else {
            return 
false;
        }
    }


View

PHP Code:
<?php $data = array('class' => 'form-horizontal');
echo 
form_open_multipart('admin/page/page_edit' .'/'$this->uri->segment(4), $data);
?>
<?php 
echo validation_errors('<div class="alert alert-danger">''</div>'); ?>
<?php 
foreach ($page as $page_data) {?>
<div class="form-group">
<label class="col-sm-2">Page Name</label>
<div class="col-sm-10">
<input type="text" name="page[1][page_name]" value="<?php echo $page_data['page_name'];?>" placeholder="Please Enter A Page Name" class="form-control" />    
</div>
</div>

<div class="form-group">
<label class="col-sm-2">Page Description</label>
<div class="col-sm-10">
<textarea name="page[2][page_description]" class="form-control" style="height: 20rem;"><?php echo $page_data['page_description'];?></textarea>  
</div>
</div>

<div class="form-group">
<label class="col-sm-2">Page Status</label>
<div class="col-sm-10">
<select class="form-control" name="page[3][page_status]">
<?php if ($page_data['page_status']) {?>
<option value="0">Disabled</option>
<option value="1" selected="selected">Enabled</option>
<?php } else { ?>
<option value="0" selected="selected">Disabled</option>
<option value="1">Enabled</option>
<?php ?>
</select>
</div>
</div>
<?php ?>
<button type="submit" class="btn btn-primary" ><i class="fa fa-save"></i> Save Form</button>
<?php echo form_close();?>

Model Function


PHP Code:
public function page_update($page_id$data) {
    foreach (
$data['page'] as $page) {
        
$this->db->query("UPDATE " $this->db->dbprefix "page SET  
            page_name = " 
 $this->db->escape($page['page_name']) . ", 
            page_description = " 
 $this->db->escape($page['page_description']) . "
            WHERE page_id = '" 
. (int)$page_id "'
        "
);
    }
        

    
$this->db->query("DELETE FROM " $this->db->dbprefix "page_content WHERE page_id = '" . (int)$page_id "'");

    if (isset(
$data['page_content'])) {
        foreach (
$data['page_content'] as $page_content) {
            
$this->db->query("INSERT INTO " $this->db->dbprefix "page_content SET 
                page_id = '" 
. (int)$page_id "', 
                content_name = " 
 $this->db->escape($page_content['content_name']) . ", 
                content_text = " 
 $this->db->escape($page_content['content_text']) . "
            "
);
        }
    }


Any Suggestion
if ($this->form_validation->run($this) == FALSE)

$this - there is such a configuration?




PHP Code:
if ($this->form_validation->run() == FALSE


is not it supposed to be?
You have got to love the http://php.net/ website have been reading on $_POST section

I found out what I was doing wrong. I had different numbers in the middle part of post.

I had

Code:
name="page[1][page_name]" & name="page[2][page_description] & name="page[3][page_status]"

It should of been with the second part number all should of been the same.


Code:
name="page[0][page_name]" & name="page[0][page_description]" & name="page[0][page_status]"

http://php.net/manual/en/reserved.variables.post.php Very good reading.
(05-23-2015, 10:33 PM)klassev Wrote: [ -> ]if ($this->form_validation->run($this) == FALSE)

$this - there is such a configuration?







PHP Code:
if ($this->form_validation->run() == FALSE


is not it supposed to be?

If you use HMVC you need it for MY_Form_Validation Other Wise Sometimes Callbacks do not work with HMVC


PHP Code:
<?php

class MY_Form_validation extends CI_Form_validation {

    function 
run($module ''$group '') {
        (
is_object($module)) AND $this->CI = &$module;
        return 
parent::run($group);
    }