Passing a parameter from the view to the library and returning after the process.

I am going to create a custom library. I want to pass a string from the view to the library and the process, and then return to the same view after. My code looks like this:

application / libraries / MultiImageParser.php

 <?php
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    //return profile pic of img arrays.
      class MultiImageParser { 
          function parser($multiImage)   {    //get $prods->images here as parameter 
             $images = $multiImage;  //gets multiple image from controller like 1.jpg,2.jpg,3.jpg
             $pieces = explode(",", $images); //explode make arrays.
             $one = $pieces[0];
             return $one;
          }
       }

View

 <?php 
     $CI =& get_instance();
     $CI->load->library('multiImageParser');  //loading library from view
     $profilepic = $CI->multiImageParser->parser($prods->images);
     echo $profilepic;
 ?>

And I get this error call to member function parser() on a non-object. How can i solve this.

0
source share
2 answers

The problem is the way the library method is called. According to CI Documentation - Creating Libraries :

Object instances will always be lowercase

$this->someclass->some_function();  // Object instances will always be lower case

So, in your case, it should be:

$profilepic = $this->multiimageparser->parser();

, , MVC

:

 public function controller_method() {
    $this->load->library('MultiImageParser');
    $img = $this->multiimageparser->parser();
    $data = array(
        "profilepic" => $img
    );
    $this->load->view('view_name', $data);
}

:

echo $profilepic;
0

, , . (, , ) , . , , , . , , , $prods->images. , , (/, ?) . , , .

0

Source: https://habr.com/ru/post/1672326/


All Articles