8 Replies - 9429 Views - Last Post: 13 December 2011 - 09:20 AM Rate Topic: -----

#1 mattrmclaren   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 10-September 11

Data from form submission vs. API

Posted 11 December 2011 - 08:00 AM

Right now I'm trying to tackle the problem of dealing with data either a) from the Graph API or B) provided by the user manually through form submission. To keep it simple, this is how I'm currently setting up the class, not accounting for a manual form submission:

class user {
        
    public $collegemajor;
    
    public function __construct($collegemajor)
    
    $this->collegemajor = $collegemajor;
    
    $collegemajor = isset($user_profile['education'][0]['concentration'][0]['name']) ? $user_profile['education'][0]['concentration'][0]['name'] : null ;
    $objUser = new user ($collegemajor);
    
    }


The goal is to set $collegemajor as the Graph API value if it's available or as the posted form submission if the API data isn't available. Assume if the data isn't available from the API, the user has already submitted the major via form. What is the best way to do this? Is it a getter and setter method? If so I'm lost on how I would construct it. How about adding it to this line:

$collegemajor = isset($user_profile['education'][0]['concentration'][0]['name']) ? $user_profile['education'][0]['concentration'][0]['name'] : null ;


The value of the form submission, if it exists, would be something like this:

$_POST['major'];


Here's my failed attempt below. It didn't return the form submitted value as planned. Also, the form data could be inserted into a database. Would it be easier to take this approach rather than a post method? Any help would be greatly appreciated:

class user {
        
    public $collegemajor;
    
    public function __set($collegemajor, $value) {
            if (isset($this->collegemajor)) {
                return $this->collegemajor;
            } else {
                return $_POST['major'];
            }
        }
    
    public function __construct($collegemajor) {
    
    $this->collegemajor = $collegemajor;
    
    }
        
    public function echos () {
        echo "$this->collegemajor";
        }
    }
    
    ?>


Is This A Good Question/Topic? 0
  • +

Replies To: Data from form submission vs. API

#2 codeprada   User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: Data from form submission vs. API

Posted 11 December 2011 - 12:03 PM

The functions __get and __set would work great here. However I'm guessing you don't want to limit __set to just the college major. A user may have a bunch of other attributes so your magic methods should always be flexible. Have a look at this example
<?php
	class user
	{
		private
			$college_major
			// .. other properties
		;
		
		/**
		 * $data should be in the format of
		 * 		array('college_major' => 'Computer Science')
		 */
		public function __construct(array $data)
		{
			$this->setData($data);
		}
		
		public function setData(Array $data)
                {
			if($data != NULL)
                           foreach($data as $key => $value)
                               if(property_exists($this, $key))
                                  $this->$key = $value;
                        return $this;
                }
		
		
		public function __set($property, $value)
                {
                        if(property_exists($this, $property))
                            $this->$property = $value;
            
                        return $this;
                }
		
		public function __get($property)
                {
                       if(property_exists($this, $property))
                            return $this->$property;
                
                       return NULL;
                }
	}
?>



Also don't hardcode the $_POST['major'] into your object. Objects that store data should be independent on how the data is retrieved. What if it's a PUT request instead of POST?

*For some strange reason my code is always unaligned whenever I paste it within the browser. -__-

This post has been edited by codeprada: 11 December 2011 - 12:04 PM

Was This Post Helpful? 1
  • +
  • -

#3 mattrmclaren   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 10-September 11

Re: Data from form submission vs. API

Posted 11 December 2011 - 06:13 PM

I'm just a little lost. What is the $data variable? In the Graph API data is stored like this:

$user_profile['education'][0]['concentration'][0]['name']

What is the value of the $data variable and which array are you referring to, the array from the Graph API? And what would the object instance look this in this example? ie, how would the following change?

$collegemajor = isset($user_profile['education'][0]['concentration'][0]['name']) ? $user_profile['education'][0]['concentration'][0]['name'] : null ;

$objUser = new user ($collegemajor);


I'm sorry to be a pain but this is the main obstacle preventing me from moving forward with the project. If the user has their major listed on Facebook or they provide it manually by submitting a form (which is then stored in a database). So how do I create a setter method that checks to see if the Graph API value is set or, alternately, if the database value is set. I've looked at tons of get/set methods but none of them seem to be making sense to me.
Was This Post Helpful? 0
  • +
  • -

#4 codeprada   User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: Data from form submission vs. API

Posted 11 December 2011 - 07:17 PM

I think you've jumped beyond your knowledge base here. $data has no value, it is a parameter. Note that $data is an associative array which the index being the property name and the value well the value. With that said if I wanted to set the college major I would create an array like this
$data = array('college_major' => $user_profile['education'][0]['concentration'][0]['name']);



You're also looking at the wrong magic method if you want to verify if a property is set or not. __isset is the magic method for checking if object properties exist. Since $user_profile['education'][0]['concentration'][0]['name'] can be NULL we would use the __isset magic method to check if it is NULL or not. If it happens to be NULL then you'll have to fetch the data from the user via a form.

Brush up on your object oriented skills. Go through the tutorials here and it will all be a lot clearer to you.
Was This Post Helpful? 0
  • +
  • -

#5 mattrmclaren   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 10-September 11

Re: Data from form submission vs. API

Posted 11 December 2011 - 09:46 PM

class user {
        
    public $college_major;
    
    public function __construct($college_major)

    public function __construct(array $data)
		{
			$this->setData($data);
		}
		
		public function setData(Array $data)
                {
			if($data != NULL)
                           foreach($data as $key => $value)
                               if(property_exists($this, $key))
                                  $this->$key = $value;
                        return $this;
                }
		
		
		public function __set($property, $value)
                {
                        if(property_exists($this, $property))
                            $this->$property = $value;
            
                        return $this;
                }
		
		public function __get($property)
                {
                       if(property_exists($this, $property))
                            return $this->$property;
                
                       return NULL;
                }
	}
    
 

        if (isset($user_profile['education'][0]['concentration'][0]['name'])) {
            $data = array('college_major' => $user_profile['education'][0]['concentration'][0]['name']);
        } else {
            $data = array('college_major' => $row['collegemajor']);
        }

        $objUser = new user ($college_major);

    


Not sure if I'm way off here but maybe you get where I'm going with this. You saw from my original post how I'm currently setting the value. Do I need to completely start from scratch or are there simple adjustments I can make to the original structure?
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4245
  • View blog
  • Posts: 13,452
  • Joined: 08-June 10

Re: Data from form submission vs. API

Posted 12 December 2011 - 03:57 AM

I donít see the point of using __get()/__set() here since they donít work on public properties and youíre not having data otherwise inaccessible (e.g. data stored in an array somewhere). even the properties you create with setData() are public so that the magic methods wonít trigger.
Was This Post Helpful? 0
  • +
  • -

#7 mattrmclaren   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 10-September 11

Re: Data from form submission vs. API

Posted 12 December 2011 - 06:16 PM

View PostDormilich, on 12 December 2011 - 03:57 AM, said:

I donít see the point of using __get()/__set() here since they donít work on public properties and youíre not having data otherwise inaccessible (e.g. data stored in an array somewhere). even the properties you create with setData() are public so that the magic methods wonít trigger.


This problem is driving me insane. If the value is set in the Graph API, set the object property accordingly, if the value isn't set in the Graph API, set the property to the value stored in the database (from the form submission). I've looked at dozens of different tutorials but none of them address a problem like this. Is this even possible the way I'm describing?
Was This Post Helpful? 0
  • +
  • -

#8 codeprada   User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: Data from form submission vs. API

Posted 12 December 2011 - 10:40 PM

The problem is you don't have a solid foundation in PHP's OOP. If a property is public then it is accessible without the need of any functions. The __get and __set function are only needed on private and protected properties.

It is very possible. Did you check out the link I posted in my #4 post?
Was This Post Helpful? 0
  • +
  • -

#9 mattrmclaren   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 10-September 11

Re: Data from form submission vs. API

Posted 13 December 2011 - 09:20 AM

I did watch all the videos and read the tutorial.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1