Solved

Clickable link on fields from mysql

Posted on 2012-04-03
7
339 Views
Last Modified: 2012-04-12
I have a user database setup in mysql and I want to list all username in my flash application, make the username click-able so I can retrieve each user information upon click on it.

I successfully got to display the list of all usersname using PHP, but I am not clear how to achieve my goal.

Any tips? (I am not strong on PHP)
0
Comment
Question by:Angha110
  • 3
  • 3
7 Comments
 
LVL 37

Expert Comment

by:CyanBlue
ID: 37804008
Simply speaking, you do need another PHP script that retrieves the information from the database based on the query string you provide.

Let's say this is what you get from the listUsernames.php file. (Format is not important.  I am just trying to describe the idea.)
1,Andy
2,Brad
3,Clark
4,Dave
5,Eric
...

Open in new window

When you get that data inside the Flash, you'll create a table like structure where you will list them like this where I am just using [ ] to indicate that they are buttons.
[ Andy ]
[ Brad ]
[ Clark ]
[ Dave ]
[ Eric ]
...

Open in new window

When you create those buttons, you will feed the id numbers associated to that person so that you can utilize that id when calling PHP script to get details.  In other words, you should create getDetails.php file that is used like getDetails?id=3 for Clark's information and such so that you can display the detail information when you get the data.
0
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37804138
There are a LOT of ways of doing it...
I need some more info:

1. Which version of Flash are you using?
2. And how are you programming Flash? In AS2 or AS3?
3. Are you using any components?
0
 

Author Comment

by:Angha110
ID: 37817368
I am using AS3.
CS5
No component

Cyan, the logic you explained is right, no doubt. But my skill on PHP & Flash is not strong.

Here is the PHP code that reads the username and it does show the result. (works fine)
I believe my first problem is how to bring these to my flash application.
Next step would be making them click-able which should trigger an event sending back some inquiry to another PHP (as you mentioned) which will retrieve the whole data for that specific (clicked) username.

So the first thing (I tried many things without success) is how do I bring those username in the loop to my flash. I don't know, maybe an array. But how do I make the connection and read those data.

<?php

$host = "-------------";
$username = "---------";
$password = "---------";
$db_name = "----------";
$tableName = "--------";

mysql_connect($host, $username, $password)
or die("...  " . mysql_error());
echo "Connected to MySQL. user: '". $username ."' <br />";
$db = mysql_select_db($db_name);
$sql = "SELECT * FROM $tableName";
$res = mysql_query($sql) or die("FAIL: $sql<br/>" . mysql_error() );
while ($row111 = mysql_fetch_assoc($res))
{
    echo $row111["username"];
    echo "' <br />";
}
?>

Open in new window

0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 7

Accepted Solution

by:
designatedinitializer earned 500 total points
ID: 37817731
Angha:

You need PHP and AS3 code to accomplish this.
I usually do it like this:
1. First, in PHP, grab the data from MySQL and generate an XML document.
2. Then, in Flash, load the XML from PHP with the aid of our friends Request, Loader and XML...

The PHP code follows. Notice you need to follow some indications that are commented. I made a simple test scenario which you must change to conform to your needs.
<?php
// Generate the XML header
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo "<XMLresponse>\n";

if(!isset($_GET['secret']) || $_GET['secret']!="hushhush"){
	// Security check...
	// The calling script must include '?secret=hushhush' in the URL
	die("<error code=\"1\" message=\"Unauthorized access attempt.\" ></error>\n</XMLresponse>");
}

/*
	Place your db stuff here, then replace the call to 
	"fake_mysql_fetch_assoc" with "mysql_fetch_assoc",
	in the while loop below.

    You might want to let Flash know when an error occurs, like so:
    die("<error code=\"2\" message=\"Could not connect to database.\" ></error>\n</XMLresponse>");
*/

/* for demonstration purposes, I'm faking here
   the $rs resource var returned by MySQL, and
   the mysql_fetch_assoc() call.
*/
$rs = array( array("username"=>"George"), array("username"=>"John"), array("username"=>"Paul"), array("username"=>"Ringo"));
function fake_mysql_fetch_assoc(&$r){
	if(count($r)>0){
		return array_pop($r);
	}else{
		return false;
	}
}

// Generate an xml element for each record,
// passing the data as attributes.
// If you have unique ids for these records,
// you might want to use the id attribute,
// because this will produce the idmap construct
// available inside Flash
while ($record = fake_mysql_fetch_assoc($rs)) {
	echo "<record username=\"".$record['username']."\"></record>\n";
}

// Close the wrapper tag
echo "</XMLresponse>";

// make sure everything is output
flush();
ob_flush();
?>

Open in new window



Now here's the AS3 code. You'll also have to adapt it to your needs.
package  {
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
 
	public class userTest extends MovieClip {
		
		
		var externalXML:XML;

		public function userTest() {
			// initialize an XML var to get data into
			var loader:URLLoader = new URLLoader();
			var request:URLRequest = new URLRequest("http://localhost/testes/phpFlash/getXMLdata.php?secret=hushhush");
			
			loader.load(request);
			loader.addEventListener(Event.COMPLETE, onComplete);
		 
		}
		
		private function onComplete(event:Event):void 
		{ 
			var loader:URLLoader = event.target as URLLoader; 
			if (loader != null) 
			{
				externalXML = new XML(loader.data);
				trace(externalXML.toXMLString());
				// do your stuff here
				
			} 
			else 
			{ 
				trace("loader is not a URLLoader!"); 
			} 
		}
			
	}
	
}

Open in new window

0
 

Author Comment

by:Angha110
ID: 37839591
This question is because I am a beginner on PHP,

Aren't we suppose to see the generated XML file if we go to the php page directly. I assume echo would display the value. I don't see anything on the screen. (My php does connect to mysql here)

Also, my flash file does not show any output either.
0
 

Author Comment

by:Angha110
ID: 37839674
This work fine. I can access to the generated XML file.

Thank you designatedinitializer
0
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37839933
great.
you might want to check out AS3 xmlsocket object too: this is great for async communication. I've made very cool (near)realtime stuff with it.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question