php
Function for modify filename on a view from array
I created a function to retrieve the original path of an image stored in database. public function getImageWork($suffix) { $basePath = 'uploads/works/'; $fullname = pathinfo($this->images, PATHINFO_FILENAME); $imageWork = $basePath . $fullname . $suffix; if (file_exists($imageWork)) { return URL('/') . '/' . $imageWork; } else { return $imageWork = URL('/') . '/img/no-avatar.jpg'; } } I create several different image sizes that I store in the same folder from the same name but at the end I add, for example, _cover or _thumb and thanks to this function, it allows me in my view to make $ image->getImageStudents('_ thumb.jpg') Only, I'm in front of a different case and I can't make it work. I have an image gallery, and in my DB, the images are stored under an array. ["uploads/img1.jpg","uploads/img2.jpg","uploads/img3.jpg"…] How I can do to change my function so that in my foreach, it works. Here I do pathinfo($this-> images) but by doing that, I recuperate the entire array, and I can not seem to work. thank you very much
I would split the code up in two functions, where one function takes in an image variable instead of relying on this. Something like this: public function getImageWork($suffix) { if (!is_array($this->images)) { return $this->getSingleImageWork($this->images, $suffix); } $output = []; foreach ($this->images as $image) { $output[] = $this->getSingleImageWork($images, $suffix); } return $output; } public function getSingleImageWork($image, $suffix) { $basePath = 'uploads/works/'; $fullname = pathinfo($image, PATHINFO_FILENAME); $imageWork = $basePath . $fullname . $suffix; if (file_exists($imageWork)) { return URL('/') . '/' . $imageWork; } return $imageWork = URL('/') . '/img/no-avatar.jpg'; } Note: I am not sure how the variables are, but I assume here that $this->images is either a variable of some kind, or a array of values.
To accommodate both single value and array for images in getImageWork method, you need to refactor it in the following way, public function getImageWork($suffix = ""){ $basePath = 'uploads/works/'; $imagePathArr = array(); $images = is_array($this->images) ? $this->images : array($this->images); foreach($images as $image){ $fullname = pathinfo($image, PATHINFO_FILENAME); $imageWork = $basePath . $fullname . $suffix; if (file_exists($imageWork)) { $imagePathArr[] = URL('/') . '/' . $imageWork; } else { $imagePathArr[] = URL('/') . '/img/no-avatar.jpg'; } } return $imagePathArr; } As an output, the above method will return an array of image paths which you can use later in the codebase.
Related Links
Cannot run a simple PHP file on the server [closed]
I get multiple Json Object from curel and need to conver it to PHP
SOAP error log, what's the issue?
Print specified value from an JS object
PHP mysql_connect not returning boolean
how to add script in controller in CI
HTML5 history with jQuery Ajax - Javascript issue
How to find the Array with the longest string length inside a multi dimensional array [closed]
application wide classes for codeigniter [closed]
Jquery json add new row
Not getting the content of webpage using Curl in Php
Can the Magento soap api add a tracking order after the shipment was already created?
Getting all values from a specific array within a multidimentional array
Best practice: generating queries with data sent with AJAX
php, mysql best way to access static reference table
Android post JSON to web service php and recieving JSON from service