To remove the extraneous commas going into the database, you can usearray_filter
,trim
, orpreg_replace
depending on your preference.
With a theoretical value for$this->input->post('coresubjs[]')
of
array(
'',
'',
'0',
'A',
'B',
'',
'D',
'',
'',
);
Usingimplode(',')
only will result in
,,0,A,B,,D,,
However please keep in mind, that any commas in the post values will not be properly accounted for and should be escaped using another means. eg:1,000
will be exploded asarray("1", "000")
. For this reason I recommend refactoring in favor of using{-code-10}
andjson_decode()
orserialize()
andunserialize()
, instead ofimplode(',')
andexplode(',')
Example: https://3v4l.org/I2ObY
Removes the "empty" values (0, "", false, null
) from theimplode
array argument.
This will work against any input value that is empty, including the middle of the array.
implode(',', array_filter($this->input->post('coresubjs[]')))
Result
Notice that all extraneous commas and the0
value was removed
A,B,D
To circumvent the issue with "empty" values such as0, false
, you can use a custom callback instead.
implode(',', array_filter($this->input->post('coresubjs[]'), function($value) {
return null !== $value && '' !== $value;
}))
Result
Notice all extraneous commas were removed
0,A,B,D
Removes only the leading and trailing commas from theimplode
value.
trim(implode(',', $this->input->post('coresubjs[]')), ',')
Result
Notice the leading and trailing commas are removed but extra comma in the middle remained.
0,A,B,,D
Similar totrim
, removing the leading and trailing commas from theimplode
value, with the addition of replacing any 2 or more commas in the middle with a single comma.
preg_replace(['/,{2,}/', '/^,+/', '/,+$/'], [',', '', ''], implode(',', $this->input->post('coresubjs[]')))
Pattern Explanation:
,{2,}
any 2 or more commas ^,+
begins with 1 or more comma ,+$
ends with 1 ore more commaResult
Notice All extraneous commas have been removed
0,A,B,D
In yourupdate_core function
, you need totrim
the additional','
. You can do this as follow -
public function update_core(){
$data = array(
'core_subjects' => rtrim(implode(',', $this->input->post('coresubjs[]')), ','); // remove the extra ',' to the right
);
$this->db->where('id', $this->input->post('coresubjID'));
return $this->db->update('subjects', $data);
}
Also, remember that you don't have to remove the data every time you update. When updating it will automatically overwrite the previous value. So yourremove_core
method is redundant and should be removed
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
CodeIgniter is a framework that is known for requiring a minimum amount of customization to get it up and running. This allows those who choose it to work at a good pace. It has been updated many times since its inception in 2006. Now the most recent version is 4.0.3.
https://www.codeigniter.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.