DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Full Service Class For Amfphp

Jonnie Spratley user avatar by
Jonnie Spratley
·
Sep. 21, 08 · · Code Snippet
Like (0)
Save
Tweet
676 Views

Join the DZone community and get the full member experience.

Join For Free
Here is a very helpful script that will allow you to create, update, delete, and read all data from your database through amfphp. When you receive the data in Flex you will have a success mapping of the value objects. Code is as follows:


mapObject( $data );
			array_push( $list, $vo );
		}		
		return $list;	
	}	


	public function getSnippets()
	{
		//We must specify our vo, because we need to map correctly
		require_once( "../vo/SnippetVO.php" );
		
		$sql = mysql_query( "SELECT * FROM ". $this->table. "" );
		
		$result = array();

		while( $snip = mysql_fetch_array( $sql ) )
		{
			//Create a new snippet vo
			$snippet = new SnippetVO();
			$snippet->snippet_id 			= $snip[snippet_id];
			$snippet->snippet_title 		= $snip[snippet_title];
			$snippet->snippet_code 			= $snip[snippet_code];
			$snippet->snippet_type 			= $snip[snippet_type];
			$snippet->snippet_created 		= $snip[snippet_created];
			$snippet->snippet_user		 	= $snip[snippet_user];
			//Result is a snippet
			$result[] = $snippet;
		}
		//Print out the result
		return $result;
	}

	//This is used for returning the created or updated snippet for flex
	public function getOne( $id )
	{
		$rs = mysql_query( "SELECT * FROM ".$this->table." WHERE snippet_id = ".$id );
		//Map the recordset to our vo
		$list = $this->mapRecordSet( $rs );
		//Return our vo
		return $list[ 0 ];		
	}
	
	//Creates a new snippet
	public function saveSnippet( $snippet )
	{
		require_once( "../vo/SnippetVO.php" );
		//Check to see if the snippet has an id of 0
		if ( $snippet[snippet_id] == 0 ) 
		{
		$query = "INSERT INTO ".$this->table."
								( snippet_title,
								snippet_code,
								snippet_type,
								snippet_created,
								snippet_user )		
								VALUES (
								'".mysql_real_escape_string($snippet[snippet_title])."',
								'".mysql_real_escape_string($snippet[snippet_code])."',
								'".mysql_real_escape_string($snippet[snippet_type])."',
								'".mysql_real_escape_string($snippet[snippet_created])."',
								'".mysql_real_escape_string($snippet[snippet_user])."')";
		if( !mysql_query( $query ) ) 
		{		
			return false;
		}				
			return $this->getOne( mysql_insert_id() );
			
			} else {
					$id = $snippet[snippet_id];
			
					$query = "UPDATE ".$this->table." SET 
					snippet_title = '".mysql_real_escape_string($snippet[snippet_title])."',
					snippet_code = '".mysql_real_escape_string($snippet[snippet_code])."',
					snippet_type = '".mysql_real_escape_string($snippet[snippet_type])."',
					snippet_created = '".mysql_real_escape_string($snippet[snippet_created])."',
					snippet_user = '".mysql_real_escape_string($snippet[snippet_user])."'
			
					WHERE snippet_id =". $id;
				
					if( !mysql_query( $query ) )
					{		
						return false;		
					}				
					//Return the created snippet
					return $this->getOne( $id );				
				}				
	}

	public function removeSnippet($id)
	{
		$sql = mysql_query( "DELETE FROM ".$this->table." WHERE snippet_id = ".$id );
		
		if( !$sql )
		{
		//	trigger_error("Unable to delete Snippets", E_USER_ERROR);
			return "There was an error removing this snippet";
		}
		else return $id;		
	}      
      

	//Save image from the given bytearray and return the path of the saved image
	public function takeScreenshot( $byteArray, $filename )
	{
		if( !file_exists( $this->output_dir ) || !is_writeable( $this->output_dir ) )
			trigger_error ( "Please create a directory first with write access", E_USER_ERROR );

		$data = $byteArray->data;
		
		//Put the File in the Directory, and Rename it, what the User wanted the Name to be.
		file_put_contents( $this->output_dir . "/$filename", $data );
	   
	   return $this->server_url . $this->output_dir . "/$filename";
	}
}
?>


And here is the Value Object 



snippet_id 	= $snip[snippet_id];
		$this->snippet_title 	= $snip[snippet_title];
		$this->snippet_code 	= $snip[snippet_code];
		$this->snippet_type 	= $snip[snippet_type];
		$this->snippet_created 	= $snip[snippet_created];
		$this->snippet_user 	= $snip[snippet_user];
	}
}
?>

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What SREs Can Learn From the Atlassian Nightmare Outage of 2022
  • How to Optimize MySQL Queries for Speed and Performance
  • A Simple Guide to Heaps, Stacks, References, and Values in JavaScript
  • Comparing Distributed Databases

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo