Solved

Clickable link on fields from mysql

Posted on 2012-04-03
7
341 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

830 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