Jump to content

Recommended Posts

Hi, quite new to classes and trying to get my head round them by playing with some silly examples :)

 

In my config include file is this:

 

  class clsHTML {
    var $arrGenders=array('X','Male','Female','Both');
    function makeoptsGender() {
      $optGender='';
      for ($i=1;$i<count($this->$arrGenders);$i++) {
        $optGender.='<option value="'.$i.'">'.$this->$arrGenders[$i].'</option>';
      }
      return $optGender;
    }
  }

 

In one of the main scripts I've got code like this:

 

  $clsHTML=new clsHTML();
  $optGender=$clsHTML->makeoptsGender();

 

No matter what I try and do I can't get anything returned by my function. I've even tried using echo inside the makeoptsGender() function to display text and nothing. What I should be getting returned is something like this:

 

<option value="1">Male</option><option value="2">Female</option><option value="3">Both</option>

 

If anyone is able to help point out what I've done wrong I'd be really grateful, thanks.

change

$this->$arrGenders

to

$this->arrGenders

 

<?php
class clsHTML {
    var $arrGenders=array('X','Male','Female','Both');
    function makeoptsGender() {
      $optGender='';
      for ($i=1;$i<count($this->arrGenders);$i++) {
        $optGender.='<option value="'.$i.'">'.$this->arrGenders[$i].'</option>';
      }
      return $optGender;
    }
  }
?>

<?php
class clsHTML {
    var $arrGenders=array('X','Male','Female','Both');
    function makeoptsGender() {   
      
      $optGender="<select>";
      foreach ($this->arrGenders as $value) {
      	$optGender.="<option value=$value>$value</value>";
      }
      $optGender.="</select>";
      
      return $optGender;
    }
  }
  
  $clsHTML=new clsHTML();
  $optGender=$clsHTML->makeoptsGender();
  echo $optGender;
  
  ?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.