Jump to content

Array Loop with more than 1 value?


Ruddy

Recommended Posts

		// Get the users armour names
	$user_armour = array($user_head, $user_upper, $user_hands, $user_lower, $user_feet);
	$i=0;  
	while($i < count($user_armour)) {  
	$query1="SELECT * FROM armour WHERE id='$user_armour[$i]'";
	$result1=mysql_query($query1);
	$array1=mysql_fetch_array($result1);

	$armour_name[$i]=$array1["name"];

	$i++;
	}
	// set the users armours name
	$user_prot = array($user_head, $user_upper, $user_hands, $user_lower, $user_feet);

	// Get the users overall protection
	$i=0;  
	while($i < count($user_prot)) {  
	$query1="SELECT * FROM armour WHERE id='$user_prot[$i]'";
	$result1=mysql_query($query1);
	$array1=mysql_fetch_array($result1);

	$armour_prot[$i]=$array1["protection"];

	$i++;
	}
	// set the users protection
	$user_protection=$armour_prot[0] + $armour_prot[1] + $armour_prot[2] + $armour_prot[3] + $armour_prot[4];

 

Is there a way to get them as one so:

$user_armour = array($user_head, $user_upper, $user_hands, $user_lower, $user_feet);
	$i=0;  
	while($i < count($user_armour)) {  
	$query1="SELECT * FROM armour WHERE id='$user_armour[$i]'";
	$result1=mysql_query($query1);
	$array1=mysql_fetch_array($result1);

	$armour_name[$i]=$array1["name"];
                $armour_prot[$i]=$array1["protection"];

	$i++;
	}
	// set the users armours name
	$user_prot = array($user_head, $user_upper, $user_hands, $user_lower, $user_feet);

 

Will that work and if so how would you then get the 2 different values out of say "$user_prot[0]".

 

Cheers guys,

Ruddy

Link to comment
https://forums.phpfreaks.com/topic/257992-array-loop-with-more-than-1-value/
Share on other sites

In your code $user_prot and $user_armour are the exact same arrays, you can test them out by using print_r on the variables that contain those arrays.

 

That immediately makes your code redundant and is where your problems start.

 

Two get multiple values out of $user_prot like you are asking, you will need to assign another array to the key you want to have multiple values.

 

So $user_prot[0] will need to be an array, it'll have two extra keys 0 and 1 which can contain the two values that you need.

 

Also look into using foreach instead of while, i believe you'll find it easier to work with and recreate arrays that way.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.