Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Thursday, 12 April 2012

How to get the REAL date into Flash

Flash has a Date object, that can give you the date and time, but it gives you the date and time that is on the users computer.

e.g.
var dt:Date = new Date();
trace(dt);

This will trace out whatever time the user's computer is set to, whether it is 5mins slow, or 10 years fast.

If this date/time is being used for some kind of authentication i.e. to see if the deadline is past, then you should load the time from the server.

var ld:LoadVars = new LoadVars();
ld.onLoad = function(succ:Boolean) {
        dt_txt.text = this.servertime;
}
ld.sendAndLoad('servertime.php',ld,'POST');

On the servertime.php file:


$today = date('YmdHis');
echo "&servertime=$today";
?>

(Sometimes I have issues with the data being echoed out, try removing the ampersand from the front, or not.)

The reason why I use the date('YmdHis') is because it gives you a number that is perfect for sorting, and a recognised datetime stamp for mysql, if you ever need it.

4.30pm (and 22 seconds), 5/11/2012   =    20121105163022

If you need to sort the dates, you can do a numeric sort. If you want to check if something happened before Jun 2012, you look to see if the number is smaller than 20120600000000, and so on.

Thursday, 22 March 2012

How to Create a MySQL Database if it Doesn't Exist - Very Easily

This is so easy I almost don't believe it, except I've tested it and it works.

$create = mysql_query("CREATE DATABASE IF NOT EXISTS databasename");

Crazy easy, no?

Friday, 6 August 2010

Line breaks

OK, so...

I am entering text in a flash text field, which gets sent to a mysql database. This later gets retrieved by php, and folded into a javascript function, so that another swf can use that data.

The problem is, if there are any line breaks in the initial text, then that ends up in the javascript function, and is seen as an error.

So to solve it, I will attempt to get the php file that takes the text out of the database, to convert that line break into something that the javascript function can stand, and that the swf can interpret.

Continuation -

Yeah, that worked. First I tried nl2br, which is supposed to put html linebreak tags where the line breaks are, although I din't see this effect. But this would have required me to make changes to my flash file.

So I used:

$lineBreaks = Array("\n","\r");
$iQuestion = ...get text from MySQL database
$oQuestion = str_replace($lineBreaks,'\u000d',$iQuestion);

$lineBreaks is an array of possible ways in which the line breaks may come down, I wasn't sure if this would differ depending on the user's operating system etc.

Wednesday, 23 June 2010

JavaScript CAN Call PHP Functions

So this is where I got to:

function gQ() {
var request = new XMLHttpRequest();
request.open("GET","getQus.php?val=What",false);
request.send();
val = request.responseText;
return val;
}

The thing is, this just gives me a query string, I can get that from PHP directly if I want one, and with less fuss.

Oh well... at least I know it can be done.

Monday, 21 June 2010

Sorting by date

Hurrah!

My date sorting problem is finally solved. Simply format your date with the most important figures first, and with leading zeros. Then any ordinary sort (ie alphabetical) will return an array of these sorted by year, then month, etc.

eg. 4.15pm 23rd September 2009 => '2009-09-23-16-15'

PS. Actionscript 3 considers 09 to represent the month of October, because January is 0.

Monday, 10 May 2010

Getting data from MySQL

Today, I have been mostly... not getting data from a MySQL database.

I thought I had this sorted already - I had a script that could send data to a php script, then receive a query string and format the data as a URL variable.

As usual, when you try to actually use it for something - it turns out to be more complicated.

First of all, using capital letters in the query string that PHP is outputting is apparently not a good idea! Can you imagine?!? It took me three hours just to pin the problem down to that!

So then I though I had the problem sorted, but no.

I think it might be something to do with the length of the query string...

More on this later?