Archive

Posts Tagged ‘HTML’

A Flash gallery frontend

May 29th, 2009

 

I wrote a flash gallery front-end a week or so back. There’s a picture of it above. Just kidding it’s an actual instance! Some of the thumbnails might take a while to load. That is because they are not thumbnails but giant, giant images.

It uses ExternalInterface and Javascript to display the descriptions in the HTML. I’ll post the relevant code with an explanation next time, but let me tell you right now – having more than one gallery per page doesn’t pose a problem ( programmatically ).

The gallery is described by XML, and added to a page with a simple Javascript call.

Dev , , , , ,

Using ExternalInterface to communicate between Javascript and Actionscript 3: Or, I want Paypal buy now buttons in Flash

May 27th, 2009

The statement that Flash TextAreas ( TextFields, etc. ) can display HTML can be deceptive. They can certainly display a subset of HTML. But this is a very limited subset, and for good reason.

While it’s easy to understand why this is the case, it can still be frustrating. Say, for example, you’re building an image gallery and would like to have some of Paypal’s buy-now buttons in image descriptions. A Paypal button can be perhaps a deceptive thing – it’s tiny forms whose only visible element is the buy-now image – which cannot easily be accurately displayed by Flash.

But don’t worry, especially if your Flash will be displayed within a browser, for that is a thing built to display HTML. In fact, it’s one of its primary and most useful purposes.

The trick here is to get Flash to communicate to the browser what HTML to display, and how to do it. It turns out this is relatively easy to accomplish.

Here we make the following assumption: Your SWF file knows what HTML to display, and when. In my mind, this is a pretty safe assumption to make.

Okay, so I’ll break the method down into steps:
Step 1: Flash tells javascript to display some HTML.

Okay that’s about it.

You’ll have to write the Javascript to display the HTML where you want it, but that’s easy enough.

Flash provides an External Interface class ( flash.external.ExternalInterface ) that allows for communication between Actionscript 3 and the Flash Player container – in this case the web page and its Javascript.

You’ll have to include the ExternalInterface in your class file, and there are some checks you can (and probably should) diligently perform, but the important Actionscript 3 is:

import flash.external.ExternalInterface;
...
var html:String = "<p>This is some html.</p>";
if (ExternalInterface.available) {
	ExternalInterface.call("writeHTML", html );
}

The Javascript function writeHTML will be called with the parameter html. So it’d probably be good if there was such a function:

function writeHTML( str ) {
	document.write( str );
}

A better Javascript function would be, well.. better.. but, you know, brevity and all that.

That’s basically all there is to it. Next time blog I’ll provide a more concrete example of ExternalInterface in use (one that uses multiple parameters in its Javascript call). And if you want, post the applicable code?

As a side note: if you’re loading the HTML via XML, always remember to wrap it in CDATA.

Dev, being helpful to people less experienced than myself in the ways of the world; mostly in the ways of the web actually , , , ,

A sliding drawer type thing feat. jquery

May 23rd, 2009

A while ago a friend of mine realized he had so many employees that listing them all on a single page would make for one of great length. A strange problem to have.
Using some simple CSS and Javascript we can hide the bulk of the information and show only what is necessary. You know, for increased usability and all that:

A definitions list provides the perfect data structure for our purposes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<dl class="drawerify">
	<dt>A bird</dt> 
	<dd>
		<img src="images/thumb-bird.jpg">This is a picture of a bird sitting an a tree, and looking quite proud of that.
	</dd>
 
	<dt>A cave</dt> 
	<dd>
		<img src="images/thumb-cave.jpg">This is the description of the cave.. Tor lives here, and is a Zombie.
 
	</dd>
 
	<dt>A zombie</dt> 
	<dd>
		<img src="images/thumb-zombie.jpg">Poker was pretty popular for a while, even the zombies got into it.	
	</dd>
 
	<dt>Bobby</dt> 
	<dd>
		<img src="images/thumb-bobby.jpg">This is a picture of bobby.  I actually have a giant version of this framed on a wall over to the right.	
	</dd>
 
</dl>
<div id="info_container"></div>

For each entry, we have a title (dt) and the data itself (dd). After the definition list, we have an empty div identified as info_container. This is where we’ll display the appropriate data.

The HTML itself is pretty minimal and hopefully clear, which is just the way we like it.

The javascript is a bit more complicated:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
google.load("jquery", "1");	// We want jquery... let's say version 1?
google.setOnLoadCallback( // Once google tells us JQuery has finished loading, we can do some stuff
	function() { // This is the annonymous function google will call-back
		$('dl.drawerify dt').click( //Every time we click a dt in the drawerified definitions list
			function() {
				var new_one = $('dl.drawerify dt').index(this);
				if (selected == new_one) return;  // Don't do anything if we've clicked on the one that's already open
				$('dl.drawerify dt:nth('+selected+')').removeClass('current');
				selected = new_one;
				$(this).addClass('current');
				show_info($(this).next('dd').html() ); // Fill the info_container
			}
		);
		$('dl.drawerify dt:first').click(); // Initially, we want the first one shown (so we just fake-click on it)
	}
);
 
var selected = -1; // The index of the currently displayed entry in the definitions list
 
// Fill the info_container with the appropriate data
function show_info( desc ) {
	var container = $('#info_container');
	if (container.html().length > 0) {
 
	// Animate the drawer retracting	
	  container.animate({
		width: "0px"
	  }, 300, "swing", function() { container.html(''); show_info( desc ); } );
 
	} else {
		container.html("<div class=\"inner\">" + desc + "</div>"); // Fill the container
 
		// Animate the drawer extending
		container.animate({
			width: "250px"
		}, 300, "swing" );
	}
}

The first line is us telling Google that we want to use JQuery. We could just include JQuery ourselves, but man we are way too lazy for that.

We want only one dt tag to have the current class applied to it at a given time. Lines 8 removes the class from the previously selected dt and line 10 adds it to the new one, using various jquery selector strings.
In line 11, we pass the contents of the dd immediately following the current dt to the show_info function.

Now comes the animation part. Jquery provides a function called animate. We can use it to animate things!
Starting at line 23, the logic goes a little something like this:
If there there is already information in the info_container, we want to retract the drawer. If not, we’re good to extend it once we’ve filled it with the new content.

A lot of jquery functions take callbacks as optional parameters, which is useful for us because once a drawer has closed we’d like to clear it of content and then fill it with new content and open it. That last part sounds an awful lot like what we’d do anyway if the drawer were empty. So maybe all we really have to do is clear it of content, and then have the show_info function call itself with its original parameters? Sounds good to me, and we can totally do it with another anonymous function. That’s what line 28 is about.

And now the Javascript is done! That wasn’t so complicated? I am a big fan of anonymous functions.

Next comes the CSS. This is where we’ll define what the drawer-thing looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
dl.drawerify, #info_container {
	height: 108px;
	background-color: #fff;
	padding: 10px;
}
dl.drawerify {
	width: 140px;
	float: left;
	margin: 0px;
}
dl.drawerify dt.current {
	color: #ffffff;
}
 
dl.drawerify dd {
	display: none;
}
dl.drawerify dt {
	cursor: pointer;
	font-weight: bold;
	background: #324e63;
	color: #b3c4d2;
	font-family: arial, verdana;
	padding: 3px;
	padding-left: 10px;
	margin-bottom: 1px;
	font-size: 16px;
}
dl.drawerify dt:hover {
	background-color: #233544;
}
#info_container {
	width: 0px;
	float: left;
	overflow: hidden;
	font-family: verdana;
	font-size: 8pt;
	padding-left: 0px;
	color: #324e63;
}
#info_container .inner {
	width: 220px;
}
#info_container img {
	margin-right: 10px;
	float: left;
}
body {
	background-color: #EDEFF0;
}

The most important parts of the css are:
Line 7 and 8 let us display info_container to the left of the definitions list, rather than below it.
Line 19 tells the browser to display the little hand icon when you mouse-over the definition titles instead of an arrow (like when you mouse-over a link).
Line 35 and 42 allow the animation to work nicely. 35 tells the browser to clip anything that exceeds the dimensions of the info_container, and 42 sets the width of the inner div. By setting it, we prevent its width from automatically changing to fit within info_container. That way its content doesn’t get re-arranged during the animation.

The complete code is available, though everything important can be found above.

Dev, being helpful to people less experienced than myself in the ways of the world; mostly in the ways of the web actually ,

PHPing a very simple form

March 5th, 2009

We’re going to create the same form as last time, but enhance it a bit using PHP and MySQL. Where applicable, I’ve linked parts of the code to the appropriate documentation where you can find more information and possibly cake.

We’ll be using more than one PHP file, just to show off require_once.

The first step is to connect to the database. I’ll include a couple of simple functions for dealing with the resources returned.

This file is called mysql.inc.php:


<?php

This is the magical start tags that ends with ?>. Between these two guys is where we place our PHP code. You can have more than one PHP block per file, as you'll see below, and the "php" succeeding the ? character isn't actually necessary.

//configuration information

These two slashes start a single-lined comment. Anything following those two characters, until the line ends is ignored by PHP - you know, for documentation? :p

$server = 'localhost';
$username = '';
$password = '';
$database = 'quizes';

Variables begin with the $ character, and all statements are terminated using the ; character.

//connect to the database
mysql_connect($server, $username, $password);
mysql_select_db($database);

//retrieve rows from a table as an array of associative arrays
function sql_getRows($query) { //$query is a line of SQL we will pass to the function
   $rows = mysql_query($query);
   return sql_assoc($rows); //this function is defined 3 lines from now
}

//turn a mysql resource into an array of associated arrays
function sql_assoc($resource) {
   $i = 0;
   $values = array();
   while ($row = mysql_fetch_assoc($resource)) {
     foreach($row as $k => $v) $values[$i][$k] = $v;
     $i++;
   }
   return $values;
}
?>


 

Okay, and now for the form! We’ll call this file index.php:


<?php

require_once
('mysql.inc.php');

We're telling php to require the inclusion of the file we just made (above). We could have used include_once, but it won't work without that file anyway.

//create the html for a select box populated with clients from a database
//we want all of the clients

$clients = sql_getrows('SELECT client_id, client_name FROM clients');

This is the SQL select query we're sending to the database through our sql_getrows function. Since we want all the clients, we left off the WHERE clause. We are also explicitly specifying fields by name - the * character can be used if you'd like to grab all the fields.

The format of the data we get back will be an array of associative arrays - so what we are getting is a nested array. The first level is indexed numerically; the second by they database table field name. To access the client_id of the first record returned, you would use the syntax $clients[0]['client_id']. Or, you can use a loop to run through the data:

$select_html = "<select name=\"clientId\">\n";

Right now we're creating a string (stored as the variable $select_html) which consists of the HTML necessary for a select box.

\ is an escape character. One set of quotes tells PHP where our string starts and where it ends, another is part of the HTML we'll be outputting. If we have quotes within quotes, we have to escape them using the \ character. Similarly, it is used for special characters like \n - which is the new line character. The new lines won't change how your form is displayed within the web browser, but they can be important if you're going to be viewing the source created. The same goes for \t, except that it is the tab character. Outputting well-formatted code can help with debugging.

foreach($clients as $c) {
$select_html .= "\t<option value=\"". $c['client_id'] ."\">". $c['client_name'] ."</option>\n";

Foreach loops can take the form ($array as $key=>$value), or just ($array as $value) - this is the latter. Each time it loops through the clients list, we use the variable $c to reference that client's information. $c is itself an associative array keyed by the client table's field names (that we requested via SQL).

}
$select_html .= "</select>\n";

the . character is for string concatenation. Placing it before the = symbol is the same as writing $select_html = $select_html . "</select>\n";. So we're just appending to the string already held in the variable.

?>

Here ends our first php block for the file

<h1>Quiz maker</h1>

<form method="post" value="form-handler.php" >

<p>Client: <? echo $select_html; ?></p>

This is a single statement within a php block. Echo just tells php to output the value of $select_html variable here.

<p>Name: <input type="text" name="QuizName" value="MyFirstQuiz" /></p>
<p>Date: <input type="text" name="QuizDate" value="<? echo date("Y-m-d"); ?>" /> <small>YYYY-MM-DD</small></p>

<table>
<tr>
<th>Question</th><th>Answer</th>
</tr>
<tr>
<td><input type="text" name="questions[]" /></td>
<td><input type="text" name="answers[]" /></td>
</tr>
<tr>
<td><input type="text" name="questions[]" /></td>
<td><input type="text" name="answers[]" /></td>
</tr>
</table>

<input type="submit" value="Create »" />
</form>


Now we have a form!
Next time, maybe the form submission handler? Or maybe a blog about perfume? Or maybe installing Magento on Dreamhost? I don’t even know!

Also, it’s neumann‘s birthday.

Dev, being helpful to people less experienced than myself in the ways of the world; mostly in the ways of the web actually , ,