Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Clickable link on fields from mysql

Posted on 2012-04-03
7
Medium Priority
?
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 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