0

I've exhausted all research endpoints (docs, Google, SO, etc.), so I've been driven to ask this question publicly. The very nature of my problem should have been solved by the CodeIgniter 3.0.6 official documentation in the section entitled "Adding Dynamic Data to the View" as I (think) I'm dealing with a variable never making the scope of my controller to be accessed by my view.

I'm adding a single, custom modification to a content publishing app consisting of an edit page fetching content by ID for table data update. 1st, the form;

EDIT.PHP

<div class="row">
  <div class="col-md-12">
     <h3>Update post</h3>
         <form method="post" action="<?php echo base_url('post/update'); ?>" enctype="multipart/form-data">
     <div class="form-group">
       <label for="title">Title</label>
         <input type="text" name="title" id="title" placeholder="News title" value="<?php echo $data['post']->title; ?>" class="form-control" />
     </div>
     <div class="form-group">
       <label for="category">Category</label>
           <select name="category" id="category" class="form-control">
           <?php foreach($data['categories'] as $category): ?>
               <option value="<?php echo $category->idcategory; ?>" <?php echo set_select('category', $category->idcategory); ?>><?php echo $category->title; ?></option>
           <?php endforeach; ?>
           </select>
     </div>
     <div class="form-group">
       <label for="image">Image</label>
         <input type="file" name="image" id="image" class="form-control" placeholder="Upload an image" />
     </div>
     <div class="form-group">
       <label for="body">Post detail</label>
          <textarea name="body" id="body" class="form-control" placeholder="Provide news content. Basic HTML is allowed."><?php echo $data['post']->body; ?></textarea>
     </div>
     <div class="form-group">
       <label for="tags">Tags</label>
         <input type="text" name="tags" id="tags" value="<?php echo set_value('tags'); ?>" class="form-control" placeholder="Comma separated tags" />
     </div>
             <button type="submit" class="btn btn-primary">Submit</button>
    </form>
  </div>
</div>

I've edited the input values to target the values I need the form populated with in the cases of the Title and Post detail inputs (didn't need to alter anything for the category input, as it's a dropdown working brilliantly), and leaving the tags form input as is to not break output layout while I troubleshoot. Controller/function Post.php was duped from original 'add' function, and I'm including the entirety rather than what I feel is the problematic code chunk;

UPDATE FUNCTION

   public function update($idpost) {
    $this->load->helper('form');
    $data['title'] = 'Update post | News Portal';
    $data['post'] = $this->posts->get($idpost);
    $this->load->model('category_model', 'cm');
    $data['categories'] = $this->cm->get_all();

    $this->load->library('form_validation');

    $this->form_validation->set_rules('title', 'title', 'trim|required');
    $this->form_validation->set_rules('body', 'post body', 'trim|required');
    $this->form_validation->set_rules('tags', 'tags', 'required');

    if($this->input->method(TRUE) == 'POST' && $this->form_validation->run()) {

        $config['upload_path'] = './assets/uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '2000';
        $config['max_width']  = '2000';
        $config['max_height']  = '1200';
        $config['encrypt_name']  = TRUE;

        $this->load->library('upload', $config);

        if (!$this->upload->do_upload('image')) {
            $this->template->alert(
                $this->upload->display_errors(),
                'danger'
            );
        } else {
            $upload_data = $this->upload->data();
            $idpost = $this->posts->add(array(
                'iduser' => $this->user->id(),
                'title' => $this->input->post('title'),
                'body' => $this->input->post('body'),
                'image' => $upload_data['file_name']
            ));
            $tags = $this->input->post('tags');
            if(strlen($tags) > 0) {
                $this->load->model('tag_model', 'tm');
                $tags = explode(',', trim($tags));
                $tags = array_map(array($this->tm, 'set_tag'), $tags);
                $this->load->model('post_tag_model', 'ptm');
                foreach($tags as $idtag) {
                    $this->ptm->add(array(
                        'idpost' => $idpost,
                        'idtag' => $idtag
                    ));
                }
            }
            $idcategory = $this->input->post('category');
            if($idcategory) {
                $this->load->model('post_category_model', 'pcm');
                $this->pcm->add(array(
                    'idpost' => $idpost,
                    'idcategory' => $idcategory
                ));
            }

            $this->template->alert(
                'Updated news item successfully',
                'success'
            );
            redirect('post');
            return;
        }
    }

    $this->template->view('post/edit', $data);
}    

This variable ($tags) is somehow lost between the controller and view, confirmed by using var_dump($this->_ci_cached_vars); to check all available objects within that corresponding view. I just need the variable $tags to repopulate with the data appropriate to the form input. How is it that there was no initialization of $tags within this function?

I COMPLETELY understand that the variable WILL NEVER be passed to the corresponding view because it's non-existant (as confirmed by my previous var_dump), but I'm lost as to how exactly to draw $tags within function scope so it can help form input to retrieve targeted data? All the other inputs are repopulating as needed. And, as an aside, I actually tracked down the original developer of this project and conferred with him on this. I tried to wrangle two approaches he outlined, but I ended up getting blank or erroring pages. The closest I could come theoretically to his second point - adapting a bit of code already in the news view partial;

<?php
    if($tags = get_tags($data['news']->idpost)) {
        echo '<div class="tags">';
        echo 'Terms: ';
        foreach($tags as $tag) {
            echo ' <i class="fa fa-fw fa-link"></i> <a href="' . base_url('news/tag/' . $tag->idtag) . '">' . $tag->title . '</a> ';
        }
        echo '</div>';
    }
    ?>

which always ends in Undefined Index/Variable or some other damnable error message that I try to break my neck to solve, but just keep sinking further into the quagmire (lol!). It SEEMS simple to me, the basis of my problem, but I keep going around, and around, and around until I'm drunk dizzy and ten time more lost than I started out. Could someone provide a crumb of understanding?

I mean, I'm getting a conniption fit as it should be as simple as the answer provided here @ Passing variable from controller to view in CodeIgniter. But, alas, 'tis not...thanks in advance for any clarificative leads.

@DFriend - I'm not wanting to alter the structure too much as ;

a) the code this is duped from is working and the only goal is to pull data from the appropriate table into that form input,

b) I don't want to disturb current functionality or inadvertently open another issue, and,

c) I'm trying to zero in on the correct elements.

Thank you for your time and answer, @DFriend.

Community
  • 1
  • 1
HomeOffice
  • 139
  • 3
  • 16

1 Answers1

0

I believe that the reason for your problems is because of the redirect call. By calling that you are essentially putting a new URL into the browser. Websites are stateless. Without using sessions or other means each request for a URL is unique. So by calling redirect you are wiping any knowledge of $tags from existence.

You can probably get around this by pushing $tags into the $_SESSION array and then checking for and retrieving it in the post controller.

Alternately, if post() is in the same controller you can simple call it instead of the redirect. post() will have be modified to accept an argument, or $tags will have to be a property of the controller class.

So to call directly to post, instead of

  redirect('post');
  return;

do this

 $this->post($tags);
 return;

Then define post to accept an optional argument

public function post($tags=null){

    //somewhere in here
    if(isset($tags)){ 
        //$data is sent to views
        $data['tags'] = $tags;
    }
}

Expanded Answer: How to implement a Post/Read/Get pattern in Codeigniter and still use field validation and repopulated form fields.

Using Codeigniter (CI) to implement a PRG pattern for processing requires an extension of the CI_Form_validation class. The code that follows should be in /application/libraries/MY_Form_validation.php

<?php
/**
 * The base class (CI_Form_validation) has a protected property - _field_data
 * which holds all the information provided by validation->set_rules() 
 * and all the results gathered by validation->run()
 * MY_Form_validation provides a public 'setter' and 'getter' for that property.
 *
 */
class MY_Form_validation extends CI_Form_validation{

    public function __construct($rules = array())
    {
        parent::__construct($rules);

    }
    //Getter
    public function get_field_data() {
      return $this->_field_data;    
}
    //Setter
    public function set_field_data($param=array()){
      $this->_field_data = $param;
    }

}

Without the template library you are using I fell back on $this->load->view(). And without access to your models and associated data I had to make some assumptions and, in some cases, leave db calls out.

Overall, I tried not to alter the structure too much. But I also wanted to demonstrate a couple handy uses of the form helper functions.

for the most part the restructuring I did was mostly an attempt at providing a clear example. If I succeed as showing the concepts you should be able to implement this fairly easily.

Here's the revised view. It makes more use of the 'form' helper functions.

<head>
  <style>
    .errmsg {
      color: #FF0000;
      font-size: .8em;
      height: .8em;
    }
  </style>
</head>
<html>
  <body>
    <div class="row">
      <div class="col-md-12">
        <h3>Update post</h3>
        <?php
        echo form_open_multipart('posts/process_posting');
        echo form_hidden('idpost', $idpost);
        ?>

        <div class="form-group">
          <label for="title">Title</label>
          <input type="text" name="title" id="title" placeholder="News title" value="<?php echo $title; ?>" class="form-control" />
          <span class="errmsg"><?php echo form_error('title'); ?>&nbsp;</span>
        </div>
        <div class="form-group">
          <label for="category">Category</label>
          <?php
          echo form_dropdown('category', $categories, $selected_category, ['class' => 'form-control']);
          ?>
        </div>
        <div class="form-group">
          <label for="image">Image</label>
          <input type="file" name="image" id="image" class="form-control" placeholder="Upload an image" />
        </div>
        <div class="form-group">
          <label for="body">Post detail</label>

          <textarea name="body" id="body" class="form-control" placeholder="Provide news content. Basic HTML is allowed."><?php echo $body; ?></textarea>
          <span class="errmsg"><?php echo form_error('body'); ?>&nbsp;</span>
        </div>
        <div class="form-group">
          <label for="tags">Tags</label>
          <input type="text" name="tags" id="tags" value="<?php echo $tags ?>" class="form-control" placeholder="Comma separated tags" />
          <span class="errmsg"><?php echo form_error('tags'); ?>&nbsp;</span>
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
        <?= form_close(); ?>
      </div>
    </div>
  </body>
</html>

The crux of this solution is to store form_validation->_field_data in the session when validation fails. The view loading function looks for a failure flag in session data and, if the flag is true, restores form_validation->_field_data to the current form_validation instance.

I tried to put a lot of explanation in the comments. What follows in the controller complete with display and processing methods.

class Posts extends CI_Controller
{
  function __construct()
  {
    parent::__construct();
    $this->load->library('session');
    $this->load->library('form_validation', NULL, 'fv');
  }

  /**
   * post()
   * In this example the function that shows the posting edit page
   * Note the use of the optional argument with a NULL default
   */
  function post($idpost = NULL)
  {
    $this->load->model('category_model', 'cm');
    $categories = $this->cm->get_all();
    /*
     * Your model is returning an array of objects. 
     * This example is better served by an array of arrays. 
     * Why? So the helper function form_dropdown() can be used in the view.
     * 
     * Rather than suggest changing the model
     * these lines make the conversion to an array of arrays.
     */
    $list = [];
    foreach($categories as $category)
    {
      $list[$category->idcategory] = $category->title;
    }
    $data['categories'] = $list; // $data is used exclusivly to pass vars to the view


    if(!empty($idpost))
    {
      //if argument is passed, a database record is retrieved.
      $posting = $this->posts->get($idpost);


      //Really should test for a valid return from model before using it. 
      //Skipping that for this example
      $title = $posting->title;
      $body = $posting->body;
      //assuming your model returns the next two items like my made up model does
      $selected_category = $posting->category; 
      $tags= $posting->tags; 
    }
    else
    //a failed validation (or a brand new post)
    {
      //check for failed validation
      if($this->session->failed_validation)
      {
        // Validation failed. Restore validation results from session.
        $this->fv->set_field_data($_SESSION['validated_fields']);
      }
    }

    //setup $data for the view

    /* The 'idpost' field was add to demonstrate how hidden data can be pasted to and from
     * a processing method. In this case it would be useful in providing a 'where = $value'
     * clause on a database update. 
     * Also, a lack of any value could be used indicate an insert is requried for a new record.
     * 
     * Notice the ternary used to provide a default value to set_value()
     */
    $data['idpost'] = $this->fv->set_value('idpost', isset($idpost) ? $idpost : NULL);

    $data['title'] = $this->fv->set_value('title', isset($title) ? $title : NULL);
    $data['body'] = $this->fv->set_value('body', isset($body) ? $body : NULL);
    $data['tags'] = $this->fv->set_value('tags', isset($tags) ? $tags : NULL);
    $data['selected_category'] = $this->fv->set_value('category', isset($selected_category) ? $selected_category : '1');

    $this->load->view('post_view', $data);
  }

  public function process_posting()
  {
    if($this->input->method() !== 'post')
    {
      //somebody tried to access this directly - bad user, bad!
      show_error('The action you have requested is not allowed.', 403);
      // return;  not needed because show_error() ends with call to exit
    }

    /*
     * Note: Unless there is a rule set for a field the 
     * form_validation->_field_data property won't have 
     * any knowledge of the field. 
     * In Addition, the $_POST array from the POST call to the this page
     * will be GONE when we redirect back to the view! 
     * So it won't be available to help repopulate the <form>
     * 
     * Rather than making a copy of $_POST in $_SESSION we will rely
     * completely on form_validation->_field_data 
     * to repopulate the form controls.
     * That can only work if there is a rule set 
     * for ANY FIELD you want to repopulate after failed validation.
     */

    $this->fv->set_rules('idpost', 'idpost', 'trim');  //added any rule so it will repopulate
    // in this example required would not be useful for 'idpost'

    $this->fv->set_rules('title', 'title', 'trim|required');
    $this->fv->set_rules('body', 'post body', 'trim|required');
    $this->fv->set_rules('tags', 'tags', 'required');
    //add rule for category so it can be repopulated correctly if validation fails
    $this->fv->set_rules('category', 'category', 'required');

    if(!$this->fv->run())
    {
      // Validation failed. Make note in session data
      $this->session->set_flashdata('failed_validation', TRUE);

      //capture and save the validation results
      $this->session->set_flashdata('validated_fields', $this->fv->get_field_data());

      //back to 'posts/index' with server code 303 as per PRG pattern
      redirect('posts/post', 'location', 303);
      return;

    }
    // Fields are validated
    // Do the image upload and other data storage, set messges, etc

    // checking for $this->input->idpost could be used in this block 
    // to determine whether to call db->insert or db->update
    // 
    // When process is finished, GET the page appropriate after successful <form> post
    redirect('controller/method_that_runs_on_success', 'location', 303);
  }

//end Class
}

Questions? Comments? Insults?

DFriend
  • 8,869
  • 1
  • 13
  • 26
  • thank you for the lookover, but I'll have to disagree with you about the source of my problem being located in the redirect (if I'm going to imitate the PRG pattern, the redirect sends me to the page which shows an updated result - tested with the "add" function this one is duped from). I agree with you that there has to be a declaration of tags variable for it to be added to the view called into the controller. I'm testing that by placing $data['tags'] = $tags; in the index() function. – HomeOffice Apr 22 '16 at 20:26
  • I understand about your desire to use the PRG pattern. Unfortunately CI doesn't make that easy to accomplish. My PRG solution uses a custom library that extends CI_Form_validation. It adds a setter and getter for the protected property `_field_data`. A separate method is the form's "action" which redirects as appropriate depending on validation results. On failure flashdata is used to get validation results back to the initial form. That page looks for the flashdata and acts on it if present. I can edit my answer with a full example if you like - but not till Monday. :) Let me know. – DFriend Apr 22 '16 at 20:49
  • I completely appreciate the offer and am in no crazy rush, so I'll keep plugging away over the weekend. Appreciate all the help so far, but it's a drag having to "customize" for a feature that just screams to be included in terms of common sense in the 1st place - who ever heard of a content publishing app without the edit function (lol!)? Just saying ;) – HomeOffice Apr 22 '16 at 20:59
  • Are you using CodeIgniter version 3.0.+ ? – DFriend Apr 22 '16 at 21:53
  • CodeIgniter 3.0.6 it is (I believe, since the app is dated @ 2016, and the package took some custom imports from the official 3.0.6 download). – HomeOffice Apr 22 '16 at 22:05
  • Unfortunately, for this answer - it'd take the application down a road it really doesn't need to travel. I'm not having ANY problem with the PRG pattern. Tried extrapolating a workaround for how to get the tags variable echoed back into the specific form input scraping this, but, STILL, no joy. Thanks so much for your time and effort @DFriend - the code I duped this from allows entry of tags to the database, but I have to figure out how to hit post_tags table to access the tag ids to return them to the input via set_value(); – HomeOffice Apr 25 '16 at 22:42