Solved

Clickable link on fields from mysql

Posted on 2012-04-03
7
330 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now