Welcome Guest, Not a member yet? Register   Sign In
Make an select with php and put selected
#1

[eluser]Diegosf93[/eluser]
I´m going crazy i want to create a droplist and show to the user thats one that its save in the db, but i have problems with the php in the view look:

Code:
<?php
  for ($i=1; $i <= count($categorias); $i++){ ?&gt;
   echo "<option name='$i' selected='  if($valueCat== $i){echo "selected";}'"    $categorias[$i]</option>"?&gt;;
} ?&gt;
  </select>


$valueCat = Hidden input with the id of the category i want to put selected.

Its 100% i have a big error but i´m with this last 3 hours, can you see where i have the error.

Sorry for my english.
#2

[eluser]CroNiX[/eluser]
Try using the form helper for that stuff. It will make your life a lot easier.
#3

[eluser]srpurdy[/eluser]
This code is really broken.

Not sure where you are intending to get the $valueCat from but this is atleast a bit cleaner code below without the form_helper.

Code:
<select name="my_select_field">
&lt;?php for($i=1; $i <= $count($categories); $i++):?&gt;
<option value="&lt;?php echo $categories[$i];?&gt;" &lt;?php if($valueCat == $i):?&gt;selected="selected"&lt;?php endif;?&gt;>&lt;?php echo $categories[$i];?&gt;</option>
&lt;?php endfor;?&gt;
</select>
#4

[eluser]skunkbad[/eluser]
Yeah, use the form helper. Like CI, it's meant to make your job easier. If you are against it, then maybe something like this in a helper:

Code:
&lt;?php

if ( ! function_exists('create_select_options'))
{
function create_select_options( $arr, $assoc=FALSE, $selected=FALSE )
{
  $html = '';

  // If the array is not an associative array
  if( $assoc === FALSE )
  {
   foreach( $arr as $x )
   {
    // If there is not a selected option
    if( $selected === FALSE )
    {
     $html .= '<option value="' . $x . '">' . $x . '</option>';
    }

    // Else there is a selected option
    else
    {
     // If this is the selected option
     if( $selected == $x )
     {
      $html .= '<option selected="selected" value="' . $x . '">' . $x . '</option>';
     }

     // Else this is not the selected option
     else
     {
      $html .= '<option value="' . $x . '">' . $x . '</option>';
     }
    }
   }
  }

  // Else the array is a standard array
  else
  {
   foreach( $arr as $k => $v )
   {
    // If there is not a selected option
    if( $selected === FALSE )
    {
     $html .= '<option value="' . $k . '">' . $v . '</option>';
    }

    // Else there is a selected option
    else
    {
     // If this is the selected option
     if( $selected == $k )
     {
      $html .= '<option selected="selected" value="' . $k . '">' . $v . '</option>';
     }

     // Else this is not the selected option
     else
     {
      $html .= '<option value="' . $k . '">' . $v . '</option>';
     }
    }
   }
  }

  return $html;
}
}
#5

[eluser]Diegosf93[/eluser]
But the form helper permits put id to the options or only to the select?
#6

[eluser]Diegosf93[/eluser]
[quote author="srpurdy" date="1348530177"]This code is really broken.

Not sure where you are intending to get the $valueCat from but this is atleast a bit cleaner code below without the form_helper.

Code:
<select name="my_select_field">
&lt;?php for($i=1; $i <= $count($categories); $i++):?&gt;
<option value="&lt;?php echo $categories[$i];?&gt;" &lt;?php if($valueCat == $i):?&gt;selected="selected"&lt;?php endif;?&gt;>&lt;?php echo $categories[$i];?&gt;</option>
&lt;?php endfor;?&gt;
</select>
[/quote]

Thanks this work for me, sorry for the double post!

Always good results in this forum!




Theme © iAndrew 2016 - Forum software by © MyBB