Page 1 of 1

i cant get it to output

Posted: Fri Sep 18, 2009 7:36 am
by gotornot
Im trying to run a simple if command and this doesnt seem to work im trying to get it to hiighlight the option already selected in the db any ideas why?

function selected()
{
global $selectedno;
//for loop to loop through 10
$a = 11;
for ($i = 0; $i <10; $i++)
{
$a = --$a;
$num = $a;
if ($selectedno == $num)
{
$bash = ' selected="selected"';
}
echo '<option value="' . $a . '"' . $bash . '>' . $a . ' ' . $selectedno . ' </option>';
}
}

Re: i cant get it to output

Posted: Fri Sep 18, 2009 7:52 am
by Eric!
The way your function was setup once $bash is defined as SELECTED the echo would always print SELECTED. Either undefine $bash when it isn't in use or just echo something differently like below:

Code: Select all

function selected()
{
  global $selectedno;
  //for loop to loop through 10
  $a = 11;
  for ($i = 0; $i <10; $i++)
  {
    $a = --$a;
    $num = $a;
    if ($selectedno == $num)
    { 
      echo '<option value="' . $a . '" SELECTED>' . $a . ' </option>';
    }
    else
    {
      echo '<option value="' . $a . '">' . $a .  ' </option>';
    }
  }
}
FYI -- you can also run your for loop from 10 to 0 with $i-- and skip the whole $a thing you're doing. I also don't think you want $selectedno echoed everytime so I modified the echo. You can also drop the use of $num and just use $a too. And one last thing, instead of using a global, why not pass the selectedno to the function?