i cant get it to output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gotornot
Forum Commoner
Posts: 54
Joined: Fri Jul 31, 2009 2:30 am

i cant get it to output

Post 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>';
}
}
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: i cant get it to output

Post 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?
Post Reply