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
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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.