Welcome Guest, Not a member yet? Register   Sign In
set select to repulate lists from forms
#1

[eluser]alvaroeesti[/eluser]
The guide says that to repopulate a select list from a form after having to return to it

you do this


Code:
<option value="one" &lt;?php echo set_select('myselect', 'one', TRUE); ?&gt; >One</option>

However, that is when you hard code it, which is the least of the cases. How do you go about when you are populating your lists dynamically from a db? such as in this:

Code:
&lt;?php foreach($paises as $pais): ?&gt;
  <option value ="&lt;?php echo $pais->id_pais?&gt;">&lt;?php echo $pais->nombre_pais?&gt;</option>
  &lt;?php endforeach;?&gt;

I have this, but it aint working

Code:
<select name ="paises" id = "paises">
  &lt;?php foreach($paises as $pais): ?&gt;
  <option value ="&lt;?php echo $pais->id_pais?&gt;">&lt;?php echo set_select('paises', $pais->id_pais);?&gt;&lt;?php echo $pais->nombre_pais?&gt;&lt;?php echo set_select('paises', $pais->nombre_pais);?&gt;</option>
  &lt;?php endforeach;?&gt;
#2

[eluser]CroNiX[/eluser]
because set_select() works on the options part, not the select part (like you have in your first code)
#3

[eluser]alvaroeesti[/eluser]
I had pasted excess of code before, what I have is this:


Code:
<select name ="paises" id = "paises">
  &lt;?php foreach($paises as $pais): ?&gt;
  <option value ="&lt;?php echo $pais->id_pais?&gt;">&lt;?php echo set_select('paises', $pais->id_pais);?&gt;&lt;?php echo $pais->nombre_pais?&gt;&lt;?php echo set_select('paises', $pais->nombre_pais);?&gt;</option>
  &lt;?php endforeach;?&gt;
  
  </select>

but it doesn't work
#4

[eluser]CroNiX[/eluser]
Code:
$this->load->helper('form');

$options[0] = 'Select a Value';  //default first value in options
//this should be done in the model and pass back the $options array to the view.
foreach($paises as $pais)
{
    $options[$pais->id_pais] = $pais->nombre_pais;
}

//view
echo form_dropdown('paises', $options, set_value('paises', 0), 'id="paises"');
#5

[eluser]alvaroeesti[/eluser]


Thank you, I am going to examine that to understand well the details. I have 3 interdependent select lists which work flawlessly. Now I am trying to add this repopulation feature because elswewhere in the web I am trying to paginate, and every time I click on a page link it went blank as it did not find any values in the form. So I hpe that by doing this, pagination will work.

The first select list gets populated in the view as it does the foreach loop there, but the other 2, (out of country state and city) are created in the Model, as you indicated.

I will carefully see how to implement that,

regards
#6

[eluser]CroNiX[/eluser]
And why the way you have it isn't working...

Your code:
Code:
<option value ="&lt;?php echo $pais->id_pais?&gt;">&lt;?php echo set_select('paises', $pais->id_pais);?&gt;&lt;?php echo $pais->nombre_pais?&gt;&lt;?php echo set_select('paises', $pais->nombre_pais);?&gt;</option>

User Guide:
Code:
//Notice set_select() is within the opening <option> tag (this is where 'select="selected"' needs to go...)
<option value="one" &lt;?php echo set_select('myselect', 'one', TRUE); ?&gt; >One</option>

So yours should be:
Code:
<option value="&lt;?php echo $pais-&gt;id_pais; ?&gt;" &lt;?php echo set_select('paises', $pais->id_pais); ?&gt; >&lt;?php echo $pais->nombre_pais; ?&gt;</option>
#7

[eluser]alvaroeesti[/eluser]

I have copied and pasted it just like that. I refreshed the page to get a free start, selected a country, then on purpose i entered non numeric values in a field to generate an error, which the validation catches, and when it catches it it redirects back to the form. However the country I had selected was not maintained. Don't know why or whether this set_select cannot beat a redirect to the start..


EDIT:


It is possible that this:


Code:
if($this->form_validation->run() == FALSE)
  {
  
   header("Location:http://localhost/housing/index.php/home_C/index/");

forces a total resetting as if I put the cursor in the http box and press enter
#8

[eluser]CroNiX[/eluser]
Because when you redirect, you lose all POST values. Instead of redirecting, you should just load the form view. This is normal PHP behavior. POST only works for that one request. New request (which is what redirect does)...it gets wiped out.
#9

[eluser]CroNiX[/eluser]
Code:
if ($this->form_validation->run() === FALSE)
{
  //failed, load form view to repopulate and display form validation errors
}
else
{
  //we passed...do something with the data, load a success view, redirect, etc.
}
#10

[eluser]alvaroeesti[/eluser]

Yes, correct.

I tried at the time to have the validation form point out the errors, but because I had one controller to launch a form and another controller to validate, it wouldn't work and finally I decided to send the user to the start with a redirect.


EDIT:
So I am not so much concerned about the user deliberately not entering right values, then the question would be Cronix, when I am going to do pagination, on clicking the page link, it resends the query to the db. It did not work for me because the variables which were supposed to be passed on each query were blank. Will this method of set_select save them for the pagination query to work?

That is, the important need for implementing the set_select is that it will keep the posted variables available for the pagination calls to the DB

thank you




Theme © iAndrew 2016 - Forum software by © MyBB