How to addchild for each array value with an incremented int?

Hello I am trying to addChild for an array that I have.
I want to make it so if I have values in my database table it displays those values on
the movieclip.
my problem is I don’t want to have to add a line for each addChild, I want it to be based on what is in the database.

Here is what I have sofar

public class shop extends MovieClip
{
	public static const shopURL:String = "http://localhost/scripts/shop.php";
	public var menu_request:URLRequest;
	public var menu_url:URLRequest;
	public var _loader:URLLoader;
	public function shop():void
	{
	 	loadShopMenuData();
	}
	
	private function loadShopMenuData():void {
		// base section lets get some initial rank data
		var randomParam:String = "?p=" + Math.floor(Math.random() * (10000000));
		_loader = new URLLoader();
		menu_url = new URLRequest(shopURL);
		menu_request = new URLRequest(shopURL + "?action=menus");
		menu_request.method = URLRequestMethod.POST;
		_loader.addEventListener(Event.COMPLETE, onLoadMenuData);
		_loader.load(menu_request);				
	}

	public function onLoadMenuData(e:Event):void {
		
		
		var menu:String = e.target.data;
		var menuMC:menuBtn = new menuBtn();
		var menuArray:Array=menu.split(",");
		for each(menu in menuArray)
		{			
			if(menuArray[1] != null)
			{
				menuMC.mainMenu.menuName.text = menuArray[1];
				menuMC.x = 100;
				menuMC.y = 100;
				addChild(menuMC);
			}
		}
			
		
	}
}	

How would I addChild and change its Y position by 25 for each addChild that is to be added?
and also add an incremented value of 1 for each menuArray[] so basically make it so it does an addChild for reach database entry and moves it down appropriately

sorry if my explanation isn’t the best but I am sometimes pretty bad at explaining things haha

Potentially something like this:

var menuMC:menuBtn
var menuArray:Array = e.target.data.split(",")
for(var i:int = 0; i < menuArray.length; i++) {
    menuMC = new menuBtn();		
    menuMC.mainMenu.menuName.text = menuArray[i];
    menuMC.x = 100;
    menuMC.y = 100 + i * 25;
    addChild(menuMC);		
}
1 Like

thank you soo much!

1 Like

You’re welcome.

1 Like

actually one more question if I may.
I have an extra movieclip with no name at the beginning and end of the array of clips on stage.
so one at the top and one at the bottom.
I tried

if(menuMC.mainMenu.menuName.text == "")
				{
					removeChild(menuMC);
				}

but it only got rid of one of them
any ideas?

here is my php file

<?php
// defining main variables


// Include config file
	include('./common.php');

// Connect to database
	$link = dbConnect();
	// getting data
	$data = "";
	
		$query = "SELECT * FROM ".TABLE_PREFIX."_shop_menu";
		$result = mysql_query($query) or die ("no query");
		$menu = $result['name'];
		$result_array = array();
		while($row = mysql_fetch_assoc($result))
		{
			if($row['name'] != null)
			{				
			echo', '.$row['name'];
			}
		}
		
	 
	 switch($_GET["action"]) 
				{

					case "menus":	
							$data .= ', '.$row['name'];
							die($data);
						break;	
						default:
							 echo "Unknown action";
						break;
				}		
?>

I don’t know PHP, so there may be a way to handle it from that end. To do it in AS, you can try this:

var menuMC:menuBtn
var menuArray:Array = e.target.data.split(",")
for(var i:int = 0; i < menuArray.length; i++) {
    if(menuArray[i] == "") continue;
    menuMC = new menuBtn();		
    menuMC.mainMenu.menuName.text = menuArray[i];
    menuMC.x = 100;
    menuMC.y = 100 + i * 25;
    addChild(menuMC);		
}
1 Like

Ok I figured it out it was a problem with my php file.
I had to change

$data .= ', '.$row[‘name’];
to this
$data .= $row[‘name’];

Hey sorry to bring back my thread, but I kinda have a new problem.
I changed from php to c# for a medium to mysql.
could you possibly tell me why it is that when I addChild using this method below I either get too many clips added or not enough or when they are there sometimes the text field values I want added are mixed up, like where I want character level it says the character name or something like this?

				    private function charList(event:ProgressEvent):void {
   	 while(socket.bytesAvailable)
		{
		 		var str:String = this.socket.readUTFBytes(this.socket.bytesAvailable);
				// post my stat values :)
				var val:Array = str.split("+|+");
				var Names:Array = [val[1], val[17], val[24], val[36], val[48]];//, val[60], val[72], val[84], val[96], val[108]];
				var Levels:Array = [val[2], val[18], val[25], val[37], val[49]];//, val[61], val[73], val[85], val[97], val[109]];
				var Races:Array = [val[3], val[19], val[26], val[38], val[50]];//, val[62], val[74], val[86], val[98], val[110]];
				var Genders:Array = [val[4], val[20], val[27], val[39], val[51]];//, val[63], val[75], val[87], val[99], val[111]];
				var Classes:Array = [val[5], val[21], val[28], val[40], val[52]];//, val[64], val[76], val[88], val[100], val[112]];
				
				for(var i:int = 0; i < Names.toString().length; i++) {	
						var cSel:CS = new CS();
						cSel.chrNam.text = "" + Names[i];
						cSel.chrLvl.text = "" + Levels[i];							
						cSel.chrRce.text = "" + Races[i];
						cSel.chrGen.text = "" + Genders[i];
						cSel.chrCls.text = "" + Classes[i];
						cSel.x = 100;
						cSel.y = 100 + i * 60;
						addChild(cSel);
						
		}
			}
								
		 
	}

only thing I can think of is that flash is not keeping up with adding the children with the values as fast as c# is sending them.