PDA

View Full Version : Tutorials on how to dynamically name variables in PHP?


holdaway
01-28-2008, 10:32 PM
I am currently trying out loading images and pictures into Flash via PHP. Everything works fine with 1 image and 1 piece of text, but of course what I want to do is build a list of items, each with a picture and text to go with it.

This is the code after the general connecting code:
$outcome = mysql_query("SELECT eventImage,eventTitle, eventText FROM conferences1 WHERE id_num = '1'");
if (!$outcome) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($outcome);

echo $row[0]; // image name
echo $row[1]; // conference title
echo $row[2]; // conference text
// echo the variables
echo ("&confImage=$row[0]");
echo ("&confTitle=$row[1]");
echo ("&confText=$row[2]");

So after this, I just tell Flash to look for the variables 'confImage','confTitle' and 'confText'. Of course right now, it is only set to look on row 1 of the database. What I need to know is how to retrieve row items from the database and dynamically re-name them so that for example the picture names would become: &confImage1, $confImage2, etc.

So it looks as though I need to create a loop of some sort to toggle through all the rows and add a number to them. Anyone know how to help or have a handy link to a tutorial? Lots of links on how to make loops but haven't found any that deal with using a loop to re-name(declare) variables.

sneakyimp
02-22-2008, 11:03 PM
There is a really cool class written by some Italian guy that helps for transferring complicated data from PHP to flash:
http://www.sephiroth.it/test/unserializer/

You might check that out...it could save you a lot of time in the future. Not sure if it works for AS3.

In your case, perhaps try this in PHP. I have removed the WHERE clause from your query under the assumption that you want all events rather than just the one with id_num=1

$outcome = mysql_query("SELECT eventImage,eventTitle, eventText FROM conferences1")
or die('query failed');
$response_string = '';
$i = 0;
while($row = mysql_fetch_assoc($outcome)) {
$i++;
$response_string .= '&confImage' . $i . '=' . urlencode($row['eventImage']);
$response_string .= '&confTitle' . $i . '=' . urlencode($row['eventTitle']);
$response_string .= '&confText' . $i . '=' . urlencode($row['eventText']);
}
echo $response_string;