0

I have a bit of an issue using codeigniter's set_select and set_checked within my forms, I am adding these to my existing forms as I am the stage in development where I am trying to tidy things up and failed validation resetting forms was not a big issue when I was still working on the project but now its coming to a close its become a major headache.

Firstly the set_select, I have this code which outputs me drop down from an array which is passed to the view from the controller which gets the results from a table in my database, the form I am implementing this in has 10 drop down boxes each corresponds to a table in my database. Anyway this is the code:

<label for="rating">Rating: </label>
<select name="rating">
<?php
    if(isset($rating) && $rating != 'none') {
    echo '<option value="" '.set_select('rating', '', TRUE).'></option>';
        foreach($rating as $row)  { 
            echo '<option value="'.$row->door_rating_rating.set_select('rating', $row->door_rating_rating).'">'.$row->door_rating_rating.'</option>';
        }
    } else {
        echo '<option value="none">Nothing to list</option>';
    }
?>
</select> 

It is just not working and as far as I can see there shouldn't be a problem with my code but this is the first time I have used this and I have looked at examples of using it but could not find an example of using it in a for loop so is what I am doing even possible?

This is my set_checked code within the view and this too is not working after failed validation:

Temporary Refuge Door?<input type="checkbox" class="temp_ref" name="tempref" value="1" <?php echo set_checkbox('tempref', '1'); ?> /> 

Any help with either of these would be really appreciated.

Someone
  • 894
  • 3
  • 22
  • 43
  • Why not use form_dropdown. http://stackoverflow.com/questions/8049458/codeigniter-form-helper-set-select – karmafunk Jun 26 '13 at 08:53
  • Is there no way to get it working for the way I have produced my drop down? as changing to the method I am using means changing roughly 10 dropdowns in 5 views and then I would need to edit all the functions in the models which pull the data as the form_dropdown uses arrays differently to how I generated mine in that it uses key->value and I only use the value of each key for both the value and text in an option. – Someone Jun 26 '13 at 13:39
  • I don't know, I've only used form_dropdown. Not sure why you would want to do it the long way round unless form_dropdown doesn't do something yo need. – karmafunk Jun 26 '13 at 13:51
  • I can see myself using this in future for sure but this project has been my first use of codeigniter and the project has been massive, learned so much but still learning. Just want to know if theres an easier way to get set_selected working without having to redo so much code. – Someone Jun 26 '13 at 13:56

2 Answers2

2

Looks like you had the set_select within the quotes for the option value. I moved it after it. I have also made an edit to use printf for better readability.

<label for="rating">Rating: </label>
<select name="rating">
<?php
    if(isset($rating) && $rating != 'none') {
     echo '<option value="" '.set_select('rating', '', TRUE).'></option>';
         foreach($rating as $row)  { 
             printf('<option value="%s" %s>%s</option>', $row->door_rating_rating, set_select('rating', $row->door_rating_rating), $row->door_rating_rating);
         }
     } else {
         echo '<option value="none">Nothing to list</option>';
     }
?>
</select> 
karmafunk
  • 1,453
  • 12
  • 20
2

To solve set_select() try the following. Assuming you have an array named $isps which contains id and name field.

<select id="isp" name="isp">
 <option value="" selected>Select a ISP</option>
 <?php foreach ($isps as $row) { ?>
<option value="<?php echo $row->id ; ?>" <?php echo set_select('isp', $row->id, False); ?> ><?php echo $row->name ; ?> </option> 
 <?php } ?>
</select>
rumman0786
  • 1,150
  • 10
  • 20
  • Thanks for taking the time to post this, I no longer need this but I am sure others will find it useful. – Someone Jan 22 '15 at 10:47