Scuba_Steve
07-06-2007, 03:36 PM
Ok folks i'm running around in circles trying to figure out why this won't work. What i'm looking to create is an app that allows people to log into a database and retrieve information in an "items" table for all items linked to their login name (email address).
So far everything works fine. The first part of my flash app handles the login and sends their login info to a php script that verifies the credentials and sends a value back to the flash app that allows it to play to the next section. In this next section, a 2nd php script pulls info from the "items" table based on the user's login name and sends that item info back to flash. flash then loops through and puts the data into a multi-dimensional array which is then set as the dataProvider for a datagrid.
I want users to be able to edit data within the datagrid and then be able to click "Save Changes" at which point Flash loops through the newly updated multi-dimensional array and updates the "items" table via a 3rd php script.
I'm at the point where I can pull the info down based on the username and populate the datagrid. Problem is, when I make changes and hit "Save Changes", those changes aren't reflected in the database. :confused:
I've posted my actionscript below as well as the 2 php scripts in question. If anyone can help me sort this out, there's $20 in it for you.
stop();
mc_confirmReset._visible = false;
saveChanges_btn.addEventListener("click", saveChanges);
reset_btn.addEventListener("click", resetGrid);
var fetch_lv:LoadVars = new LoadVars();
fetch_lv.username = _global.username; // sends our username to the php script so we know what products to query
// fyi this global var was defined earlier in the flash app so i know it's valid:)
var retrieve_lv:LoadVars = new LoadVars();
fetch_lv.sendAndLoad("http://www.mydomain.com/path/to/my/gridfeeder.php", retrieve_lv, "POST");
var prodArray:Array = new Array();
retrieve_lv.onLoad = function(success:Boolean) {
// If Flash is able to successfully send and load the variables from the server-side script...
if (success) {
trace ("Loading " + this.numRows + " products linked to " + this.username + "...");
var numRows:Number = this.numRows;
for (var i:Number = 0; i<numRows; i++) {
prodArray[i] = new Array();
prodArray[i]["Qty"] = this["invlevel" + i];
prodArray[i]["Use Inv"] = this["useinv" + i];
prodArray[i]["S-Price"] = this["saleprice" + i];
prodArray[i]["R-Price"] = this["regprice" + i];
prodArray[i]["Sale Type"] = this["pricestatus" + i];
prodArray[i]["Name"] = this["name" + i];
prodArray[i]["P/N"] = this["prodnum" + i];
};
setupGrid();
};
};
function setupGrid() {
productGrid.dataProvider = prodArray;
productGrid.editable = true; // To lock columns you have to make the whole grid editable and then certain columns uneditable...
productGrid.setStyleProperty("WordWrap", true);
productGrid.getColumnAt(0).editable = false; // We don't want them to be able to edit "P/N"
productGrid.getColumnAt(1).editable = false; // or "Name" columns.
productGrid.getColumnAt(0).width = 100;
productGrid.getColumnAt(1).width = 160;
productGrid.getColumnAt(2).width = 70;
productGrid.getColumnAt(3).width = 60;
productGrid.getColumnAt(4).width = 50;
productGrid.getColumnAt(5).width = 50;
};
var saveData_lv:LoadVars = new LoadVars();
var wasItSaved_lv:LoadVars = new LoadVars();
var continueOn:String = "Yes";
function saveChanges(){
trace("saveChanges() function triggered...");
for (var i:Number = 0; (i<prodArray.length) && (continueOn="Yes"); i++){
continueOn = "No";
saveData_lv.productNum = prodArray[i]["P/N"];
saveData_lv.newSaleType = prodArray[i]["Sale Type"];
saveData_lv.newRegPrice = prodArray[i]["R-Price"];
saveData_lv.newSalePrice = prodArray[i]["S-Price"];
saveData_lv.newUseInv = prodArray[i]["Use Inv"];
saveData_lv.newQty = prodArray[i]["Qty"];
saveData_lv.sendAndLoad("http://www.mydomain.com/path/to/my/gridsaver.php", wasItSaved_lv, "POST");
wasItSaved_lv.onLoad = function(success:Boolean){
if (success){
continueOn = this.continueOn;
trace("wasItSaved_lv onLoad function triggered");
};
};
trace ("continueOn: " + continueOn);
trace ("saveData_lv.productNum =" + saveData_lv.productNum);
trace ("saveData_lv.newRegPrice = " + saveData_lv.newRegPrice);
};
setupGrid(); // need this to make sure productArray is loaded with saved data bounced back from the db.
};
Note: the "continueOn" crap was my pathetic attempt to force that loop to run synchronously... I thought perhaps that my actionscript was hammering the php script that actually updated the database but when i stuck those trace statements into the function to see who was being trigger when, i found that the loop just kept running regardless of the onLoad being in there...
Now, the PHP scripts...
This one pulls the data I want based on the user's login name and sends it to Flash:
<?php // gridfeeder.php
$username = $_POST['username'];
include("login_info.php");
$connect = mysql_connect($host, $user, $pass);
if (!$connect){
die ("Feed unavailable.");
} else {
mysql_select_db($database, $connect);
$query = "SELECT prodnum, name, pricestatus, regprice, saleprice, useinv, invlevel FROM items WHERE shipemail='$username'";
$result = mysql_query($query);
$numRows = mysql_num_rows($result);
$toFlash = "&username=$username&numRows=$numRows";
$i = 0;
while($row=mysql_fetch_array($result)){
$toFlash .= "&prodnum$i=$row[prodnum]&name$i=$row[name]&pricestatus$i=$row[pricestatus]®price$i=$row[regprice]&saleprice$i=$row[saleprice]&useinv$i=$row[useinv]&invlevel$i=$row[invlevel]";
$i++;
};
};
echo $toFlash;
mysql_close($connect);
?>
And this script is SUPPOSED to receive the new item information and update the database accordingly:
<?php // gridsaver.php
$productNum = $_POST['productNum'];
$newSaleType = $_POST['newSaleType'];
$newRegPrice = $_POST['newRegPrice'];
$newSalePrice = $_POST['newSalePrice'];
$newUseInv = $_POST['newUseInv'];
$newQty = $_POST['newQty'];
include("login_info.php");
$connect = mysql_connect($host, $user, $pass3);
if (!$connect){
die ("Feed unavailable.");
} else {
mysql_select_db($database, $connect);
$query = "UPDATE items SET pricestatus='$newSaleType', regprice='$newRegPrice', saleprice='$newSalePrice', useinv='$newUseInv', invlevel='$newQty' WHERE prodnum='$productNum'";
$result = mysql_query($query);
$response = mysql_num_rows($result);
$continueOn = "Yes";
};
echo "&response=" . $response . "&continueOn=" . $continueOn;
mysql_close($connect);
?>
Any ideas? Been fudging with it for 2 weeks now but just keep banging my head against the wall.:o
v/r,
Steve
So far everything works fine. The first part of my flash app handles the login and sends their login info to a php script that verifies the credentials and sends a value back to the flash app that allows it to play to the next section. In this next section, a 2nd php script pulls info from the "items" table based on the user's login name and sends that item info back to flash. flash then loops through and puts the data into a multi-dimensional array which is then set as the dataProvider for a datagrid.
I want users to be able to edit data within the datagrid and then be able to click "Save Changes" at which point Flash loops through the newly updated multi-dimensional array and updates the "items" table via a 3rd php script.
I'm at the point where I can pull the info down based on the username and populate the datagrid. Problem is, when I make changes and hit "Save Changes", those changes aren't reflected in the database. :confused:
I've posted my actionscript below as well as the 2 php scripts in question. If anyone can help me sort this out, there's $20 in it for you.
stop();
mc_confirmReset._visible = false;
saveChanges_btn.addEventListener("click", saveChanges);
reset_btn.addEventListener("click", resetGrid);
var fetch_lv:LoadVars = new LoadVars();
fetch_lv.username = _global.username; // sends our username to the php script so we know what products to query
// fyi this global var was defined earlier in the flash app so i know it's valid:)
var retrieve_lv:LoadVars = new LoadVars();
fetch_lv.sendAndLoad("http://www.mydomain.com/path/to/my/gridfeeder.php", retrieve_lv, "POST");
var prodArray:Array = new Array();
retrieve_lv.onLoad = function(success:Boolean) {
// If Flash is able to successfully send and load the variables from the server-side script...
if (success) {
trace ("Loading " + this.numRows + " products linked to " + this.username + "...");
var numRows:Number = this.numRows;
for (var i:Number = 0; i<numRows; i++) {
prodArray[i] = new Array();
prodArray[i]["Qty"] = this["invlevel" + i];
prodArray[i]["Use Inv"] = this["useinv" + i];
prodArray[i]["S-Price"] = this["saleprice" + i];
prodArray[i]["R-Price"] = this["regprice" + i];
prodArray[i]["Sale Type"] = this["pricestatus" + i];
prodArray[i]["Name"] = this["name" + i];
prodArray[i]["P/N"] = this["prodnum" + i];
};
setupGrid();
};
};
function setupGrid() {
productGrid.dataProvider = prodArray;
productGrid.editable = true; // To lock columns you have to make the whole grid editable and then certain columns uneditable...
productGrid.setStyleProperty("WordWrap", true);
productGrid.getColumnAt(0).editable = false; // We don't want them to be able to edit "P/N"
productGrid.getColumnAt(1).editable = false; // or "Name" columns.
productGrid.getColumnAt(0).width = 100;
productGrid.getColumnAt(1).width = 160;
productGrid.getColumnAt(2).width = 70;
productGrid.getColumnAt(3).width = 60;
productGrid.getColumnAt(4).width = 50;
productGrid.getColumnAt(5).width = 50;
};
var saveData_lv:LoadVars = new LoadVars();
var wasItSaved_lv:LoadVars = new LoadVars();
var continueOn:String = "Yes";
function saveChanges(){
trace("saveChanges() function triggered...");
for (var i:Number = 0; (i<prodArray.length) && (continueOn="Yes"); i++){
continueOn = "No";
saveData_lv.productNum = prodArray[i]["P/N"];
saveData_lv.newSaleType = prodArray[i]["Sale Type"];
saveData_lv.newRegPrice = prodArray[i]["R-Price"];
saveData_lv.newSalePrice = prodArray[i]["S-Price"];
saveData_lv.newUseInv = prodArray[i]["Use Inv"];
saveData_lv.newQty = prodArray[i]["Qty"];
saveData_lv.sendAndLoad("http://www.mydomain.com/path/to/my/gridsaver.php", wasItSaved_lv, "POST");
wasItSaved_lv.onLoad = function(success:Boolean){
if (success){
continueOn = this.continueOn;
trace("wasItSaved_lv onLoad function triggered");
};
};
trace ("continueOn: " + continueOn);
trace ("saveData_lv.productNum =" + saveData_lv.productNum);
trace ("saveData_lv.newRegPrice = " + saveData_lv.newRegPrice);
};
setupGrid(); // need this to make sure productArray is loaded with saved data bounced back from the db.
};
Note: the "continueOn" crap was my pathetic attempt to force that loop to run synchronously... I thought perhaps that my actionscript was hammering the php script that actually updated the database but when i stuck those trace statements into the function to see who was being trigger when, i found that the loop just kept running regardless of the onLoad being in there...
Now, the PHP scripts...
This one pulls the data I want based on the user's login name and sends it to Flash:
<?php // gridfeeder.php
$username = $_POST['username'];
include("login_info.php");
$connect = mysql_connect($host, $user, $pass);
if (!$connect){
die ("Feed unavailable.");
} else {
mysql_select_db($database, $connect);
$query = "SELECT prodnum, name, pricestatus, regprice, saleprice, useinv, invlevel FROM items WHERE shipemail='$username'";
$result = mysql_query($query);
$numRows = mysql_num_rows($result);
$toFlash = "&username=$username&numRows=$numRows";
$i = 0;
while($row=mysql_fetch_array($result)){
$toFlash .= "&prodnum$i=$row[prodnum]&name$i=$row[name]&pricestatus$i=$row[pricestatus]®price$i=$row[regprice]&saleprice$i=$row[saleprice]&useinv$i=$row[useinv]&invlevel$i=$row[invlevel]";
$i++;
};
};
echo $toFlash;
mysql_close($connect);
?>
And this script is SUPPOSED to receive the new item information and update the database accordingly:
<?php // gridsaver.php
$productNum = $_POST['productNum'];
$newSaleType = $_POST['newSaleType'];
$newRegPrice = $_POST['newRegPrice'];
$newSalePrice = $_POST['newSalePrice'];
$newUseInv = $_POST['newUseInv'];
$newQty = $_POST['newQty'];
include("login_info.php");
$connect = mysql_connect($host, $user, $pass3);
if (!$connect){
die ("Feed unavailable.");
} else {
mysql_select_db($database, $connect);
$query = "UPDATE items SET pricestatus='$newSaleType', regprice='$newRegPrice', saleprice='$newSalePrice', useinv='$newUseInv', invlevel='$newQty' WHERE prodnum='$productNum'";
$result = mysql_query($query);
$response = mysql_num_rows($result);
$continueOn = "Yes";
};
echo "&response=" . $response . "&continueOn=" . $continueOn;
mysql_close($connect);
?>
Any ideas? Been fudging with it for 2 weeks now but just keep banging my head against the wall.:o
v/r,
Steve