Drawing an optionbox in a class function

Alles, was PHP betrifft, kann hier besprochen werden.

Drawing an optionbox in a class function

Postby sprinkel » 27. October 2010 16:58

Dear developers,

I'm having a problem when I try to use an optionbox whithin a class function.

I'm using the code below:

Code: Select all
class golfbaan
{
   public $golfbaan = NULL;
   public $aantal_lussen = NULL;
    public $banen;
    private $retrieve_banen_sql = "SELECT baan FROM baan_055 ORDER BY baan";
    private $result_retrieve_banen;
   private $verbinding;
   private $data_baan;
   public $baan;
   public $optiontekst = "-- Selecteer --";
   public $result_baan;
 
    function RetrieveBanen()
    {
       $this->verbinding = new verbinding;
      $this->verbinding->verbinden();
       $this->verbinding->selecteren();
       $this->result_retrieve_banen=mysql_query($this->retrieve_banen_sql)or die("Query om banen op te halen geeft fout: ".mysql_error());
       return $this->result_retrieve_banen;
    }
    function ShowOptionbox()
   {
      echo "<form name=\"golfronde\" method=\"post\" style=\"color:#00F\">";
      echo "<fieldset style=\"border-color: #0CF; color:#00F\">";
      echo "<legend style=\"color:#F90\">Invoeren S.V.P.</legend>";
      echo "<table width=\"700\" border=\"0\" cellspacing=\"0\" cellpadding=\"1\">";
      echo "<tr>";
      echo "<th scope=\"col\" align=\"left\" style=\"font:Verdana, Geneva, sans-serif; font-size:14px\" width=\"220\"></th>";
      echo "<th scope=\"col\" align=\"center\" style=\"font:Verdana, Geneva, sans-serif; font-size:14px\" width=\"20\"></th>";
      echo "<th scope=\"col\" align=\"left\" style=\"font:Verdana, Geneva, sans-serif; font-size:14px\" width=\"180\"></th>";
      echo "<th scope=\"col\" align=\"left\" style=\"font:Verdana, Geneva, sans-serif; font-size:14px; color:#F00\" width=\"240\"></th>";
      echo "</tr><tr><td>Golfbaan</td><td> : </td><td>";
      echo "<select name=\"baan\" style=\"background-color:#D5FFFF; font-size:14px; color:#00F; font:Verdana, Geneva, sans-serif\" onchange=\"verwerk()\">";
      echo "<option value=\"" . $this->optiontekst . "\"";
      $this->baan = $baan;
      if ($this->baan == NULL)
      {
         echo "selected=\"selected\"";
      }
         echo ">-- Selecteer --</option>";
         $this->result_baan = $this->RetrieveBanen();
      while ($this->data_baan=mysql_fetch_array($this->result_baan))
      {
         echo "<option value=\"$this->data_baan[baan]\" ";
         if($this->baan == $this->data_baan['baan'])
          {
              echo "selected=\"selected\"";
          }
          echo (">$this->data_baan[baan]</option>");
      }
   }
}


The line echo "<option value=\"$this->data_baan[baan]\" ";" is cousing a problem because the variable "$this->data_baan[baan]" isn't interpreted as a variable.

The optionbox gives the right number of selected records, but the content in the optionbox is "Array[baan]" instead of the attribute value.

Am I using OO wrong or can sombody give me a hint?

Thank you in advance
cees fortuin
sprinkel
 
Posts: 16
Joined: 29. August 2009 01:38
Location: Netherlands
Operating System: Windows Vista

Re: Drawing an optionbox in a class function

Postby WilliL » 27. October 2010 17:55

I prefer when using html-tags with option, quote and double quote signs, and concate variables with '.'
echo '<option value="'.$this->data_baan[baan].' " ';
Willi
WilliL
 
Posts: 660
Joined: 08. January 2010 10:54
Operating System: Win7Home Prem 64 SP1

Re: Drawing an optionbox in a class function

Postby sprinkel » 27. October 2010 18:55

Thanks for your reply WilliL,

I changed the code but the behavior is stil the same. The variable "$this->data_baan[baan]" isn't interpreted as a variable

regards,
Cees
sprinkel
 
Posts: 16
Joined: 29. August 2009 01:38
Location: Netherlands
Operating System: Windows Vista

Re: Drawing an optionbox in a class function

Postby Altrea » 27. October 2010 19:28

Code: Select all
echo "<option value=\"{$this->data_baan['baan']}\" ...


Code: Select all
echo "<option value='" .$this->data_baan['baan']. "' ...
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Drawing an optionbox in a class function

Postby sprinkel » 27. October 2010 20:29

Hi Altrea,

Thank toy for your response, this solution didn't affect the behavoir, "$this->data_baan[baan]" is still not interpreted as a variable. The reason is probably that I use the optionbox within a method of a class. I'm not sure this is allowed.

Anybody else a possible solution?

Regards
Cees
sprinkel
 
Posts: 16
Joined: 29. August 2009 01:38
Location: Netherlands
Operating System: Windows Vista

Re: Drawing an optionbox in a class function

Postby Altrea » 27. October 2010 21:00

Code: Select all
$this->baan = $baan;

What is this line for?
That doesn't make sense.
$baan is not defined.
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Drawing an optionbox in a class function

Postby sprinkel » 28. October 2010 08:27

I forgot to clear that line, you are right, it doesn't make sence. it was the rest of a Get function. but it isn't the cause of the problem.
Somehow PHP isn't interpreting a string with quotes, in an echo statement, well, if that string is part of function in a class defenition.
sprinkel
 
Posts: 16
Joined: 29. August 2009 01:38
Location: Netherlands
Operating System: Windows Vista

Re: Drawing an optionbox in a class function

Postby Nobbie » 28. October 2010 08:44

sprinkel wrote:Somehow PHP isn't interpreting a string with quotes, in an echo statement, well, if that string is part of function in a class defenition.


No, it is not the problem of echo or of a class definition.

It's simply a basic question of the syntax definition of array elemnts embedded in quoted strings. You should read a PHP manual about that. By the way, another problem is that your array element is not specified correctly, as the key MUST NOT be a constand (without any quotes) but a string:

Wrong syntax:

$array[key]

Right syntax;

$array['key']

In some cases PHP will freely interprete the constant key as a string "key" (switch on error messaging and you will receive the appropriate notice).
Nobbie
 
Posts: 13170
Joined: 09. March 2008 13:04

Re: Drawing an optionbox in a class function

Postby sprinkel » 28. October 2010 14:09

Thank you all,

The right syntax is: echo "<option value=\"".$this->data_baan['baan']."\">".$this->data_baan['baan']."</option>";

Regards
Cees
sprinkel
 
Posts: 16
Joined: 29. August 2009 01:38
Location: Netherlands
Operating System: Windows Vista


Return to PHP

Who is online

Users browsing this forum: No registered users and 7 guests