Create a 'Latest News Section' with Macromedia Flash, PHP and MySql


posted: 24-10-05
author: PrimeVector
website: http://www.free-webmaster-resource.com
views: 23254
comments: 14
total pages: 5
current page: 1
page jump: 1 | 2 | 3 | 4 | 5

download source files


Live Example!

advice

Readers that dont have Flash 8 installed wont be able to view the source files. They were created with Flash 8. You can buy Macromedia Flash Pro 8 here, or just download a trial first. This is the latest and best version of Flash and we can do nothing else but recommend it to everybody because we absolutely love it. This new edition opens up a lot more of the magic that was already there, and it certainly is a new fresh wind for us Flash developers. Its a great investment for the future. Sorry for not having the source files available in all versions, if demand is high i will try to add more types of source files where needed?


What am i going to learn in this tutorial ?

We are going to setup a small Mysql database filled with some news items. We will then use PHP to get those news items displayed in Flash. We will use the loadVars.load to load the variables into Flash and append them to a receiving loadVars Object. We then take that data and display it in a html enabled dynamic textbox.


Let's begin with setting up the database

We will create a database that holds some news items. Each item has an id, a title, a body and a posted date. You can use the following SQL query to create the database. You can insert this query directly into the MySql command terminal, but i almost never use that personally. I prefer to use a database administrator program called phpMyAdmin or something similar. Programs like that give you a visual representation of your database and you can manipulate it all you want. The best things is that its free and a lot of hosts offer it in their basic package. So go and check your host if you have access to phpMyAdmin or else just install it. Now download the query to create the database here! and run it to create the database.



Check This Amazing Traffic Generating Widgetsl 4 Webmasters!

The PHP Part

<?
First we will define a few variables, the first one is the database host (usually localhost)
$server = "localhost";
Your database user name
$user = "your database username here";
Your database password
$pass = "your password here";
Your database name
$database = "your databse name here";
The table name
$tableName = "tutorial002_news";
Here we open the connection with mysql_connect , the @ is a way to not show certain messages a query can display. Some of the variables we defined above will be plugged in here to establish the connection with the correct parameters.
$conn = @mysql_connect($server,$user,$pass);
Now we select the database with "mysql_select_db", again we use the @.
$database = @mysql_select_db($database,$conn);
With "mysql_query" we setup and execute our query. What this query does is select *, which means select everything, from the table we defined in variable "$tableName". The result will be ordered by the posted date of the news item with "ORDER BY".
$query = @mysql_query("SELECT * FROM $tableName ORDER BY posted desc");
With "mysql_num_rows" we get the number of rows retrieved by our query. We will place this number in a variable called "$total_rows". The value of this variable will be sent to Flash and it will play a very important roll there. It will tell us if we have data or not, because if e.g. this variable stays empty we know we dont have any news items in our database! When you dont setup a total variable you can expect some nice cpu freeze ups.
$total_rows = @mysql_num_rows($query);
Here we setup a counter variable. When we go through the data our query retrieved, this counter will be increased. We will use its value in the final print to Flash.
$counter = 0;
With a while loop we loop through the data our query retrieved. The "mysql_fetch_array" actually gets that data and places it in an array called "$myNewsData"
while($myNewsData = @mysql_fetch_array($query)){
     We now can get the data from the $myNewsData array by calling the column names we
     had setup in the database. Which in this case were: id, title, body and posted. so
     $id = $myNewsData["id"]; gets all the ids and places them in a variable called "$id" for
     us to use when we start sending to Flash..
     $id = $myNewsData["id"];
     This gets all the titles and places them in a variable called "$title".
     $title = $myNewsData["title"];
     This gets all the body text the items have and places them in variable "$body".
     $body = $myNewsData["body"];
     This gets all the posted dates from the news items. In the next part of this tutorial we
     will create an admin to add new news items, there you wil see how we fill the posted
     column with a timestamp. So dont break your head too much on how we get this data
     yet! When we need to show the posted data we take that timestamp and convert it
     into a nice readable Date-Month-Year format everybody can read with "strftime".
     $posted = strftime("%d-%m-%y", $myNewsData["posted"]);
     Now we increase our counter variable.
     $counter++;
     Now we will start really sending data to Flash. To enable Flash to read a variable we
     setup here, the variable need an & in front of it. You can see that in effect in
     this part of the code: &news_data. Now have a look at this part &news_data$counter
     and see how our counter is appended to this variable. Because we are in a loop it will
     render to something like this: &news_data1 &news_data2 &news_data3 etc...
     The next part which might look strange to you is this part =$id|$name|$age|$country.
     This fills "news_data$counter" with the variables we defined above!
     So an example of how this whole line could render in this loop would be:
     &news_data1=1|news title 1|Hello i am news item1|1127236660
     &news_data2=2|news title 2|Hello i am news item2|1127954934
     Dont mind the (1127236660 and 1127954934), thats what a timestamp looks like!
     You can see a "|" between each variable like here $id|$name|$age|$country. I did
     this because now i can send a whole string of data all related to a certain news item
     and then in Flash use split to split it at the "|" chracter to unpack that string.
     print("&news_data$counter=$id|$title|$body|$posted");
Close the while loop
}
Here we print the total variable to Flash. The total variable tells us how many users were retrieved. In Flash this "total" variable is used to check if we have data. This also prevents computer freeze ups because when something goes wrong we now get an error trace instead of a nice cpu freeze :)
print("&total=$total_rows");
?>

On the next page you can find this PHP code again, but without the comments.




Check This Amazing Traffic Generating Widgetsl 4 Webmasters!

next page
page jump: 1 | 2 | 3 | 4 | 5
jump to comments

Comments

14 comment(s) found in 3 pages  
viewing comment(s): 1 - 6
page: 1 

1 | 2 | 3 |

13-12-07:d p rao

1.use receiver.sendAndLoad in place of receiver.Load 2.add: receiver,GET

12-12-07:d p rao

play with 'receiver.load("............................."). you can view this clear!

06-11-07:guest

think its an bad example when the script is causing problems to the browser. another tip. dont write in the php source code because i find it irritating to delete all the text 1 for 1.

31-03-07:UKbloke

The script crashes when run in browser or in flash 8. If any1 got it working pleae let us know what, if any changes were made. Thanks

28-03-07:sajjad from pak

could not view live example ...

24-02-07:guest

could not view live example.



- Press here to view all available smilies!





In Focus
Cool Stuff