Jump to content

Deleting/Adding data values from a multiselect


KSI

Recommended Posts

Currently I have a multiselect option in my edit page where at first it should display all the existing values in the database as already checked and once the user removes a selected value, it should be removed from the database and if it is already not there in the database, it should be added to the database. Currently this is my code:

View class:

<select name="curations[]" id="curations" multiple class="form-control multiselects" <?php echo (isset($read) ? $read:''); ?> multiple>
<?php $checked = array();
    foreach($get_curation as $key => $value){ 
    $checked[]=$value['curations_id'];
    }
    if($curations){ foreach($curations as $cur){ ?>
<option value="<?php echo $cur['id']; ?>" <?php echo in_array($cur['id'], $checked) ? 'selected' : 'selected'; ?>><?php echo $cur['title']; ?></option>
<?php }
} ?>
</select>

Controller Class:

if ($this->input->post('curations') != '') {
    $curationsPost = $this->input->post('curations');
    $curationsArray = array();
    $c = 0;
    foreach($curationsPost as $cp){
        if ($cp != ''){
        $curationsArray[$c]['listings_id']= $id;
        $curationsArray[$c]['curations_id']= $cp;

        $c++;
    }
}

    if(count($curationsArray) > 0){
        $this->listings_model->update_curations($curationsArray);
    }
}

Model class:

function update_curations($curationsArray){
    $this->db->insert_batch("crm_property_curation",$curationsArray);
 }

As of now by default it displays all the values as selected eventhough the values aren't present in the database and now everytime that I submit the form, it creates a duplicate entry in my database if the value was already checked. So basically what I need here is 2 things, 1 is that it shouldn't show everything initially when user enters the page and only show the selected values, and 2 is that when the form is submitted, it should insert the nonduplicate selected values and delete the unselected values.

I've tried $this->db->update_batch("crm_property_curation",$curationsArray); but this does not work. As my current query is inserting all the values again, I think what my query should do is delete all the values with the particular listings_id and replace them with the incoming values if thats possible.

Link to comment
Share on other sites

Why the two multiples in your select tag?

Why the if to simply set the 'selected' attribute for everything?

Where is this database involved.?

Why so many classes for a simple html page build?  Looks like a very simple straight-forward task that is further complicated by the un-necessary separation into different classes.

How about learning how write your code without going into and out of php mode?  No need to make your code so difficult to read and follow.  Try going into php mode and staying there. Set the values you need to insert and then simply insert them into the html instead of inserting a 'block' of php code inside your html.  Start your echos with a double quote and wrap your array elements in braces to handle them.

Link to comment
Share on other sites

On 12/5/2021 at 9:48 AM, ginerjm said:

 

How about learning how write your code without going into and out of php mode?  No need to make your code so difficult to read and follow.  Try going into php mode and staying there. Set the values you need to insert and then simply insert them into the html instead of inserting a 'block' of php code inside your html.  Start your echos with a double quote and wrap your array elements in braces to handle them.

This is a great point from @ginerjm  Mixing "presentation" and "logic" is the best way to have hard to maintain code.  PHP is intrinsically a templating product, in that you can put partial html scripts and include them easily.  There are also numerous excellent and easy to integrate template systems.  

If you can separate the database/model related code from all the other code that is really just html, it will be easier to see how to approach things in a simple and maintainable way.  The concept is certainly related to KISS and to breaking larger blocks of complicated interdependent code down into discreet functions that do one thing in a predictable way.

From a database standpoint, your comment that "it creates a duplicate entry in my database if the value was already checked" also tells you that you are not using features of the database engine that will provide you data integrity.  You can use constraints/indexes, as well as "UPSERTS" to help manage this.  I would provide further examples, but I have no idea from your code snippets what your database looks like.

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.