?
Solved

Clickable link on fields from mysql

Posted on 2012-04-03
7
Medium Priority
?
365 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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
LVL 7

Accepted Solution

by:
designatedinitializer earned 2000 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

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …

569 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