SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Jul 2009
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    This code is not storing the string

    This class is supposed to take in an array, and store
    "<option>3</option>" into $sizes.

    However, a print_r of $sizes shows that its only storing the number. Can someone tell me why?

    PHP Code:
    class getSizes{

      public 
    $item;
      public 
    $sizes;

      public function 
    __construct($item){
        
    $this->item $item;
        
    $this->sizes = Array();
      }

      public function 
    displaySizes($item){
        
    $this->storeInArray($item'XS');
        
    $this->storeInArray($item'S');
        
    $this->storeInArray($item'M');
        
    $this->storeInArray($item'L');
        
    $this->storeInArray($item'XL');
        
        return 
    $this->sizes;
      }

      public function 
    storeInArray($item$sizeType){
        if (
    $item[0][$sizeType] != ""){
          
    $this->sizes[$sizeType] = "<option>{$item[0][$sizeType]}</option>";
        }
      }  


  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have u tried removing the curly brackets?

  3. #3
    SitePoint Evangelist stonedeft's Avatar
    Join Date
    Aug 2009
    Posts
    586
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That depends on how you called the class. You might as well post that too
    Don't Panic

  4. #4
    SitePoint Addict
    Join Date
    Jul 2009
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by solas View Post
    Have u tried removing the curly brackets?
    I tried after reading this. But it doesn't work.

    Quote Originally Posted by stonedeft View Post
    That depends on how you called the class. You might as well post that too
    Ok, here it is. Actually I modified it cause I realized I could simplify it. But I can't edit my earlier post so I'll just go ahead and post the modified version here as well.

    PHP Code:
    $sizes = new getSizes();
    $sizes->getHTMLOptions($item); 
    slightly modified version
    PHP Code:
    class getSizes{

      public 
    $item;
      public 
    $sizes;

      public function 
    __construct(){
      }

      public function 
    getHTMLOptions($item){
        
    $this->storeInArray($item'XS');
        
    $this->storeInArray($item'S');
        
    $this->storeInArray($item'M');
        
    $this->storeInArray($item'L');
        
    $this->storeInArray($item'XL');
        
        return 
    $this->sizes;
      }

      public function 
    storeInArray($item$sizeType){
        if (
    $item[0][$sizeType] != ""){
          
    $this->sizes[$sizeType] = "<option>{$item[0][$sizeType]}</option>";
        }
      }  


  5. #5
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $sizes = new getSizes();
    $sizes->getHTMLOptions($item); 
    So where is $item defined (outside the class)? What is it?
    Ian Anderson
    www.siteguru.co.uk

  6. #6
    SitePoint Addict
    Join Date
    Jul 2009
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteguru View Post
    PHP Code:
    $sizes = new getSizes();
    $sizes->getHTMLOptions($item); 
    So where is $item defined (outside the class)? What is it?
    Yes its out of the class.

    In fact, I found out I made a silly mistake. It DID actually store the string. But because its a HTML tag, that's why its not showing when I did a print_r.

    Thanks for all the help though

  7. #7
    SitePoint Evangelist stonedeft's Avatar
    Join Date
    Aug 2009
    Posts
    586
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    before parsing it with the class echo $items to see if it has a value
    Don't Panic


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •